diff --git a/README.md b/README.md index 8abfc63..dada26f 100644 --- a/README.md +++ b/README.md @@ -175,8 +175,11 @@ Usage: oprah export [options] Export of all of the configuration from the provider to a text json file Options: - -p, --path [path] The location for the output secrets & configuration file - (default: "/tmp/oprah-exports.json") + -p, --path [path] The location for the output secrets & configuration file + (default: "/tmp/oprah-exports.json" or ".env_oprah") + -t, --target [target] The output target, available options are json|env + (default:json) + -h, --help display help for command ``` diff --git a/bin/oprah b/bin/oprah index 3f23fec..c25a8ca 100755 --- a/bin/oprah +++ b/bin/oprah @@ -64,12 +64,17 @@ program ) .option( '-p, --path [path]', - 'The location for the output secrets & configuration file (default: "/tmp/oprah-exports.json")' + 'The location for the output secrets & configuration file (default: "/tmp/oprah-exports.json" or ".env_oprah")' ) - .action(({ path }) => { + .option( + '-t, --target [target]', + 'The output target, available options are json|env (default:json)' + ) + .action(({ path, target }) => { const { stage, config } = program.opts(); oprahPromise = makeOprah({ stage, config }).export( - path || '/tmp/oprah-exports.json' + path || (target === 'env' ? '.env_oprah' : '/tmp/oprah-exports.json'), + target || 'json' ); }); diff --git a/lib/commands/import-export/make-export.js b/lib/commands/import-export/make-export.js index 839b275..def1c70 100644 --- a/lib/commands/import-export/make-export.js +++ b/lib/commands/import-export/make-export.js @@ -4,7 +4,10 @@ const fs = require('fs'); const { log } = require('../../utils/logger'); -const makeExport = ({ settingsService, parameterStore }) => async filePath => { +const makeExport = ({ settingsService, parameterStore }) => async ( + filePath, + target +) => { const settings = await settingsService.getSettings(); log(chalk.white(`Getting parameters..`)); @@ -26,7 +29,11 @@ const makeExport = ({ settingsService, parameterStore }) => async filePath => { return fs.writeFileSync( filePath, - JSON.stringify({ configs, secrets }, null, 2) + target === 'env' + ? `${Object.entries({ ...configs, ...secrets }) + .map(([key, val]) => `${key.replace(/[^A-Za-z0-9]+/g, '_')}="${val}"`) + .join('\n')}\n` + : JSON.stringify({ configs, secrets }, null, 2) ); }; diff --git a/lib/commands/import-export/make-export.test.js b/lib/commands/import-export/make-export.test.js index 312c96b..b8a999b 100644 --- a/lib/commands/import-export/make-export.test.js +++ b/lib/commands/import-export/make-export.test.js @@ -10,7 +10,7 @@ describe('export', () => { const getSettingsMock = jest.fn(); const getParametersMock = jest.fn(); - beforeEach(() => { + beforeAll(() => { exportFunction = makeExport({ settingsService: { getSettings: getSettingsMock @@ -21,7 +21,7 @@ describe('export', () => { }); }); - it('should write to the specified file path', () => { + beforeEach(() => { getSettingsMock.mockImplementation(() => Promise.resolve({ secretParameters: ['path/to/secret1'], @@ -40,7 +40,13 @@ 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(() => { @@ -59,4 +65,16 @@ describe('export', () => { ); }); }); + + it('should write ENV version to the specified file path', () => { + const filePath = './.env.test'; + const target = 'env'; + + return exportFunction(filePath, target).then(() => { + expect(fs.writeFileSync).toHaveBeenCalledWith( + filePath, + 'path_to_config1="config1Value"\npath_to_secret1="secret1Value"\n' + ); + }); + }); }); diff --git a/package.json b/package.json index acb86bb..74d280f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oprah", - "version": "5.0.1", + "version": "5.1.0", "description": "Package to deploy parameters to AWS", "repository": "https://github.com/ACloudGuru/oprah.git", "author": "subash adhikari ", @@ -8,7 +8,8 @@ "node": ">=12.14.0" }, "contributors": [ - "DevOps Team " + "DevOps Team ", + "Jaepil Kim " ], "license": "MIT", "bin": { @@ -58,4 +59,4 @@ "pinst": "^2.1.6", "prettier": "^1.18.2" } -} +} \ No newline at end of file