-
Notifications
You must be signed in to change notification settings - Fork 3
/
photobackup.js
executable file
·110 lines (97 loc) · 3.88 KB
/
photobackup.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
#!/usr/bin/env node
/*
* Copyright (C) 2013-2016 Stéphane Péchard.
*
* This file is part of PhotoBackup.
*
* PhotoBackup is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PhotoBackup is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function () {
'use strict';
// command line documentation
var doc = '\n' +
'PhotoBackup NodeJS server.\n' +
'\n' +
'Usage:\n' +
' photobackup init [<username>]\n' +
' photobackup run [<username>]\n' +
' photobackup list\n' +
' photobackup (-h | --help)\n' +
' photobackup --version\n' +
'\n' +
'Options:\n' +
' -h --help Show this screen.\n' +
' --version Show version.\n';
// imports
var docopt = require('docopt').docopt;
var fs = require('fs');
var ini = require('ini');
var pbInit = require('./init').init;
var path = require('path');
var packagejson = require(path.join(__dirname, 'package.json'));
// variables
var expressApp = require('./express_app');
var app = expressApp.app;
var args = docopt(doc, {version: packagejson.version});
var home = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
var configPath = path.join(home, '.photobackup');
var config = {};
// compute some internal information
var username = ''; // default
if (args.hasOwnProperty('<username>') && args['<username>'] !== null) {
username = args['<username>'];
}
var sectionName = 'photobackup'; // default
if (username.length > 0) {
sectionName += '-' + username;
}
// which command to activate
if (args.init) {
pbInit(configPath, username, sectionName);
} else if (args.run) {
try {
config = ini.parse(fs.readFileSync(configPath, 'utf-8'));
if (!config.hasOwnProperty(sectionName)) {
console.error('ERROR: Unknown username in the current configuration...');
process.exit();
}
var port = config[sectionName].Port || 8420;
var address = config[sectionName].BindAddress || '127.0.0.1';
app.listen(port, address, function () {
console.log('PhotoBackup client listening on http://' + address + ':' + port + '\n');
});
} catch (e) {
if (e instanceof Error && e.code === 'ENOENT') {
var option = (username.length > 0) ? ' ' + username : '';
console.error("Can't read configuration file, running 'photobackup init" + option + "'");
pbInit(configPath, username, sectionName);
} else {
console.error('Unknown error: ' + e);
}
}
} else if (args.list) {
try {
config = ini.parse(fs.readFileSync(configPath, 'utf-8'));
var list = Object.keys(config).join('\n')
.replace(/photobackup-/g, '- ')
.replace(/^photobackup/g, '<unnamed one>');
console.log('Runnable PhotoBackup configurations are:\n' + list);
} catch (e) {
console.log("No configuration file found, run 'photobackup init' to create one");
}
}
if (config.hasOwnProperty(sectionName)) {
expressApp.createRoutes(config, sectionName);
}
}());