Add config substitution example

This commit is contained in:
Mike Bierlee 2022-09-29 00:13:20 +03:00
parent 72af2f322e
commit 3ae90783de
4 changed files with 46 additions and 1 deletions

View file

@ -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"
}
]
}

View file

@ -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"));
}

View file

@ -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}"
}

View file

@ -15,3 +15,6 @@ run-examples: run-jsonExample
run-jsonExample:
dub run --build=release --config=jsonExample
run-valueSubstitutionExample:
dub run --build=release --config=valueSubstitutionExample