This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·114 lines (89 loc) · 3.24 KB
/
index.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
#!/usr/bin/env node
'use strict';
const argv = require('./yargs.js');
const fs = require('fs');
const request = require('request');
var config = {};
config.bridgeIp = argv.bridgeIp;
config.bridgeUser = argv.bridgeUser;
config.endpoints = argv.endpoints;
config.backupDir = argv.backupDir || "./" + argv.bridgeUser;
/**
* backup endpoint to a local directory
* @param {string} backup directory
* @param {string} hue endpoint
*/
module.exports.backup = (backupDir, endpoint) => {
if (typeof endpoint === 'undefined') return;
const outfile = backupDir + '/' + endpoint + '.json';
const url = 'http://' + config.bridgeIp + "/api/" + config.bridgeUser + "/" + endpoint;
if (!fs.existsSync(backupDir)) {
console.log('creating ' + backupDir);
fs.mkdirSync(backupDir);
}
console.log('getting ' + url);
request(url, { method: 'GET', json: true }, (err, res, body) => {
if (err) { return console.log(err); }
fs.writeFile(outfile, JSON.stringify(body, null, 2), (err) => {
if (err) throw err;
console.log('saved ' + outfile);
});
return nextEndpoint();
});
};
/**
* restore endpoint from a local directory
* @param {string} backup directory
* @param {string} hue endpoint
*/
module.exports.restore = (backupDir, endpoint) => {
const putResource = (resource) => {
if (typeof resource === 'undefined') return nextResource();
delete resource.owner;
delete resource.created;
delete resource.lasttriggered;
delete resource.timestriggered;
console.log('putting ' + (resourceIdx + 1) + '/' + resources.length + ' ' + endpoint + ' to ' + url);
request(url, { method: 'PUT', json: true, body: resource}, (err, res, body) => {
if (err) { return console.log(err); }
console.log(body);
return nextResource();
});
};
const nextResource = () => {
resourceIdx++;
if (resourceIdx >= resources.length) return nextEndpoint();
setTimeout( function(res) {
putResource(res);
}.bind(this, resources[resourceIdx]), 1000);
return;
};
const infile = backupDir + '/' + endpoint + '.json';
const url = 'http://' + config.bridgeIp + "/api/" + config.bridgeUser + "/" + endpoint;
if (!fs.existsSync(backupDir)) {
console.log('backup dir does not exist: ' + backupDir);
return;
}
if (!fs.existsSync(infile)) {
console.log('backup file does not exist: ' + infile);
return;
}
const json = JSON.parse(fs.readFileSync(infile, 'utf8'));
let resources = [];
if (endpoint === 'config')
resources = [json];
else
resources = Object.values(json);
let resourceIdx = 0;
putResource(resources[resourceIdx]);
};
const nextEndpoint = () => {
endpointIdx++;
if (endpointIdx >= config.endpoints.length) return;
setTimeout( function(backupDir, endpoint) {
this[argv._[0]](backupDir, endpoint);
}.bind(this, config.backupDir, config.endpoints[endpointIdx]), 1000);
return;
};
let endpointIdx = 0;
this[argv._[0]](config.backupDir, config.endpoints[endpointIdx]);