mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-15 04:44:01 +01:00
Add quickstart example to readme
This commit is contained in:
parent
40fe5135d2
commit
e5de998577
37
README.md
37
README.md
|
@ -13,8 +13,6 @@ Features:
|
|||
- Internal configuration substitution (Value in config replaced by other path in config);
|
||||
- Parse configuration from string or JSONValue instead of from disk.
|
||||
|
||||
This is a work in progress. More will follow. For now see `examples/` to learn how to use it.
|
||||
|
||||
TODO: add tutorial on:
|
||||
|
||||
- Config loading
|
||||
|
@ -23,6 +21,41 @@ TODO: add tutorial on:
|
|||
- Env and config var substitution
|
||||
-- Escaping
|
||||
|
||||
## Getting started
|
||||
### DUB Dependency
|
||||
See the [DUB project page](https://code.dlang.org/packages/mirage-config) for instructions on how to include Mirage Config into your project.
|
||||
|
||||
### Quickstart
|
||||
```d
|
||||
import std.stdio : writeln;
|
||||
import mirage : loadConfig, parseJavaProperties;
|
||||
|
||||
void main() {
|
||||
// Load configuration from file (see examples/quickstart/config.json):
|
||||
auto config = loadConfig("config.json");
|
||||
writeln(config.get("application.name"));
|
||||
writeln(config.get!long("application.version"));
|
||||
|
||||
// Or parse directly from string:
|
||||
auto properties = parseJavaProperties("
|
||||
databaseDriver = Postgres
|
||||
database.host = localhost
|
||||
database.port = 5432
|
||||
");
|
||||
|
||||
auto databaseConfig = properties.getConfig("database");
|
||||
|
||||
writeln(properties.get("databaseDriver"));
|
||||
writeln(databaseConfig.get("host"));
|
||||
writeln(databaseConfig.get("port"));
|
||||
}
|
||||
```
|
||||
|
||||
More formats are available (see [Formats](#formats)).
|
||||
For more details and examples, see the [examples](examples) directory.
|
||||
|
||||
## Formats
|
||||
|
||||
## History
|
||||
|
||||
For a full overview of changes, see [CHANGES.md](CHANGES.md)
|
||||
|
|
11
dub.json
11
dub.json
|
@ -15,6 +15,17 @@
|
|||
"sourcePaths": ["source"],
|
||||
"mainSourceFile": "source/mirage/testmain.d"
|
||||
},
|
||||
{
|
||||
"name": "quickstartExample",
|
||||
"targetType": "executable",
|
||||
"description": "Minimal example on how to get started.",
|
||||
"targetName": "quickstartExample",
|
||||
"sourcePaths": ["examples/quickstart"],
|
||||
"importPaths": ["source"],
|
||||
"copyFiles": ["examples/quickstart/config.json"],
|
||||
"targetPath": "bin/examples/quickstart",
|
||||
"workingDirectory": "bin/examples/quickstart"
|
||||
},
|
||||
{
|
||||
"name": "jsonExample",
|
||||
"targetType": "executable",
|
||||
|
|
33
examples/quickstart/app.d
Normal file
33
examples/quickstart/app.d
Normal file
|
@ -0,0 +1,33 @@
|
|||
module examples.quickstart.app;
|
||||
|
||||
/**
|
||||
* 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 std.stdio : writeln;
|
||||
import mirage : loadConfig, parseJavaProperties;
|
||||
|
||||
void main() {
|
||||
// Load configuration from file (see examples/quickstart/config.json):
|
||||
auto config = loadConfig("config.json");
|
||||
writeln(config.get("application.name"));
|
||||
writeln(config.get!long("application.version"));
|
||||
|
||||
// Or parse directly from string:
|
||||
auto properties = parseJavaProperties("
|
||||
databaseDriver = Postgres
|
||||
database.host = localhost
|
||||
database.port = 5432
|
||||
");
|
||||
|
||||
auto databaseConfig = properties.getConfig("database");
|
||||
|
||||
writeln(properties.get("databaseDriver"));
|
||||
writeln(databaseConfig.get("host"));
|
||||
writeln(databaseConfig.get("port"));
|
||||
}
|
6
examples/quickstart/config.json
Normal file
6
examples/quickstart/config.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"application": {
|
||||
"name": "Quickstart!",
|
||||
"version": 1
|
||||
}
|
||||
}
|
6
makefile
6
makefile
|
@ -11,11 +11,15 @@ test:
|
|||
clean:
|
||||
dub clean
|
||||
|
||||
run-examples: run-jsonExample \
|
||||
run-examples: run-quickstartExample\
|
||||
run-jsonExample \
|
||||
run-javaPropertiesExample \
|
||||
run-valueSubstitutionExample \
|
||||
run-manipulationExample
|
||||
|
||||
run-quickstartExample:
|
||||
dub run --build=release --config=quickstartExample
|
||||
|
||||
run-jsonExample:
|
||||
dub run --build=release --config=jsonExample
|
||||
|
||||
|
|
Loading…
Reference in a new issue