|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | const { program } = require('commander'); |
| 4 | +const ServerError = require('../lib//ServerError'); |
4 | 5 | const logger = require('../lib/logger'); |
5 | | -const validate = require('../lib/validators'); |
6 | | -const Portal = require('../lib/portal'); |
7 | | -const waitForStatus = require('../lib/data/waitForStatus'); |
8 | | -const { readPassword } = require('../lib/utils/password'); |
9 | | -const { storeEnvironment, deviceAuthorizationFlow } = require('../lib/environments'); |
10 | | -const ServerError = require('../lib/ServerError'); |
11 | | - |
12 | | -const saveToken = (settings, token) => { |
13 | | - storeEnvironment(Object.assign(settings, { token: token })); |
14 | | - logger.Success(`Environment ${settings.url} as ${settings.environment} has been added successfuly.`); |
15 | | -}; |
16 | | - |
17 | | -const help = () => { |
18 | | - program.outputHelp(); |
19 | | - process.exit(1); |
20 | | -} |
21 | | - |
22 | | -const checkParams = params => { |
23 | | - // validate.existence({ argumentValue: params.email, argumentName: 'email', fail: help }); |
24 | | - if (params.email) validate.email(params.email); |
25 | | - |
26 | | - validate.existence({ argumentValue: program.args[0], argumentName: 'environment', fail: help }); |
27 | | - |
28 | | - validate.existence({ argumentValue: params.url, argumentName: 'URL', fail: help }); |
29 | | - if (params.url.slice(-1) != '/') { |
30 | | - params.url = params.url + '/'; |
31 | | - } |
32 | | - validate.url(params.url); |
33 | | -}; |
34 | | - |
35 | | - |
36 | | -const login = async (email, password, url) => { |
37 | | - return Portal.login(email, password, url) |
38 | | - .then(response => { |
39 | | - if (response) return Promise.resolve(response[0].token); |
40 | | - }) |
41 | | -} |
| 6 | +const addEnv = require('../lib/envs/add') |
42 | 7 |
|
| 8 | +program.showHelpAfterError(); |
43 | 9 | program |
44 | 10 | .name('pos-cli env add') |
45 | | - .arguments('[environment]', 'name of environment. Example: staging') |
| 11 | + .arguments('<environment>', 'name of environment. Example: staging') |
46 | 12 | .option('--email <email>', 'Partner Portal account email. Example: [email protected]') |
47 | | - .option('--url <url>', 'marketplace url. Example: https://example.com') |
| 13 | + .requiredOption('--url <url>', 'marketplace url. Example: https://example.com') |
| 14 | + .option('--partner-portal-url <partnerPortalUrl>', 'Partner Partner URL', 'https://partners.platformos.com') |
48 | 15 | .option( |
49 | 16 | '--token <token>', |
50 | 17 | 'if you have a token you can add it directly to pos-cli configuration without connecting to portal' |
51 | 18 | ) |
52 | 19 | .action(async (environment, params) => { |
53 | 20 | try { |
54 | | - checkParams(params); |
55 | | - const settings = { url: params.url, environment: environment, email: params.email }; |
56 | | - |
57 | | - if (params.token) { |
58 | | - token = params.token; |
59 | | - } else if (!params.email){ |
60 | | - token = await deviceAuthorizationFlow(params.url); |
61 | | - } else { |
62 | | - logger.Info( |
63 | | - `Please make sure that you have a permission to deploy. \n You can verify it here: ${Portal.HOST}/me/permissions`, |
64 | | - { hideTimestamp: true } |
65 | | - ); |
66 | | - |
67 | | - const password = await readPassword(); |
68 | | - logger.Info(`Asking ${Portal.HOST} for access token...`); |
69 | | - |
70 | | - token = await login(params.email, password, params.url); |
71 | | - } |
72 | | - |
73 | | - if (token) saveToken(settings, token); |
| 21 | + await addEnv(environment, params); |
74 | 22 | } catch (e) { |
75 | 23 | if (ServerError.isNetworkError(e)) |
76 | 24 | ServerError.handler(e) |
77 | 25 | else |
78 | | - logger.Error('Error'); |
79 | | - process.exit(1); |
| 26 | + logger.Error(e); |
80 | 27 | } |
81 | 28 | }); |
82 | 29 |
|
|
0 commit comments