|
1 | 1 | // npm packages |
2 | 2 | const got = require('got'); |
3 | 3 | const chalk = require('chalk'); |
| 4 | +const inquirer = require('inquirer'); |
4 | 5 |
|
5 | 6 | // our packages |
6 | 7 | const {userConfig, isLoggedIn, logout} = require('../config'); |
7 | 8 |
|
8 | | -exports.command = ['token']; |
9 | | -exports.describe = 'generate new deployment token'; |
10 | | -exports.builder = {}; |
11 | | -exports.handler = async () => { |
| 9 | +exports.command = ['token [cmd]']; |
| 10 | +exports.describe = 'generate, list or remove deployment token'; |
| 11 | +exports.builder = { |
| 12 | + cmd: { |
| 13 | + default: '', |
| 14 | + description: 'command to execute [ls | rm]', |
| 15 | + }, |
| 16 | +}; |
| 17 | +exports.handler = async args => { |
12 | 18 | if (!isLoggedIn()) { |
13 | 19 | return; |
14 | 20 | } |
15 | 21 |
|
16 | | - console.log(chalk.bold('Generating new deployment token for:'), userConfig.endpoint); |
17 | | - |
18 | 22 | // services request url |
19 | 23 | const remoteUrl = `${userConfig.endpoint}/deployToken`; |
| 24 | + // get command |
| 25 | + const {cmd} = args; |
| 26 | + // if remove or ls - fetch tokens from remote, then do work |
| 27 | + if (cmd === 'ls' || cmd === 'rm') { |
| 28 | + console.log( |
| 29 | + chalk.bold(`${cmd === 'ls' ? 'Listing' : 'Removing'} deployment token${cmd === 'ls' ? 's' : ''} for:`), |
| 30 | + userConfig.endpoint |
| 31 | + ); |
| 32 | + |
| 33 | + // get tokens from server |
| 34 | + // construct shared request params |
| 35 | + const options = { |
| 36 | + method: 'GET', |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${userConfig.token}`, |
| 39 | + }, |
| 40 | + json: true, |
| 41 | + }; |
| 42 | + // try sending request |
| 43 | + let tokens = []; |
| 44 | + try { |
| 45 | + const {body} = await got(remoteUrl, options); |
| 46 | + tokens = body.tokens; |
| 47 | + } catch (e) { |
| 48 | + // if authorization is expired/broken/etc |
| 49 | + if (e.statusCode === 401) { |
| 50 | + logout(userConfig); |
| 51 | + console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + console.log(chalk.red('Error getting deployment tokens:'), e.toString()); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (cmd === 'ls') { |
| 60 | + console.log(chalk.bold('Got generated tokens:')); |
| 61 | + console.log(''); |
| 62 | + tokens.map(t => |
| 63 | + console.log(` > ${chalk.green(t.tokenName)} ${chalk.gray(`[${new Date(t.meta.created).toLocaleString()}]`)}`) |
| 64 | + ); |
| 65 | + if (!tokens.length) { |
| 66 | + console.log(' > No deployment tokens available!'); |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const prompts = []; |
| 72 | + prompts.push({ |
| 73 | + type: 'list', |
| 74 | + name: 'rmToken', |
| 75 | + message: 'Choose token to remove:', |
| 76 | + choices: tokens.map(t => t.tokenName), |
| 77 | + }); |
| 78 | + const {rmToken} = await inquirer.prompt(prompts); |
| 79 | + |
| 80 | + // construct shared request params |
| 81 | + const rmOptions = { |
| 82 | + method: 'DELETE', |
| 83 | + headers: { |
| 84 | + Authorization: `Bearer ${userConfig.token}`, |
| 85 | + }, |
| 86 | + json: true, |
| 87 | + body: { |
| 88 | + tokenName: rmToken, |
| 89 | + }, |
| 90 | + }; |
| 91 | + try { |
| 92 | + const {body, statusCode} = await got(remoteUrl, rmOptions); |
| 93 | + if (statusCode !== 204) { |
| 94 | + console.log(chalk.red('Error removing deployment token!'), body.reason || 'Please try again!'); |
| 95 | + return; |
| 96 | + } |
| 97 | + console.log(chalk.green('Deployment token successfully removed!')); |
| 98 | + } catch (e) { |
| 99 | + // if authorization is expired/broken/etc |
| 100 | + if (e.statusCode === 401) { |
| 101 | + logout(userConfig); |
| 102 | + console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + console.log(chalk.red('Error getting deployment tokens:'), e.toString()); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + console.log(chalk.bold('Generating new deployment token for:'), userConfig.endpoint); |
| 114 | + |
| 115 | + // ask for token name |
| 116 | + const prompts = []; |
| 117 | + prompts.push({ |
| 118 | + type: 'input', |
| 119 | + name: 'tokenName', |
| 120 | + message: 'Token name:', |
| 121 | + validate: input => input && input.length > 0, |
| 122 | + filter: input => input.trim(), |
| 123 | + }); |
| 124 | + const {tokenName} = await inquirer.prompt(prompts); |
| 125 | + |
20 | 126 | // construct shared request params |
21 | 127 | const options = { |
| 128 | + method: 'POST', |
22 | 129 | headers: { |
23 | 130 | Authorization: `Bearer ${userConfig.token}`, |
24 | 131 | }, |
25 | 132 | json: true, |
| 133 | + body: { |
| 134 | + tokenName, |
| 135 | + }, |
26 | 136 | }; |
27 | 137 | // try sending request |
28 | 138 | try { |
|
0 commit comments