-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodd-cli.js
executable file
·165 lines (148 loc) · 3.56 KB
/
odd-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
#!/usr/bin/env node
'use strict';
const winston = require('winston');
const yargs = require('yargs');
const Application = require('./lib/application');
const apiToken = require('./commands/api-token');
const loadDocuments = require('./commands/load-documents');
const updateProperty = require('./commands/update-property');
exports.main = function () {
const LOG_LEVEL = process.env.ODD_LOG_LEVEL || 'info';
const BASE_URL = process.env.ODD_BASE_URL;
const parser = yargs
.command(
'api-token',
'Generate an API token for a user', {
username: {
describe: 'The username to own the token',
demand: true,
alias: 'u'
},
password: {
describe: 'The the password for the user',
demand: true,
alias: 'p'
}
}
)
.command(
'load',
'Recursively load a directory of JSON documents to your channel', {
channel: {
describe: 'Your channel ID',
demand: true,
alias: 'c'
},
username: {
describe: 'Your username',
demand: true,
alias: 'u'
},
password: {
describe: 'Your password',
demand: true,
alias: 'p'
},
source: {
describe: 'Path to the source file or directory of files',
demand: true,
alias: 's'
}
}
)
.command(
'update-property',
'Create or update a property using a source directory', {
source: {
describe: 'Path to the source directory',
demand: true,
alias: 's'
}
}
)
.help();
const argv = parser.argv;
const command = argv._[0];
if (!BASE_URL) {
console.error(`The ODD_BASE_URL env variable is required. Ex: https://api.oddnetworks.com/api/v1`);
process.exit(1);
}
winston.level = LOG_LEVEL;
const app = Application.create({
log: winston,
baseUrl: BASE_URL
});
if (!command) {
console.error('A command must be specified\n');
parser.showHelp();
process.exit(1);
}
function printErrorAndExit(message) {
console.error(`${message}\n`);
parser.showHelp();
process.exit(1);
}
function reportError(err) {
switch (err.code) {
case 'VALIDATION_ERROR':
err.errors.forEach(err => {
app.log.error(`Validation Error: ${err.detail}`);
});
break;
case 'AUTHENTICATION_ERROR':
app.log.error(`Authentication Error: ${err.detail}`);
break;
default:
return Promise.reject(err);
}
return null;
}
switch (command) {
case 'api-token':
if (!argv.username) {
printErrorAndExit('A username is required');
} else if (argv.password) {
execApiToken(app, argv).catch(reportError);
} else {
printErrorAndExit('A password is required');
}
break;
case 'load':
if (!argv.username) {
printErrorAndExit('A username is required');
} else if (!argv.password) {
printErrorAndExit('A password is required');
} else if (!argv.channel) {
printErrorAndExit('A channel is required');
} else if (argv.source) {
execLoad(app, argv).catch(reportError);
} else {
printErrorAndExit('A source is required');
}
break;
case 'update-property':
if (argv.source) {
execUpdateProperty(app, {source: argv.source}).catch(reportError);
} else {
printErrorAndExit('A source path is required');
}
break;
default:
console.error(`"${command}" is not a valid command\n`);
parser.showHelp();
}
};
function execApiToken(app, args) {
return apiToken.main(app, args).then(jwt => {
console.log(jwt.token);
});
}
function execLoad(app, args) {
return loadDocuments.main(app, args);
}
function execUpdateProperty(app, args) {
return updateProperty.main(app, args);
}
if (require.main === module) {
exports.main();
}