mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
19.14.2022 725909255d81bccf67bac844fcd8fe2e978cae9a
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package de.micromata.borgbutler.server;
 
import org.apache.commons.lang3.StringUtils;
 
public class BorgConfig {
    public static final String BORG_DEFAULT_DOWNLOAD_VERSION = "1.1.17";
 
    private String[][] borgBinaries = {
            {"freebsd64", "FreeBSD 64"},
            {"linux32", "Linux 32"},
            {"linux64", "Linux 64"},
            {"macosx64", "MacOS X 64"}};
 
    private String minimumRequiredBorgVersion = "1.1.8";
 
    public String getBinariesDownloadUrl() {
        return "https://github.com/borgbackup/borg/releases/download/" + version + "/";
    }
 
    /**
     * One of the values "macosx64", "linux64" etc. for using a binary provided by BorgButler or null / "manual" for
     * using a manual installed borg version.
     */
    private String borgBinary;
 
    private boolean versionOK = false;
    private String version = BORG_DEFAULT_DOWNLOAD_VERSION;
    private String statusMessage;
 
    public BorgConfig copyFrom(BorgConfig other) {
        this.borgBinary = other.borgBinary;
        this.versionOK = other.versionOK;
        this.version = other.version;
        this.statusMessage = other.statusMessage;
        return this;
    }
 
    public String[][] getBorgBinaries() {
        return this.borgBinaries;
    }
 
    /**
     * @return The minimal required borg version (installed on host).
     */
    public String getMinimumRequiredBorgVersion() {
        return this.minimumRequiredBorgVersion;
    }
 
    public String getBorgBinary() {
        return this.borgBinary;
    }
 
    public boolean isVersionOK() {
        return this.versionOK;
    }
 
    public String getVersion() {
        return this.version;
    }
 
    public String getStatusMessage() {
        return this.statusMessage;
    }
 
    BorgConfig setBorgBinary(String borgBinary) {
        this.borgBinary = borgBinary;
        return this;
    }
 
    BorgConfig setVersionOK(boolean versionOK) {
        this.versionOK = versionOK;
        return this;
    }
 
    BorgConfig setVersion(String version) {
        if (StringUtils.isNotBlank(version)) {
            this.version = version;
        } else {
            this.version = BORG_DEFAULT_DOWNLOAD_VERSION;
        }
        return this;
    }
 
    BorgConfig setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
        return this;
    }
 
    public static int compareVersions(String thisVersion, String otherVersion) {
        String[] thisParts = checkVersion(thisVersion);
        String[] otherParts = checkVersion(otherVersion);
        int length = Math.max(thisParts.length, otherParts.length);
        for (int i = 0; i < length; i++) {
            int thisPart = i < thisParts.length ?
                    Integer.parseInt(thisParts[i]) : 0;
            int thatPart = i < otherParts.length ?
                    Integer.parseInt(otherParts[i]) : 0;
            if (thisPart < thatPart)
                return -1;
            if (thisPart > thatPart)
                return 1;
        }
        return 0;
    }
 
    // https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java
    public static String[] checkVersion(String version) {
        if (version == null) {
            throw new IllegalArgumentException("Version can not be null");
        }
        if (!version.matches("[0-9]+(\\.[0-9]+)*")) {
            throw new IllegalArgumentException("Invalid version format: " + version);
        }
        return version.split("\\.");
    }
 
    @Override
    public String toString() {
        return "BorgConfig{" +
                ", borgBinary='" + borgBinary + '\'' +
                ", versionOK=" + versionOK +
                ", version='" + version + '\'' +
                ", statusMessage='" + statusMessage + '\'' +
                '}';
    }
}