Skip to content

Commit

Permalink
Fix json overrides (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-lau authored May 12, 2024
1 parent 6f23fff commit 4178b8d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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

0 comments on commit 4178b8d

Please sign in to comment.