summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java
blob: c47645b45376ac1d829fb5958a88e21663bd82a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.keuin.kbackupfabric.config;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.keuin.kbackupfabric.util.PrintUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class KBackupConfig {

    private static KBackupConfig instance = getDefault();
    private static final String CONFIG_FILE = "kbackup.json";

    @JsonProperty("incbak_cow")
    private Boolean incbakCow;

    public static KBackupConfig getInstance() {
        return instance;
    }

    private static KBackupConfig getDefault() {
        return new KBackupConfig(false);
    }

    public static void load(File file) throws IOException {
        ObjectMapper om = new ObjectMapper();
        om.enable(JsonParser.Feature.ALLOW_COMMENTS);
        try {
            instance = om.readValue(file, KBackupConfig.class);
        } catch (FileNotFoundException ignored) {
            // generate default config file
            PrintUtil.info("Config file does not exist. Creating default config: " + file.getAbsolutePath());
            instance = getDefault();
            ObjectWriter w = om.writerWithDefaultPrettyPrinter();
            w.writeValue(file, instance);
        }
        validate(instance);
    }

    private static void validate(KBackupConfig cfg) {
        if (cfg.incbakCow == null) {
            PrintUtil.warn("incbak_cow could not be null");
            cfg.incbakCow = false;
        }
    }

    public static void load() throws IOException {
        load(new File(CONFIG_FILE));
    }

    public KBackupConfig() {
    }

    public KBackupConfig(Boolean incbakCow) {
        this.incbakCow = incbakCow;
    }

    public Boolean getIncbakCow() {
        return this.incbakCow;
    }

    public void setIncbakCow(Boolean incbakCow) {
        this.incbakCow = incbakCow;
    }
}