From bb74c15301291d581cbcc4615f13332aec24f0d2 Mon Sep 17 00:00:00 2001 From: kyle-ruan Date: Mon, 6 Sep 2021 19:13:48 +1000 Subject: [PATCH] feat: export config only values --- README.md | 16 ++++---- bin/oprah | 6 ++- lib/commands/import-export/make-export.js | 11 +++-- .../import-export/make-export.test.js | 41 +++++++++++++++---- package.json | 4 +- 5 files changed, 55 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index dc1d606..674c968 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ [![NPM Status][npm-image]][npm-url] [![dependencies Status][dependencies-image]][dependencies-url] - # 🐝 Oprah Node module to push configuration and encrypted secrets to AWS. @@ -51,7 +50,6 @@ $ oprah run --stage --interactive Following is the configuration file will all possible options: - ``` service: oprah-service provider: ssm # Only supports ssm for now. @@ -181,6 +179,8 @@ Options: -t, --target [target] The output target, available options are json|env (default:json) + -C, --config-only [configOnly] Only export `config` section + -h, --help display help for command ``` @@ -203,9 +203,9 @@ Feel free to use the code, it's released using the MIT license. [github-actions-image]: https://github.com/acloudguru/oprah/actions/workflows/publish.yml/badge.svg [github-actions-url]: https://github.com/ACloudGuru/oprah/actions/workflows/publish.yml -[dependencies-image]:https://david-dm.org/ACloudGuru/oprah/status.svg -[dependencies-url]:https://david-dm.org/ACloudGuru/oprah -[npm-image]:https://img.shields.io/npm/v/oprah.svg -[npm-url]:https://www.npmjs.com/package/oprah -[codacy-image]:https://api.codacy.com/project/badge/Grade/6464d14b26214357ba838d2cdbdfcb8e -[codacy-url]:https://www.codacy.com/app/subash.adhikari/oprah?utm_source=github.com&utm_medium=referral&utm_content=ACloudGuru/oprah&utm_campaign=Badge_Grade +[dependencies-image]: https://david-dm.org/ACloudGuru/oprah/status.svg +[dependencies-url]: https://david-dm.org/ACloudGuru/oprah +[npm-image]: https://img.shields.io/npm/v/oprah.svg +[npm-url]: https://www.npmjs.com/package/oprah +[codacy-image]: https://api.codacy.com/project/badge/Grade/6464d14b26214357ba838d2cdbdfcb8e +[codacy-url]: https://www.codacy.com/app/subash.adhikari/oprah?utm_source=github.com&utm_medium=referral&utm_content=ACloudGuru/oprah&utm_campaign=Badge_Grade diff --git a/bin/oprah b/bin/oprah index 89a0892..5ba25d9 100755 --- a/bin/oprah +++ b/bin/oprah @@ -71,11 +71,13 @@ program '-t, --target [target]', 'The output target, available options are json|env (default:json)' ) - .action(({ path, target }) => { + .option('-C, --config-only [configOnly]', 'Only export configs') + .action(({ path, target, configOnly = false }) => { const { stage, config } = program.opts(); oprahPromise = makeOprah({ stage, config }).export( path || (target === 'env' ? '.env_oprah' : '/tmp/oprah-exports.json'), - target || 'json' + target || 'json', + configOnly ); }); diff --git a/lib/commands/import-export/make-export.js b/lib/commands/import-export/make-export.js index 7bbc7a2..30dae02 100644 --- a/lib/commands/import-export/make-export.js +++ b/lib/commands/import-export/make-export.js @@ -6,15 +6,18 @@ const { log } = require('../../utils/logger'); const makeExport = ({ settingsService, parameterStore }) => async ( filePath, - target + target, + configOnly = false ) => { const settings = await settingsService.getSettings(); log(chalk.white(`Getting parameters..`)); - const secretsPromise = parameterStore.getParameters({ - parameterNames: get(settings, 'secretParameters') - }); + const secretsPromise = configOnly + ? Promise.resolve({}) + : parameterStore.getParameters({ + parameterNames: get(settings, 'secretParameters') + }); const configsPromise = parameterStore.getParameters({ parameterNames: get(settings, 'configParameters') diff --git a/lib/commands/import-export/make-export.test.js b/lib/commands/import-export/make-export.test.js index b8a999b..bb63270 100644 --- a/lib/commands/import-export/make-export.test.js +++ b/lib/commands/import-export/make-export.test.js @@ -28,7 +28,13 @@ describe('export', () => { configParameters: ['path/to/config1'] }) ); + }); + + afterEach(() => { + fs.writeFileSync.mockClear(); + }); + it('should write JSON version to the specified file path', () => { getParametersMock .mockImplementationOnce(() => Promise.resolve({ @@ -40,13 +46,7 @@ describe('export', () => { 'path/to/config1': 'config1Value' }) ); - }); - - afterEach(() => { - fs.writeFileSync.mockClear(); - }); - it('should write JSON version to the specified file path', () => { const filePath = './test.json'; return exportFunction(filePath).then(() => { @@ -69,7 +69,17 @@ describe('export', () => { it('should write ENV version to the specified file path', () => { const filePath = './.env.test'; const target = 'env'; - + getParametersMock + .mockImplementationOnce(() => + Promise.resolve({ + 'path/to/secret1': 'secret1Value' + }) + ) + .mockImplementationOnce(() => + Promise.resolve({ + 'path/to/config1': 'config1Value' + }) + ); return exportFunction(filePath, target).then(() => { expect(fs.writeFileSync).toHaveBeenCalledWith( filePath, @@ -77,4 +87,21 @@ describe('export', () => { ); }); }); + + it('should only get parameters for configs if configOnly is true', async () => { + getParametersMock.mockImplementationOnce(() => + Promise.resolve({ + 'path/to/config1': 'config1Value' + }) + ); + const filePath = './.env.test'; + const target = 'env'; + + return exportFunction(filePath, target, true).then(() => { + expect(fs.writeFileSync).toHaveBeenCalledWith( + filePath, + 'path_to_config1="config1Value"\n' + ); + }); + }); }); diff --git a/package.json b/package.json index b184355..7f9098c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oprah", - "version": "5.2.1", + "version": "5.3.0", "description": "Package to deploy parameters to AWS", "repository": "https://github.com/ACloudGuru/oprah.git", "author": "subash adhikari ", @@ -60,4 +60,4 @@ "pinst": "^2.1.6", "prettier": "^1.18.2" } -} +} \ No newline at end of file