-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
121 lines (107 loc) · 3 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
'use strict';
const Promise = require('bluebird');
const yargs = require('yargs');
const Client = require('./lib/client');
const REQUEST_METHODS = Object.create(null);
REQUEST_METHODS.makeRequest = '{"path": "STRING"}';
REQUEST_METHODS.getLabels = '{}';
REQUEST_METHODS.getLabel = '{"labelId": "STRING"}';
REQUEST_METHODS.getChildLabels = '{"labelId": "STRING"}';
REQUEST_METHODS.getAssetsByLabel = '{"labelId": "STRING"}';
REQUEST_METHODS.getAsset = '{"assetId": "STRING"}';
REQUEST_METHODS.getAssetStreams = '{"assetId": "STRING"}';
REQUEST_METHODS.getAssetMetadata = '{"assetId": "STRING"}';
REQUEST_METHODS.getSimilarRelated = '{"embedCode": "STRING"}';
REQUEST_METHODS.getPopularRelated = '{}';
REQUEST_METHODS.getTrendingRelated = '{}';
exports.main = function () {
const args = yargs
.usage('Usage: $0 <command> [options]')
.command('req', 'Make an Ooyala client request', {
method: {
alias: 'm',
default: 'makeRequest',
describe: 'Use the "list" command to see available methods'
},
args: {
alias: 'a',
describe: 'Arguments object as a JSON string',
demand: true
},
apiKey: {
describe: 'Defaults to env var BACKLOT_API_KEY'
},
secretKey: {
describe: 'Defaults to env var BACKLOT_SECRET_KEY'
},
baseUrl: {
describe: 'Base URL to use',
default: 'http://api.ooyala.com'
}
})
.command('list', 'List Ooyala client methods')
.help();
const argv = args.argv;
const command = argv._[0];
switch (command) {
case 'list':
return listCommand();
case 'req':
return requestCommand({
baseUrl: argv.baseUrl,
apiKey: argv.apiKey || process.env.BACKLOT_API_KEY,
secretKey: argv.secretKey || process.env.BACKLOT_SECRET_KEY,
method: argv.method,
args: argv.args
});
default:
console.error('A command argument is required.');
console.error('Use the --help flag to print out help.');
return Promise.resolve(null);
}
};
function listCommand() {
console.log('Request methods:');
console.log('');
Object.getOwnPropertyNames(Client.prototype).forEach(key => {
if (REQUEST_METHODS[key]) {
console.log(' %s --args %s', key, REQUEST_METHODS[key]);
}
});
return Promise.resolve(null);
}
function requestCommand(args) {
const apiKey = args.apiKey;
const secretKey = args.secretKey;
const baseUrl = args.baseUrl;
const method = args.method;
if (!baseUrl) {
console.error('An baseUrl is required (--baseUrl)');
return Promise.resolve(null);
}
if (!apiKey) {
console.error('An apiKey is required (BACKLOT_API_KEY)');
return Promise.resolve(null);
}
if (!secretKey) {
console.error('A secretKey is required (BACKLOT_SECRET_KEY)');
return Promise.resolve(null);
}
let params;
try {
params = JSON.parse(args.args);
} catch (err) {
console.error('--args JSON parsing error:');
console.error(err.message);
return Promise.resolve(null);
}
const client = new Client({
baseUrl,
apiKey,
secretKey
});
return client[method](params).then(res => {
console.log(JSON.stringify(res, null, 2));
return null;
});
}