/
list.ts
166 lines (151 loc) · 4.71 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { TwilioServerlessApiClient } from '@twilio-labs/serverless-api';
import { Argv } from 'yargs';
import { checkConfigForCredentials } from '../checks/check-credentials';
import checkForValidServiceSid from '../checks/check-service-sid';
import { getConfigFromFlags, ListCliFlags, ListConfig } from '../config/list';
import { printListResult } from '../printers/list';
import { getDebugFunction, logger, setLogLevelByName } from '../utils/logger';
import { ExternalCliOptions, sharedCliOptions } from './shared';
import { CliInfo } from './types';
import { getFullCommand } from './utils';
const debug = getDebugFunction('twilio-run:list');
function logError(msg: string) {
logger.error(msg);
}
function handleError(err: Error) {
debug('%O', err);
logError(err.message);
process.exit(1);
}
export async function handler(
flags: ListCliFlags,
externalCliOptions?: ExternalCliOptions
): Promise<void> {
setLogLevelByName(flags.logLevel);
let config: ListConfig;
try {
config = await getConfigFromFlags(flags, externalCliOptions);
} catch (err) {
debug(err);
logError(err.message);
process.exit(1);
return;
}
if (!config) {
logError('Internal Error');
process.exit(1);
return;
}
checkConfigForCredentials(config);
if (config.types !== 'services' && config.types[0] !== 'services') {
const command = getFullCommand(flags);
checkForValidServiceSid(command, config.serviceSid);
}
try {
const client = new TwilioServerlessApiClient(config);
const result = await client.list({ ...config });
printListResult(result, config);
} catch (err) {
handleError(err);
}
}
export const cliInfo: CliInfo = {
argsDefaults: {
types: 'services',
},
options: {
...sharedCliOptions,
'service-name': {
type: 'string',
alias: 'n',
describe:
'Overrides the name of the Serverless project. Default: the name field in your package.json',
},
'project-name': {
type: 'string',
hidden: true,
describe:
'DEPRECATED: Overrides the name of the project. Default: the name field in your package.json',
},
properties: {
type: 'string',
describe:
'Specify the output properties you want to see. Works best on single types',
hidden: true,
},
'extended-output': {
type: 'boolean',
describe: 'Show an extended set of properties on the output',
default: false,
},
cwd: {
type: 'string',
hidden: true,
describe:
'Sets the directory of your existing Serverless project. Defaults to current directory',
},
environment: {
type: 'string',
describe: 'The environment to list variables for',
default: 'dev',
},
'account-sid': {
type: 'string',
alias: 'u',
describe:
'A specific account SID to be used for deployment. Uses fields in .env otherwise',
},
'auth-token': {
type: 'string',
describe:
'Use a specific auth token for deployment. Uses fields from .env otherwise',
},
'service-sid': {
type: 'string',
describe: 'Specific Serverless Service SID to run list for',
},
env: {
type: 'string',
describe:
'Path to .env file for environment variables that should be installed',
},
},
};
function optionBuilder(yargs: Argv<any>): Argv<ListCliFlags> {
if (cliInfo.argsDefaults) {
yargs = Object.keys(cliInfo.argsDefaults).reduce(
(yargs: Argv<any>, name: string) => {
if (cliInfo.argsDefaults && cliInfo.argsDefaults[name]) {
return yargs.default(name, cliInfo.argsDefaults[name]);
}
return yargs;
},
yargs
);
}
yargs = yargs
.example(
'$0 list services',
'Lists all existing services/projects associated with your Twilio Account'
)
.example(
'$0 ls functions,assets --environment=dev --service-name=demo',
'Lists all existing functions & assets associated with the `dev` environment of this service/project'
)
.example(
'$0 ls environments --service-sid=ZSxxxxx --extended-output',
'Outputs all environments for a specific service with extended output for better parsing'
)
.example(
'$0 ls assets,variables,functions --properties=sid,date_updated',
'Only lists the SIDs and date of last update for assets, variables and function'
);
yargs = Object.keys(cliInfo.options).reduce((yargs, name) => {
return yargs.option(name, cliInfo.options[name]);
}, yargs);
return yargs;
}
export const command = ['list [types]', 'ls [types]'];
export const describe =
'List existing services, environments, variables, deployments for your Twilio Serverless Account';
export const builder = optionBuilder;