From 3ae90783de98642daa5eb86875401716b2d551d4 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 29 Sep 2022 00:13:20 +0300 Subject: [PATCH] Add config substitution example --- dub.json | 11 +++++++++++ examples/valueSubstitution/app.d | 23 +++++++++++++++++++++++ examples/valueSubstitution/config.json | 8 ++++++++ makefile | 5 ++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 examples/valueSubstitution/app.d create mode 100644 examples/valueSubstitution/config.json diff --git a/dub.json b/dub.json index 597f3f6..7833875 100644 --- a/dub.json +++ b/dub.json @@ -25,6 +25,17 @@ "copyFiles": ["examples/json/config.json"], "targetPath": "bin/examples/json", "workingDirectory": "bin/examples/json" + }, + { + "name": "valueSubstitutionExample", + "targetType": "executable", + "description": "Example of how to use environment and config substitution.", + "targetName": "valueSubstitutionExample", + "sourcePaths": ["examples/valueSubstitution"], + "importPaths": ["source"], + "copyFiles": ["examples/valueSubstitution/config.json"], + "targetPath": "bin/examples/valueSubstitution", + "workingDirectory": "bin/examples/valueSubstitution" } ] } diff --git a/examples/valueSubstitution/app.d b/examples/valueSubstitution/app.d new file mode 100644 index 0000000..81521e7 --- /dev/null +++ b/examples/valueSubstitution/app.d @@ -0,0 +1,23 @@ +/** + * Authors: + * Mike Bierlee, m.bierlee@lostmoment.com + * Copyright: 2022 Mike Bierlee + * License: + * This software is licensed under the terms of the MIT license. + * The full terms of the license can be found in the LICENSE file. + */ + +import mirage.json : loadJsonConfig, parseJsonConfig; + +import std.stdio : writeln; +import std.process : environment; + +void main() { + // This example shows how values in configuration can be substituted from + // environment variables or other configuration paths. + + environment["subject"] = "world"; + auto config = loadJsonConfig("config.json"); + + writeln(config.get("greeting")); +} diff --git a/examples/valueSubstitution/config.json b/examples/valueSubstitution/config.json new file mode 100644 index 0000000..e570a9f --- /dev/null +++ b/examples/valueSubstitution/config.json @@ -0,0 +1,8 @@ +{ + "parameters": { + "greeting": "Hello", + "subject": "$CONFIG_EXAMPLE_SUBJECT", + "followUp": "${CONFIG_EXAMPLE_FOLLOW_UP:Enjoy your day!}" + }, + "greeting": "${parameters.greeting} ${parameters.subject}! ${parameters.followUp}" +} diff --git a/makefile b/makefile index 9fe74fa..096b9db 100644 --- a/makefile +++ b/makefile @@ -14,4 +14,7 @@ clean: run-examples: run-jsonExample run-jsonExample: - dub run --build=release --config=jsonExample \ No newline at end of file + dub run --build=release --config=jsonExample + +run-valueSubstitutionExample: + dub run --build=release --config=valueSubstitutionExample \ No newline at end of file