| 23 | } |
| 24 | |
| 25 | run(params, callback) { |
| 26 | |
| 27 | let activeToken = config.get('ACTIVE_LIBRARY_TOKEN'); |
| 28 | |
| 29 | let host = params.flags.h ? params.flags.h[0] : 'https://api.autocode.com'; |
| 30 | let port = params.flags.p && params.flags.p[0]; |
| 31 | |
| 32 | let resource = new APIResource(host, port); |
| 33 | |
| 34 | resource.authorize(config.get('ACCESS_TOKEN')); |
| 35 | resource.request('/v1/library_tokens').index({}, async (err, response) => { |
| 36 | |
| 37 | if (err) { |
| 38 | return callback(err); |
| 39 | } |
| 40 | |
| 41 | let tokens = (response && response.data) || []; |
| 42 | |
| 43 | if (!tokens.length) { |
| 44 | console.log(); |
| 45 | console.log(chalk.bold.red('Oops!')); |
| 46 | console.log(); |
| 47 | console.log(`It doesn't look like you have any Identity Tokens.`); |
| 48 | console.log(`This usually means you've removed them.`); |
| 49 | console.log(); |
| 50 | console.log(`Try typing `); |
| 51 | console.log(`\t${chalk.bold('lib tokens:create')}`); |
| 52 | console.log(); |
| 53 | console.log(`To create a new Identity Token.`); |
| 54 | console.log(); |
| 55 | return callback(new Error('No Identity Tokens.')); |
| 56 | } |
| 57 | |
| 58 | console.log(); |
| 59 | console.log(`Here's a list of your available ${chalk.bold('Identity Tokens')}.`); |
| 60 | console.log(`These are your API keys that provide authentication and access to functions.`); |
| 61 | console.log(); |
| 62 | console.log(`Here you can change your active authentication token, simply choose from the list.`); |
| 63 | console.log(); |
| 64 | |
| 65 | let answers = await inquirer.prompt( |
| 66 | [ |
| 67 | { |
| 68 | name: 'libraryToken', |
| 69 | type: 'list', |
| 70 | pageSize: 100, |
| 71 | message: `Select a Identity Token to use for Authentication`, |
| 72 | choices: tabler( |
| 73 | ['Active', 'User', 'Label', 'Token', 'Valid', 'Created'], |
| 74 | tokens.map(libraryToken => { |
| 75 | return { |
| 76 | 'Active': activeToken === libraryToken.token ? ['(active)', chalk.yellow] : '', |
| 77 | 'User': libraryToken.user.username, |
| 78 | 'Label': libraryToken.label ? |
| 79 | libraryToken.label.length > 36 ? |
| 80 | libraryToken.label.substr(0, 33) + '...' : |
| 81 | libraryToken.label : |
| 82 | '', |