-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
210 lines (198 loc) · 5.16 KB
/
renderer.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* This is the code for the "renderer" thread of the electron app.
* Copyright (c) 2019 by G. Weirich
* License and terms: see LICENSE
*/
const { spawn } = require('child_process')
const { dialog } = require('electron').remote
const fixPath = require('fix-path');
const Conf = require('electron-store')
const cfg = new Conf()
/* Load the configuration data */
let setname = cfg.get("set") || "default"
let set = Object.assign({}, {
name: setname,
repoaddress: "~/restic_repo".replace("~", require('os').homedir()),
repopwd: "topsecret",
repodirs: __dirname,
s3key: "",
s3secret: ""
}, cfg.get(setname))
cfg.set(set.name, set)
cfg.set("set", set.name)
let restic = cfg.get("restic") || "restic"
fixPath()
setValues()
/**
* Set the GUI display to the values stored in the configuration
*/
function setValues(){
let el = document.getElementById("disp_reponame")
el.textContent = set.repoaddress
el = document.getElementById("setname")
el.textContent = setname
el = document.getElementById("repourl")
el.value = set.repoaddress
el = document.getElementById('repopwd')
el.value = set.repopwd
el = document.getElementById("disp_dirs")
el.textContent = set.repodirs
el = document.getElementById('repodirs')
el.value = set.repodirs
el = document.getElementById("s3key")
el.value = set.s3key
el = document.getElementById("s3secret")
el.value = set.s3secret
el = document.getElementById('setnames')
const store = cfg.store
const sel = el.selectedIndex
for (k in store) {
const o = store[k]
if (typeof (o) === 'object' && o.name) {
addOption(o.name)
}
}
addOption("new...", "new...")
for (let i = 0; i < el.options.length; i++) {
if (el.options[i].text === setname) {
el.options.selectedIndex = i
}
}
function addOption(n) {
for (let i = 0; i < el.options.length; i++) {
if (el.options[i].text === n) {
return
}
}
const option = document.createElement("option")
option.value = n
option.text = n
el.appendChild(option)
}
}
/**
* Get the selected/entered vakues from the GUI and store them in the configuration
*/
function getValues(){
el = document.getElementById("repourl")
set.repoaddress = el.value
el = document.getElementById('repopwd')
set.repopwd = el.value
el = document.getElementById('repodirs')
set.repodirs = el.value
el = document.getElementById("s3key")
set.s3key = el.value
el = document.getElementById("s3secret")
set.s3secret = el.value
//el = document.getElementById('setnames')
cfg.set(setname, set)
}
/**
* Save a new backup set
*/
function save() {
const inp = document.getElementById("newsetname")
if (inp.style.display == "inline") {
setname = inp.value
}
if (setname) {
set.name = setname
cfg.set("set", setname)
getValues()
toggle()
setValues()
inp.value = ""
inp.style.display = "none"
} else {
alert("Please enter a name for this backup profile")
inp.focus()
}
}
/**
* The user changed the backupt set in the <select> dropdown, Either it's the name of
* an existing backup set or the last entry, which is "new...". If so, we create a new backup set.
*/
function changeset() {
const sel = document.getElementById("setnames")
const backupset = sel.selectedOptions[0].value
if (backupset === "new...") {
set = {
name: "",
repoaddress: "",
repopwd: "",
s3key: "",
s3secret: "",
repodirs: ""
}
setname = ""
const inp = document.getElementById("newsetname")
inp.style.display = "inline"
inp.focus()
} else {
set = cfg.get(backupset)
setname = backupset
document.getElementById("newsetname").style.display = "none"
cfg.set("set", setname)
}
setValues()
}
/**
* The user clicked the icon to display either the execute panel or the edit panel
*/
function toggle() {
const exec = document.getElementById("exec")
const edit = document.getElementById("edit")
if (exec.style.display === "none") {
exec.style.display = "block"
edit.style.display = "none"
} else {
exec.style.display = "none"
edit.style.display = "block"
}
}
/**
* Run a restic-command
* @param {...any} cmd
*/
function do_spawn(...cmd) {
const vconsole = document.getElementById('console')
vconsole.value += `\nCommand: restic -r ${set.repoaddress} ${cmd.join(" ")}\n`
const env = Object.assign({}, process.env)
env.RESTIC_PASSWORD = set.repopwd
env.AWS_ACCESS_KEY = set.s3key
env.AWS_SECRET_KEY= set.s3secret
const args = ["-r", set.repoaddress, ...cmd]
const proc = spawn(restic, args, { env })
proc.on("error", err => {
if (err.code == "ENOENT") {
dialog.showMessageBox({
type: "error",
title: "Restic not found",
message: "The restic executable was not found. Please make sure, restic is installed and on the Path."
})
}
})
proc.stdout.on('data', chunk => {
vconsole.value += chunk;
})
proc.stderr.on('data', err => {
vconsole.value += "!!!! " + err
console.log("err: " + err)
})
proc.on("close", exitcode => {
const x = exitcode === 0 ? "no errors." : "Command returned error code: " + exitcode
vconsole.value += `\n${x}\n----------------\n`
})
}
function init() {
do_spawn("init")
}
function list() {
do_spawn("snapshots")
}
function backup() {
do_spawn("backup", set.repodirs)
}
function restore() {
do_spawn("xrestore", "latest", "--target", set.repodirs)
}