UNPKG

@sudolabs-io/aws-ssm-cli

Version:

Command line tool for AWS Systems Manager Parameter Store

123 lines (122 loc) 4.81 kB
#!/usr/bin/env node "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const yargs_1 = __importDefault(require("yargs/yargs")); const helpers_1 = require("yargs/helpers"); const pull_1 = require("./pull"); const push_1 = require("./push"); const dotenv_1 = require("./dotenv"); const clientOptions = { region: { type: 'string', describe: 'AWS Region', }, 'access-key-id': { type: 'string', describe: 'AWS Access Key ID', }, 'secret-access-key': { type: 'string', describe: 'AWS Secret Access Key', }, }; (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) .command({ command: 'push', describe: 'Push environment variables to AWS SSM Parameter Store', builder: (y) => y.options(Object.assign(Object.assign({}, clientOptions), { prefix: { alias: 'p', type: 'string', describe: 'Push variables with prefix', demandOption: true, }, file: { alias: 'f', type: 'string', describe: 'Dotenv file with variables', coerce: (file) => path_1.default.resolve(process.cwd(), file), demandOption: true, } })), handler: (pushArgs) => __awaiter(void 0, void 0, void 0, function* () { yield (0, push_1.pushParameters)(pushArgs); }), }) .command({ command: 'pull', describe: 'Pull environment variables from AWS SSM Parameter Store', builder: (y) => y.options(Object.assign(Object.assign({}, clientOptions), { prefix: { alias: 'p', type: 'string', describe: 'Pull variables starting with prefix', demandOption: true, }, json: { type: 'boolean', describe: 'Format `pull` output as JSON', }, group: { type: 'string', describe: 'Group environment variables as keys of an object', implies: 'json', } })), handler: (_a) => __awaiter(void 0, void 0, void 0, function* () { var { json, group } = _a, pullArgs = __rest(_a, ["json", "group"]); const parameters = yield (0, pull_1.pullParameters)(pullArgs); if (json) { const jsonParameters = group ? { [group]: parameters } : parameters; console.log(JSON.stringify(jsonParameters)); } else { console.log((0, dotenv_1.toDotenvString)(parameters)); } }), }) .check(({ prefix, file }) => { if (typeof prefix === 'string') { if (!prefix.startsWith('/')) { throw new Error('prefix must start with slash "/"'); } if (!prefix.endsWith('/')) { throw new Error('prefix must end with slash "/"'); } } if (typeof file === 'string' && !fs_1.default.existsSync(file)) { throw new Error(`file ${file} does not exist`); } return true; }) .alias({ h: 'help', v: 'version' }) .group(['help', 'version'], '') .group(Object.keys(clientOptions), 'Client Options:') .example([ [ 'push --prefix="/<project>/<environment>/" --file=".env"', 'Push environment variables stored in .env file with prefix', ], ['pull --prefix="/<project>/<environment>/" --json', 'Pull environment variables by prefix'], ]) .demandCommand() .strictCommands() .strictOptions().argv;