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

Fix json overrides #77

Merged
merged 3 commits into from
May 12, 2024
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Overrides work in the following manner:
key by key basis.
- For every other config format, an override file replaces the entire
config.
- If `smtp.json` or `smtp.yaml` exist, their contents will be loaded before all other config files. You can make use of [JSON Overrides](#json-overrides) here for a single file config.

## Examples

Expand Down Expand Up @@ -243,15 +244,28 @@ These are as you would expect, and returns an object as given in the file.

If a requested .json or .hjson file does not exist then the same file will be checked for with a .yaml extension and that will be loaded instead. This is done because YAML files are far easier for a human to write.

### <a name="json-overrides">JSON Overrides</a>
You can use JSON, HJSON or YAML files to override any other file by prefixing the outer variable name with a `!` e.g.

```js
{
"!smtpgreeting": [ 'this is line one', 'this is line two' ]
"!smtpgreeting": ['this is line one', 'this is line two'],
"!smtp.ini": {
main: {
nodes: 0,
},
headers: {
max_lines: 1000,
max_received: 100,
},
},
"!custom-plugin.yaml": {
secret: 'example',
},
}
```

If the config/smtpgreeting file did not exist, then this value would replace it.
If the config/smtpgreeting wasn't loaded before, then this value would replace it. Since `smtp.json` is always loaded first, it can be used to override existing config files.

NOTE: You must ensure that the data type (e.g. Object, Array or String) for the replaced value is correct. This cannot be done automatically.

Expand Down
3 changes: 3 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ function merge_struct(defaults, overrides) {
}
return defaults
}

// JSON overrides needs smtp.(json|yaml) loaded early
module.exports.get('smtp.json');
4 changes: 3 additions & 1 deletion lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ class Reader {
const fn = key.substr(1)
// Overwrite the config cache for this filename
console.log(`Overriding file ${fn} with config from ${name}`)
this._config_cache[path.join(cp, fn)] = result[key]
const cache_key = path.join(cp, fn)
this._overrides[cache_key] = true
this._config_cache[cache_key] = result[key]
}
}
}
Expand Down
Loading