-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
61 lines (55 loc) · 1.5 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
'use strict';
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
const docopt = require('docopt');
const proxy = require('proxy-agent');
const ssm = new AWS.SSM({apiVersion: '2014-11-06'});
const getCurrentParameter = async (name) => {
try {
return await ssm.getParameter({
Name: name,
WithDecryption: true
}).promise();
} catch (err) {
if (err.code === 'ParameterNotFound') {
return null;
} else {
throw err;
}
}
};
module.exports.run = async (argv) => {
const cli = fs.readFileSync(path.join(__dirname, 'cli.txt'), {encoding: 'utf8'});
const input = docopt.docopt(cli, {
version: require('./package.json').version,
argv: argv
});
if ('HTTPS_PROXY' in process.env) {
AWS.config.update({
httpOptions: {agent: proxy(process.env.HTTPS_PROXY)}
});
}
if (input.update === true) {
const name = input['--name'];
const value = input['--value'];
const type = input['--type'];
const currentData = await getCurrentParameter(name);
if (currentData === null || currentData.Parameter.Value !== value || currentData.Parameter.Type !== type) {
const params = {
Name: name,
Type: type,
Value: value,
Overwrite: true
};
if (input['--key-id'] !== null) {
params.KeyId = input['--key-id'];
}
return await ssm.putParameter(params).promise();
} else {
return {
Version: currentData.Parameter.Version
};
}
}
};