-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
115 lines (94 loc) · 3.3 KB
/
cli.js
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
import { ok } from 'assert';
import { resolve as resolvePath } from 'path';
import { inspect } from 'util';
import yargs from 'yargs';
import { getAwsAccountId } from '../lib/aws/account.js';
import { Betty } from '../lib/betty.js';
import { Context } from '../lib/context.js';
import { readAppData, writeAppData } from './appdata.js';
import * as logger from './logger.js';
global.logger = logger;
// TODO: add command for generating a file with encrypted hash values (kms)
// TODO: add init command for creating a new project
// providing inline wasn't working with:
// console.dir(obj, { ...options })
// FIXME: remove this workaround to ==^
inspect.defaultOptions.depth = 12;
const loadAccountId = async () => {
const profile = process.env.AWS_PROFILE;
const result = readAppData('aws.json');
if (result && result[profile] && result[profile].accountId) {
return result[profile].accountId;
}
else {
const accountId = await getAwsAccountId();
writeAppData('aws.json', Object.assign({}, result, {
[profile]: Object.assign({}, result[profile], { accountId })
}));
return accountId;
}
};
yargs.middleware(async argv => {
const command = argv._[0];
const { account, profile, region, config, project } = argv;
// if not using cwd we'll chdir to the target just to keep things simple
if (project) {
process.chdir(resolvePath(project));
delete argv.project; // we changed cwd so clean this up so it's not used anywhere instead of cwd
}
const context = new Context({
cwd: process.cwd(),
env: argv.env,
config,
logLevel: argv.logLevel,
awsAccountId: account,
awsProfile: profile,
awsRegion: region,
// this is not like the others. this feels more like a task/command setting
// awsLambdaRole = awsDefaultLambdaExecutionRole,
});
ok(context.awsProfile, 'aws profile required');
// no commands require region any longer
// ok(context.awsRegion, 'aws region required');
// it's best to use these when working with aws-sdk
process.env.AWS_PROFILE = context.awsProfile;
process.env.AWS_REGION = context.awsRegion;
context.awsAccountId = await loadAccountId();
// this must come before loadResource because it may depend on it
global.betty = context;
context.loadResource();
argv.betty = new Betty(context);
});
yargs.strict(true);
yargs.option('config', {
alias: 'c',
describe: 'The path to a config file to use. If not provided an attempt will be made to load either betty.js or betty.json from the cwd',
});
yargs.option('loglevel', {
default: process.env.LOG_LEVEL || (process.env.DEBUG && 'debug'),
});
// defaults to inferring via aws api
yargs.option('account');
yargs.option('profile', {
default: process.env.AWS_PROFILE,
});
yargs.option('region', {
default: process.env.AWS_REGION,
});
yargs.option('project', {
describe: 'The project directory to target if not cwd',
default: process.cwd(),
});
yargs.option('interactive', {
boolean: true,
default: true,
describe: 'When disabled, will never request input and will instead exit with error',
});
yargs.option('env', {
deprecated: true,
describe: 'For use with v1.x resource.js files requiring a global.betty.env',
default: process.env.betty_env,
});
yargs.commandDir('./commands').demandCommand();
yargs.help();
yargs.parse();