1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | const pkg = require('../package.json');
|
5 | const co = require('bluebird').coroutine;
|
6 | const path = require('path');
|
7 | const program = require('commander');
|
8 | const nconf = require('nconf');
|
9 | const ManagementClient = require('auth0').ManagementClient;
|
10 | const deploy = require('../lib/deploy');
|
11 | const token = require('../lib/token');
|
12 |
|
13 | const fail = (message) => {
|
14 | console.error(message);
|
15 | process.exit(1);
|
16 | };
|
17 |
|
18 | const hasToken = options => options.token;
|
19 | const hasClient = options => options.clientId && options.clientSecret;
|
20 |
|
21 | const checkRequired = options => {
|
22 | if (!options.auth0Domain) {
|
23 | fail('domain is required');
|
24 | }
|
25 | if (!hasToken(options) && !hasClient(options)) {
|
26 | fail('need to specify one of token or (client-id, client-secret)');
|
27 | }
|
28 | };
|
29 |
|
30 | const getClientInfo = (options) => {
|
31 | return {
|
32 | id: options.clientId,
|
33 | secret: options.clientSecret
|
34 | };
|
35 | };
|
36 |
|
37 | const AUTH0_DOMAIN = 'AUTH0_DOMAIN';
|
38 |
|
39 | const run = (type, name, options) => {
|
40 | return co(function*() {
|
41 |
|
42 | const defaults = {};
|
43 | defaults[AUTH0_DOMAIN] = options.auth0Domain;
|
44 |
|
45 |
|
46 | nconf.argv().env().defaults(defaults);
|
47 |
|
48 | const authToken = (hasToken(options)) ? options.token : yield token.get(options.auth0Domain,
|
49 | getClientInfo(options));
|
50 | const client = new ManagementClient({
|
51 | token: authToken,
|
52 | domain: options.auth0Domain
|
53 | });
|
54 |
|
55 | const workingDir = options.workingDir ? path.resolve(options.workingDir) : process.cwd();
|
56 |
|
57 | if (name) {
|
58 | yield deploy.single(client, type, workingDir, name, nconf);
|
59 | } else {
|
60 | yield deploy.all(client, type, workingDir, nconf);
|
61 | }
|
62 |
|
63 | })().catch(err => {
|
64 | fail(err.message);
|
65 | });
|
66 | };
|
67 |
|
68 | const setupProgram = (program, type, description) => {
|
69 | program
|
70 | .command(`${type} [name]`)
|
71 | .allowUnknownOption(true)
|
72 | .description(description)
|
73 | .action((name) => {
|
74 | checkRequired(program);
|
75 | run(type, name, program);
|
76 | });
|
77 | };
|
78 |
|
79 | program
|
80 | .version(pkg.version)
|
81 | .allowUnknownOption(true)
|
82 | .option('-t, --token <token>', 'Auth0 Management API token')
|
83 | .option('-c, --client-id <id>', 'Auth0 Client ID')
|
84 | .option('-s, --client-secret <secret>', 'Auth0 Client Secret')
|
85 | .option('-d, --auth0-domain <domain>', 'Auth0 Domain')
|
86 | .option('-w, --working-dir <workingdir>', 'working directory for auth0 components');
|
87 |
|
88 | setupProgram(program, 'resource', 'Create resource servers');
|
89 | setupProgram(program, 'connection', 'Create connections');
|
90 | setupProgram(program, 'client', 'Create clients');
|
91 | setupProgram(program, 'rule', 'Create rule');
|
92 |
|
93 | program.parse(process.argv);
|