Skip to content

Commit

Permalink
feat: new target option on export
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverSting committed Jun 23, 2021
1 parent e693cd3 commit c4009fd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
11 changes: 8 additions & 3 deletions bin/oprah
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});

Expand Down
11 changes: 9 additions & 2 deletions lib/commands/import-export/make-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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..`));
Expand All @@ -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)
);
};

Expand Down
22 changes: 20 additions & 2 deletions lib/commands/import-export/make-export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('export', () => {
const getSettingsMock = jest.fn();
const getParametersMock = jest.fn();

beforeEach(() => {
beforeAll(() => {
exportFunction = makeExport({
settingsService: {
getSettings: getSettingsMock
Expand All @@ -21,7 +21,7 @@ describe('export', () => {
});
});

it('should write to the specified file path', () => {
beforeEach(() => {
getSettingsMock.mockImplementation(() =>
Promise.resolve({
secretParameters: ['path/to/secret1'],
Expand All @@ -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(() => {
Expand All @@ -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'
);
});
});
});
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"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 <subash.adhikari@acloud.guru>",
"engines": {
"node": ">=12.14.0"
},
"contributors": [
"DevOps Team <devops@acloud.guru>"
"DevOps Team <devops@acloud.guru>",
"Jaepil Kim <jaepil.kim@acloud.guru>"
],
"license": "MIT",
"bin": {
Expand Down Expand Up @@ -58,4 +59,4 @@
"pinst": "^2.1.6",
"prettier": "^1.18.2"
}
}
}

0 comments on commit c4009fd

Please sign in to comment.