-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.js
91 lines (79 loc) · 2.74 KB
/
configuration.js
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
const path = require('path');
const fs = require('fs');
const os = require('os');
class Configuration {
constructor(filename = ".restic_cli") {
this.filename = filename;
this.repositories = [];
this.locations = {};
this.selected_repo = "";
this._cfg = this.loadConfig();
this.parseConfig(this._cfg);
}
loadConfig() {
const configpath = path.resolve(path.join(os.homedir(), this.filename));
var config = {};
try {
config = JSON.parse(fs.readFileSync(configpath, 'utf-8'));
} catch (error) { }
if (!config.repositories) config.repositories = [];
if (!config.locations) config.locations = [];
if (!config.selected) config.selected = "";
return config;
}
saveConfig(config) {
const configpath = path.resolve(path.join(os.homedir(), this.filename));
fs.writeFileSync(configpath, JSON.stringify(config, false, 2));
}
parseConfig(config) {
if (config.repositories) {
this.repositories = config.repositories;
}
if (config.locations) {
this.locations = config.locations;
}
// Make sure selected repository exists
if (config.selected && !this.repositories.find((x) => x.name == config.selected)) config.selected = "";
else this.selected_repo = config.selected;
// Select repository when no selected
if (!config.selected) {
if (this.repositories.length > 0) {
this.selected_repo = this.repositories[0].name;
}
}
}
save() {
this._cfg.repositories = this.repositories;
this._cfg.locations = this.locations;
this._cfg.selected = this.selected_repo;
this.saveConfig(this._cfg);
}
add_backuplocation(backup_location, tags = "") {
let matching = this.locations.find((x) => x.path == backup_location);
if (matching) {
matching['tags'] = tags;
this.save();
return true;
} else {
let nbl = { path: backup_location };
if (tags) nbl.tags = tags;
this.locations.push(nbl);
this.save();
return true;
}
}
remove_backuplocation(backup_location) {
let matching = this.locations.find((x) => x.path == backup_location);
if (matching) {
let index = this.locations.indexOf(matching);
this.locations.splice(index, 1);
this.save();
return true;
}
}
get_selected_repo(repo_name = "") {
let rname = repo_name ? repo_name : this.selected_repo;
return this.repositories.find((x) => x.name == rname);
}
}
module.exports = Configuration;