-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
134 lines (117 loc) · 3.76 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
'use strict';
const Promise = require('bluebird');
const yargs = require('yargs');
const Client = require('./lib/client');
const REQUEST_METHODS = Object.create(null);
REQUEST_METHODS.makeRequest = '{}';
REQUEST_METHODS.getAccessToken = '{}';
REQUEST_METHODS.getPlaylistCount = '{"query": "OBJECT"}';
REQUEST_METHODS.getPlaylists = '{"query": "OBJECT"}';
REQUEST_METHODS.getPlaylist = '{"playlistId": "STRING"}';
REQUEST_METHODS.getVideosByPlaylist = '{"playlistId": "STRING", "skipScheduleCheck": "BOOLEAN"}';
REQUEST_METHODS.getVideoCountByPlaylist = '{"playlistId": "STRING"}';
REQUEST_METHODS.getVideoCount = '{"query": "OBJECT"}';
REQUEST_METHODS.getVideos = '{"query": "OBJECT", "skipScheduleCheck": "BOOLEAN"}';
REQUEST_METHODS.getVideo = '{"videoId": "STRING", "skipScheduleCheck": "BOOLEAN"}';
REQUEST_METHODS.getVideoSources = '{"videoId": "STRING"}';
const listCommand = () => {
console.log('Request methods:');
console.log('');
Object.getOwnPropertyNames(Client.prototype).forEach(key => {
if (REQUEST_METHODS[key]) {
console.log(`\t${key} --args ${REQUEST_METHODS[key]}`);
}
});
return Promise.resolve(null);
};
const requestCommand = args => {
const clientId = args.clientId;
const clientSecret = args.clientSecret;
const accountId = args.accountId;
const method = args.method;
const concurrentRequestLimit = args.concurrentRequestLimit;
if (!clientId) {
console.error('A clientId is required (--clientId)');
return Promise.resolve(null);
}
if (!clientSecret) {
console.error('A clientSecret is required (--clientSecret)');
return Promise.resolve(null);
}
if (!accountId) {
console.error('An accountId is required (--accountId)');
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({clientId, clientSecret, accountId, concurrentRequestLimit});
const clientMethod = client[method];
if (!clientMethod) {
console.error(`--method ${method} not found`);
return Promise.resolve(null);
}
return clientMethod(params).then(res => {
console.log(JSON.stringify(res, null, 2));
return null;
});
};
exports.main = () => {
const args = yargs
.usage('Usage: $0 <command> [options]')
.command('req', 'Make a vimeo client request', {
method: {
alias: 'm',
default: 'makeRequest',
describe: 'Use the "list" command to see available methods'
},
args: {
alias: 'a',
default: '{}',
describe: 'Arguments object as a JSON string'
},
clientId: {
describe: 'Defaults to env var BRIGHTCOVE_CLIENT_ID',
type: 'string'
},
clientSecret: {
describe: 'Defaults to env var BRIGHTCOVE_CLIENT_SECRET',
type: 'string'
},
accountId: {
describe: 'Defaults to env var BRIGHTCOVE_ACCOUNT_ID',
type: 'string'
},
concurrentRequestLimit: {
alias: 'c',
default: 20,
describe: 'Limits the client to specified concurrent requests'
}
})
.command('list', 'List vimeo client methods')
.help();
const argv = args.argv;
const command = argv._[0];
switch (command) {
case 'list':
return listCommand();
case 'req':
return requestCommand({
clientId: argv.clientId || process.env.BRIGHTCOVE_CLIENT_ID,
clientSecret: argv.clientSecret || process.env.BRIGHTCOVE_CLIENT_SECRET,
accountId: argv.accountId || process.env.BRIGHTCOVE_ACCOUNT_ID,
concurrentRequestLimit: argv.concurrentRequestLimit,
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);
}
};