-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·202 lines (185 loc) · 8.41 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const prompt = require('prompt');
const auth = require('./util/get_auth');
const settings = require('./package.json');
/**
* @class Hecate
*/
class Hecate {
/**
* @param {Object} api Global API Settings Object
* @param {string} api.url URL of Hecate instance to interact with
* @param {string} api.username Hecate Username
* @param {string} api.password Hecate Password
* @param {Object} api.auth_rules [optional] If used as a library, an object containing the
* authentication rules of the instance as retrieved from
* /api/auth
* The CLI will automatically attempt to populate this value
*/
constructor(api = {}) {
this.url = api.url ? new URL(api.url).toString() : 'http://localhost:8000';
this.user = false;
if ((api.username || process.env.HECATE_USERNAME) && (api.password || process.env.HECATE_PASSWORD)) {
this.user = {
username: api.username ? api.username : process.env.HECATE_USERNAME,
password: api.password ? api.password : process.env.HECATE_PASSWORD
};
}
this.auth_rules = api.auth_rules ? api.auth_rules : null;
// Instantiate New Library Instances
this._ = {
auth: new (require('./lib/auth'))(this),
bbox: new (require('./lib/bbox'))(this),
webhooks: new (require('./lib/webhooks'))(this),
tiles: new (require('./lib/tiles'))(this),
clone: new (require('./lib/clone'))(this),
bounds: new (require('./lib/bounds'))(this),
feature: new (require('./lib/feature'))(this),
deltas: new (require('./lib/deltas'))(this),
user: new (require('./lib/user'))(this),
schema: new (require('./lib/schema'))(this),
server: new (require('./lib/server'))(this),
import: new (require('./lib/import'))(this),
revert: new (require('./lib/revert'))(this)
};
// Add Helper Functions
this.auth = (...opts) => this._.auth.get(...opts);
this.clone = (...opts) => this._.clone.get(...opts);
this.server = (...opts) => this._.server.get(...opts);
this.bbox = (...opts) => this._.bbox.get(...opts);
this.listDeltas = (...opts) => this._.deltas.list(...opts);
this.getDelta = (...opts) => this._.deltas.get(...opts);
this.listBounds = (...opts) => this._.bounds.list(...opts);
this.setBound = (...opts) => this._.bounds.set(...opts);
this.getBound = (...opts) => this._.bounds.get(...opts);
this.getBoundMeta = (...opts) => this._.bounds.meta(...opts);
this.register = (...opts) => this._.user.register(...opts);
this.schema = (...opts) => this._.schema.get(...opts);
this.import = (...opts) => this._.import.multi(...opts);
this.revert = (...opts) => this._.revert.revert(...opts);
this.getFeatureHistory = (...opts) => this._.feature.history(...opts);
this.getFeatureKey = (...opts) => this._.feature.key(...opts);
this.getFeature = (...opts) => this._.feature.get(...opts);
}
}
module.exports = Hecate;
// Run in CLI mode
if (require.main === module) {
const argv = require('minimist')(process.argv, {
boolean: ['help', 'version'],
alias: {
version: 'v',
help: '?'
}
});
if (argv.version) {
console.error('hecate-cli@' + settings.version);
process.exit(0);
} else if (!argv._[2] || (!argv._[2] && argv.help) || argv._[2] === 'help') {
console.error('');
console.error('usage: cli.js <command> [--version] [--help]');
console.error('');
console.error('note: the --script flag can be applied to any mode to disable prompts');
console.error(' when used the user is responsible for making sure they have all the');
console.error(' correct flags');
console.error('');
console.error('<command>');
console.error(' help Displays this message');
console.error(' user [--help] User Management');
console.error(' import [--help] Import data into the server');
console.error(' feature [--help] Download individual features & their history');
console.error(' schema [--help] Obtain the JSON schema for a given server');
console.error(' auth [--help] Obtain the JSON Auth document');
console.error(' bbox [--help] Download data via bbox from a given server');
console.error(' clone [--help] Download the complete server dataset');
console.error(' revert [--help] Revert data from an specified delta');
console.error('');
console.error('<options>');
console.error(' --version Print the current version of the CLI');
console.error(' --help Print a help message');
console.error();
process.exit(0);
}
const command = (err, hecate) => {
if (err) throw err;
const command = argv._[2];
const subcommand = argv._[3];
if (command && !hecate._[command]) {
console.error();
console.error(`"${command}" command not found!`);
console.error();
process.exit(1);
} else if (command && subcommand && !hecate._[command][subcommand]) {
console.error();
console.error(`"${command} ${subcommand}" command not found!`);
console.error();
process.exit(1);
} else if (argv.help || !subcommand) {
return hecate._[command].help();
}
if (!argv.script) {
prompt.message = '$';
prompt.start({
stdout: process.stderr
});
prompt.get([{
name: 'url',
message: 'URL to connect to local or remote Hecate instance. Be sure to include the protocol and port number for local instances, e.g. \'http://localhost:8000\'',
type: 'string',
required: 'true',
default: hecate.url
}], (err, res) => {
if (err) throw err;
hecate.url = new URL(res.url).toString();
argv.cli = true;
// if a custom auth policy hasn't been passed
if (!hecate.auth_rules) {
// fetch auth
hecate.auth({}, (err, auth_rules) => {
// if requesting auth returns a 401
if (err && err.message === '401: Unauthorized') {
// if username and password isn't set, prompt for it
if (!hecate.user) {
prompt.get(auth(hecate.user), (err, res) => {
if (err) throw err;
hecate.user = {
username: res.hecate_username,
password: res.hecate_password
};
// request auth again
hecate.auth({}, (err, auth_rules) => {
if (err) throw err;
hecate.auth_rules = auth_rules;
return run();
});
});
} else {
return run();
}
} else {
hecate.auth_rules = auth_rules;
return run();
}
});
} else return run();
});
} else {
return run();
}
/**
* Once Hecate instance is instantiated, run the requested command
*
* @private
* @returns {undefined}
*/
function run() {
if (!subcommand) {
hecate[command](argv);
} else {
hecate._[command][subcommand](argv);
}
}
};
command(null, new Hecate(argv));
}