From a62e13c664e1d41bfc16a05c98bf978599fb9fe5 Mon Sep 17 00:00:00 2001 From: Louis Laureys Date: Sat, 11 May 2024 15:22:51 +0200 Subject: [PATCH 1/3] Fix JSON/YAML overrides --- config.js | 3 +++ lib/reader.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/config.js b/config.js index 795298b..c52eb9c 100644 --- a/config.js +++ b/config.js @@ -157,3 +157,6 @@ function merge_struct(defaults, overrides) { } return defaults } + +// Load smtp.json or smtp.yaml as early as possible +module.exports.get('smtp.json'); \ No newline at end of file diff --git a/lib/reader.js b/lib/reader.js index f70a982..a6bb385 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -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] } } } From 18cedaa99f90b09408ef7015194389dec674c0b3 Mon Sep 17 00:00:00 2001 From: Louis Laureys Date: Sat, 11 May 2024 15:38:52 +0200 Subject: [PATCH 2/3] Improve json override doc --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 014514e..75921f5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. +### JSON Overrides 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. From a7cb8666f99d0a6df8bdbaf4b31458a0df107fbe Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Sun, 12 May 2024 12:59:55 -0700 Subject: [PATCH 3/3] config.js: tell why they need to be loaded there --- config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.js b/config.js index c52eb9c..ab0eb02 100644 --- a/config.js +++ b/config.js @@ -158,5 +158,5 @@ function merge_struct(defaults, overrides) { return defaults } -// Load smtp.json or smtp.yaml as early as possible +// JSON overrides needs smtp.(json|yaml) loaded early module.exports.get('smtp.json'); \ No newline at end of file