Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement combined config. #3

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# bedrock-config-yaml ChangeLog

## 2.1.0 - TBD

### Added:
- Add support for a `combined.yaml` file that may contain both an `app` and
`core` section. This file may be used instead of, or in combination with
separate `app.yaml` and `core.yaml` files. If used in conjunction with
separate files, the values in the `app.yaml` and `core.yaml` files will
override the values in the `combined.yaml` file. The use of a single
`combined.yaml` file simplifies some deployment environments.

## 2.0.0 - 2020-12-09

### Changed:
Expand Down
6 changes: 5 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {config} = bedrock;
import path from 'path';

const namespace = 'config-yaml';
const cfg = config[namespace] = {app: {}, core: {}};
const cfg = config[namespace] = {app: {}, combined: {}, core: {}};

// The Bedrock events described below are documented here:
// https://github.com/digitalbazaar/bedrock#bedrockevents
Expand All @@ -18,3 +18,7 @@ cfg.app.filename = 'app.yaml';
// applied by the last handler for `bedrock-cli.parsed`
cfg.core.path = path.join('/etc', 'bedrock-config');
cfg.core.filename = 'core.yaml';

// a combined config may include both an `app` and `core` section
cfg.combined.path = path.join('/etc', 'bedrock-config');
cfg.combined.filename = 'combined.yaml';
34 changes: 22 additions & 12 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bedrock.events.on('bedrock-cli.parsed', function applyConfig() {
throw new Error('"bedrock-config-yaml" must be the last import.');
}

_applyConfig({type: 'core'});
_applyConfig({configType: 'core'});
});

bedrock.events.on('bedrock.configure', function applyConfig() {
Expand All @@ -30,19 +30,29 @@ bedrock.events.on('bedrock.configure', function applyConfig() {
throw new Error('"bedrock-config-yaml" must be the last import.');
}

_applyConfig({type: 'app'});
_applyConfig({configType: 'app'});
});

function _applyConfig({type}) {
function _applyConfig({configType}) {
logger.debug(`Attempting to apply the "${configType}" configuration.`);
// attempt to load the YAML file specified by the config
const configFile = path.join(cfg[type].path, cfg[type].filename);
if(fs.existsSync(configFile)) {
logger.debug(`"${type}" configuration found "${configFile}".`);
const configYaml = jsYaml.safeLoad(fs.readFileSync(configFile, 'utf8'));

// params: deep, target, source
extend(true, config, configYaml);
} else {
logger.debug(`"${type}" configuration not found "${configFile}".`);
const types = ['combined', configType];
for(const type of types) {
const configFile = path.join(cfg[type].path, cfg[type].filename);
if(fs.existsSync(configFile)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, _applyConfig would be an async function and this would use await on the promise version of fs.exists. Seeing as the original version was still using blocking IO, I wouldn't hold up the PR for this. I'll create an issue out of this comment.

logger.debug(`"${type}" configuration found "${configFile}".`);
let configYaml = jsYaml.safeLoad(fs.readFileSync(configFile, 'utf8'));

// apply the combined config if it contains a section
// that corresponds to `configType`
if(type === 'combined' && configYaml[configType]) {
configYaml = configYaml[configType];
}

// params: deep, target, source
extend(true, config, configYaml);
} else {
logger.debug(`"${type}" configuration not found "${configFile}".`);
}
}
}
5 changes: 5 additions & 0 deletions test/mocha/10-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('bedrock-config-yaml', () => {
it('app yaml configuration should be merged into bedrock config', () => {
const testBedrockModuleConfig = {
bar: 'fromBedrockConfig',
combinedAppValue: 'hPtQMAHvzECxDRJd',
foo: 'fromYaml',
overwriteMe: 'fromYaml',
};
Expand All @@ -20,5 +21,9 @@ describe('bedrock-config-yaml', () => {
should.exist(config.loggers.console.someLoggerConfig);
config.loggers.console.someLoggerConfig.should.be.a('string');
config.loggers.console.someLoggerConfig.should.equal('foo');
should.exist(config.loggers.console.someLoggerCombinedConfig);
config.loggers.console.someLoggerCombinedConfig.should.be.a('string');
config.loggers.console.someLoggerCombinedConfig
.should.equal('FDRqpNJLVkgfVxPe');
});
});
7 changes: 7 additions & 0 deletions test/mock-configs/combined.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app:
test-bedrock-module:
combinedAppValue: hPtQMAHvzECxDRJd
core:
loggers:
console:
someLoggerCombinedConfig: FDRqpNJLVkgfVxPe
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {config} = bedrock;
require('bedrock-config-yaml');

config['config-yaml'].app.path = path.join(__dirname, 'mock-configs');
config['config-yaml'].combined.path = path.join(__dirname, 'mock-configs');
config['config-yaml'].core.path = path.join(__dirname, 'mock-configs');

// config for mock test-bedrock-module
Expand Down