From e5de998577459375d65baf36e532609c4b96a1db Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sun, 9 Oct 2022 00:42:15 +0300 Subject: [PATCH] Add quickstart example to readme --- README.md | 37 +++++++++++++++++++++++++++++++-- dub.json | 11 ++++++++++ examples/quickstart/app.d | 33 +++++++++++++++++++++++++++++ examples/quickstart/config.json | 6 ++++++ makefile | 6 +++++- 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 examples/quickstart/app.d create mode 100644 examples/quickstart/config.json diff --git a/README.md b/README.md index 9cd6da9..8130a95 100644 --- a/README.md +++ b/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) diff --git a/dub.json b/dub.json index 560190b..9eace40 100644 --- a/dub.json +++ b/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", diff --git a/examples/quickstart/app.d b/examples/quickstart/app.d new file mode 100644 index 0000000..4949d65 --- /dev/null +++ b/examples/quickstart/app.d @@ -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")); +} diff --git a/examples/quickstart/config.json b/examples/quickstart/config.json new file mode 100644 index 0000000..90d1d12 --- /dev/null +++ b/examples/quickstart/config.json @@ -0,0 +1,6 @@ +{ + "application": { + "name": "Quickstart!", + "version": 1 + } +} diff --git a/makefile b/makefile index 86a39e6..0460c39 100644 --- a/makefile +++ b/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