|
1 | 1 | #!/usr/bin/env node |
2 | | -const path = require('path'); |
3 | | -const fs = require('fs'); |
4 | | -const program = require('commander'); |
5 | | -const chalk = require('chalk'); |
6 | | -const fetch = require('node-fetch'); |
7 | | -const configuration = require('../lib/settings')(); |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs"); |
| 4 | +const program = require("commander"); |
| 5 | +const chalk = require("chalk"); |
| 6 | +const fetch = require("node-fetch"); |
| 7 | +const configuration = require("../lib/settings")(); |
8 | 8 |
|
9 | 9 | program.parse(process.argv); |
10 | 10 |
|
11 | | -if(!program.args.length) list(); |
| 11 | +if (!program.args.length) list(); |
12 | 12 |
|
13 | | -function list(){ |
| 13 | +function list() { |
14 | 14 | configuration |
15 | | - .load() |
16 | | - .then(settings => { console.log(`Deployments for ${chalk.green.underline(settings.username || '-')} on ${chalk.green.underline(settings.url || '-')}\n`); return settings;}) |
17 | | - .then(settings => Boolean(!(settings.token && settings.username)) ? Promise.reject(new Error('Sign in failed')) : settings) |
18 | | - .then(settings => fetch(`${settings.url}/deploys`, { headers: { Authorization: `Bearer ${settings.token}`, } }) |
19 | | - .then(response => response.status >= 400 ? Promise.reject(new Error(`Response error: ${response.statusText}`)) : response) |
20 | | - .then(response => response.json())) |
21 | | - .then(data => Boolean(data.message) ? Promise.reject(new Error(data.message)) : data) |
22 | | - .then(data => { |
23 | | - console.log(`${chalk.bold(pad(7, 'TYPE', ' '))} | ${chalk.bold(pad(5, 'NUM #', ' '))} | ${chalk.bold(pad(9, 'ID/NAME', ' '))} | ${chalk.bold(pad(45, 'URL', ' '))} | ${chalk.bold(pad(45, 'STATEFUL', ' '))}`); |
24 | | - return data; |
25 | | - }) |
26 | | - .then(data => (data || []).map(x => `${chalk.gray(pad(7, x.serviceType.toUpperCase(), ' '))} | ${chalk.gray(pad(5, x.serviceInstances, ' '))} | ${chalk.green(pad(9, x.serviceName, ' '))} | ${chalk.bold(pad(45, `https://${x.serviceUrl || '-'}`, ' '))} | ${chalk.gray(pad(45, x.serviceStateful || 'N/A', ' '))}`)) |
27 | | - .then(data => { |
28 | | - if(data.length > 0) return data.map(x => console.log(x)); |
29 | | - console.log(chalk.gray('Nothing deployed')); |
30 | | - }) |
31 | | - .catch(err => { |
32 | | - if(err.message === 'canceled') { |
33 | | - console.log(chalk.yellow('\nAborted operation.')); |
34 | | - return process.exit(0); |
35 | | - } |
36 | | - |
37 | | - if(err.message === 'Sign in failed') { |
38 | | - console.error(chalk.red(`\nCredentials not found. Use ${chalk.bold('dropstack login <url>')} to verifiy your credentials.`)); |
39 | | - process.exit(1); |
40 | | - } |
| 15 | + .load() |
| 16 | + .then(settings => { |
| 17 | + console.log( |
| 18 | + `Deployments for ${chalk.green.underline( |
| 19 | + settings.username || "-" |
| 20 | + )} on ${chalk.green.underline(settings.url || "-")}\n` |
| 21 | + ); |
| 22 | + return settings; |
| 23 | + }) |
| 24 | + .then( |
| 25 | + settings => |
| 26 | + Boolean(!(settings.token && settings.username)) |
| 27 | + ? Promise.reject(new Error("Sign in failed")) |
| 28 | + : settings |
| 29 | + ) |
| 30 | + .then(settings => |
| 31 | + fetch(`${settings.url}/deploys`, { |
| 32 | + headers: { Authorization: `Bearer ${settings.token}` } |
| 33 | + }) |
| 34 | + .then( |
| 35 | + response => |
| 36 | + response.status >= 400 |
| 37 | + ? Promise.reject( |
| 38 | + new Error(`Response error: ${response.statusText}`) |
| 39 | + ) |
| 40 | + : response |
| 41 | + ) |
| 42 | + .then(response => response.json()) |
| 43 | + ) |
| 44 | + .then( |
| 45 | + data => |
| 46 | + Boolean(data.message) ? Promise.reject(new Error(data.message)) : data |
| 47 | + ) |
| 48 | + .then(data => { |
| 49 | + console.log( |
| 50 | + `${chalk.bold(pad(7, "TYPE", " "))} | ${chalk.bold( |
| 51 | + pad(5, "NUM #", " ") |
| 52 | + )} | ${chalk.bold(pad(9, "ID/NAME", " "))} | ${chalk.bold( |
| 53 | + pad(45, "URL", " ") |
| 54 | + )}` |
| 55 | + ); |
| 56 | + return data; |
| 57 | + }) |
| 58 | + .then(data => |
| 59 | + (data || []).map( |
| 60 | + x => |
| 61 | + `${chalk.gray( |
| 62 | + pad(7, x.serviceType.toUpperCase(), " ") |
| 63 | + )} | ${chalk.gray(pad(5, x.serviceInstances, " "))} | ${chalk.green( |
| 64 | + pad(9, x.serviceName, " ") |
| 65 | + )} | ${chalk.bold(pad(45, `https://${x.serviceUrl || "-"}`, " "))}` |
| 66 | + ) |
| 67 | + ) |
| 68 | + .then(data => { |
| 69 | + if (data.length > 0) return data.map(x => console.log(x)); |
| 70 | + console.log(chalk.gray("Nothing deployed")); |
| 71 | + }) |
| 72 | + .catch(err => { |
| 73 | + if (err.message === "canceled") { |
| 74 | + console.log(chalk.yellow("\nAborted operation.")); |
| 75 | + return process.exit(0); |
| 76 | + } |
41 | 77 |
|
42 | | - if(err.message === 'Unauthorized') { |
43 | | - console.error(chalk.red(`\nUnauthorized. Use ${chalk.bold('dropstack login <url>')} to verifiy your credentials.`)); |
44 | | - process.exit(1); |
45 | | - } |
| 78 | + if (err.message === "Sign in failed") { |
| 79 | + console.error( |
| 80 | + chalk.red( |
| 81 | + `\nCredentials not found. Use ${chalk.bold( |
| 82 | + "dropstack login <url>" |
| 83 | + )} to verifiy your credentials.` |
| 84 | + ) |
| 85 | + ); |
| 86 | + process.exit(1); |
| 87 | + } |
46 | 88 |
|
47 | | - if(err.message === 'Not found') { |
48 | | - console.error(chalk.red(`\nServer communication error occurred! Retry later please.\nError: ${chalk.gray(err.message)}`)); |
49 | | - process.exit(1); |
50 | | - } |
| 89 | + if (err.message === "Unauthorized") { |
| 90 | + console.error( |
| 91 | + chalk.red( |
| 92 | + `\nUnauthorized. Use ${chalk.bold( |
| 93 | + "dropstack login <url>" |
| 94 | + )} to verifiy your credentials.` |
| 95 | + ) |
| 96 | + ); |
| 97 | + process.exit(1); |
| 98 | + } |
51 | 99 |
|
52 | | - console.error(chalk.red(`\nAn unexpected error occurred!\nError: ${chalk.gray(err.message)}`)); |
53 | | - process.exit(1); |
54 | | - }); |
| 100 | + if (err.message === "Not found") { |
| 101 | + console.error( |
| 102 | + chalk.red( |
| 103 | + `\nServer communication error occurred! Retry later please.\nError: ${chalk.gray( |
| 104 | + err.message |
| 105 | + )}` |
| 106 | + ) |
| 107 | + ); |
| 108 | + process.exit(1); |
| 109 | + } |
| 110 | + |
| 111 | + console.error( |
| 112 | + chalk.red( |
| 113 | + `\nAn unexpected error occurred!\nError: ${chalk.gray(err.message)}` |
| 114 | + ) |
| 115 | + ); |
| 116 | + process.exit(1); |
| 117 | + }); |
55 | 118 | } |
56 | 119 |
|
57 | 120 | function pad(width, string, padding) { |
58 | | - if(!string) return; |
59 | | - return (width <= string.length) ? string : pad(width, string + padding, padding) |
| 121 | + if (!string) return; |
| 122 | + return width <= string.length |
| 123 | + ? string |
| 124 | + : pad(width, string + padding, padding); |
60 | 125 | } |
0 commit comments