mirror of
https://github.com/mbierlee/mirage-config.git
synced 2025-01-18 06:12:50 +01:00
Add config substitution example
This commit is contained in:
parent
72af2f322e
commit
3ae90783de
4 changed files with 46 additions and 1 deletions
11
dub.json
11
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
23
examples/valueSubstitution/app.d
Normal file
23
examples/valueSubstitution/app.d
Normal 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"));
|
||||
}
|
8
examples/valueSubstitution/config.json
Normal file
8
examples/valueSubstitution/config.json
Normal 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}"
|
||||
}
|
5
makefile
5
makefile
|
@ -14,4 +14,7 @@ clean:
|
|||
run-examples: run-jsonExample
|
||||
|
||||
run-jsonExample:
|
||||
dub run --build=release --config=jsonExample
|
||||
dub run --build=release --config=jsonExample
|
||||
|
||||
run-valueSubstitutionExample:
|
||||
dub run --build=release --config=valueSubstitutionExample
|
Loading…
Reference in a new issue