From 6d692985e06fc3ae8ba52dcea2708e43de2aa6a2 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 13 Oct 2022 20:09:14 +0300 Subject: [PATCH] Add ini example --- TODO.md | 1 - dub.json | 11 ++++++ examples/ini/app.d | 37 +++++++++++++++++++ examples/ini/config.ini | 11 ++++++ .../javaProperties/application.properties | 2 +- makefile | 4 ++ 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 examples/ini/app.d create mode 100644 examples/ini/config.ini diff --git a/TODO.md b/TODO.md index 70b2017..94e1a64 100644 --- a/TODO.md +++ b/TODO.md @@ -14,4 +14,3 @@ - Case insensitive properties and sections - Escape characters - Support multi-line values with backslash - - Example diff --git a/dub.json b/dub.json index 3265e88..fba4221 100644 --- a/dub.json +++ b/dub.json @@ -49,6 +49,17 @@ "targetPath": "bin/examples/javaProperties", "workingDirectory": "bin/examples/javaProperties" }, + { + "name": "iniExample", + "targetType": "executable", + "description": "Example on how to read and work with INI files.", + "targetName": "iniExample", + "sourcePaths": ["examples/ini"], + "importPaths": ["source"], + "copyFiles": ["examples/ini/config.ini"], + "targetPath": "bin/examples/ini", + "workingDirectory": "bin/examples/ini" + }, { "name": "valueSubstitutionExample", "targetType": "executable", diff --git a/examples/ini/app.d b/examples/ini/app.d new file mode 100644 index 0000000..44986f0 --- /dev/null +++ b/examples/ini/app.d @@ -0,0 +1,37 @@ +/** + * 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.ini : loadIniConfig, parseIniConfig; + +import std.stdio : writeln; +import std.conv : to; + +void main() { + auto config = loadIniConfig("config.ini"); + auto serverConfig = config.getConfig("server"); + auto databaseConfig = parseIniConfig(" + host=localhost + port=5432 + "); + + auto applicationName = config.get("application.name"); + + auto httpHost = serverConfig.get("host"); + auto httpPort = serverConfig.get!uint("port"); + auto httpProtocol = serverConfig.get("protocol"); + + auto dbHost = databaseConfig.get("host"); + auto dbPort = databaseConfig.get!uint("port"); + + writeln("Starting " ~ applicationName ~ "..."); + writeln("Connecting to database at " ~ dbHost ~ ":" ~ dbPort.to!string ~ "..."); + writeln( + "HTTP server now listening at " ~ httpProtocol ~ "://" ~ httpHost ~ ":" ~ httpPort + .to!string); +} diff --git a/examples/ini/config.ini b/examples/ini/config.ini new file mode 100644 index 0000000..62f64ec --- /dev/null +++ b/examples/ini/config.ini @@ -0,0 +1,11 @@ +# This file is intentionally messy +# to demonstrate the capabilities +# of the format. + +[application] +name = "Fake HTTP Server" + +[server] +host = localhost +port = 8080 +protocol: https ;DO NOT CHANGE! \ No newline at end of file diff --git a/examples/javaProperties/application.properties b/examples/javaProperties/application.properties index 208660e..a9f1846 100644 --- a/examples/javaProperties/application.properties +++ b/examples/javaProperties/application.properties @@ -1,5 +1,5 @@ # This file is intentionally messy -# to demonstrate the full capabilities +# to demonstrate the capabilities # of the format. # App config diff --git a/makefile b/makefile index 36ac975..bb164ba 100644 --- a/makefile +++ b/makefile @@ -17,6 +17,7 @@ clean: run-examples: run-quickstartExample\ run-jsonExample \ run-javaPropertiesExample \ + run-iniExample \ run-valueSubstitutionExample \ run-manipulationExample @@ -29,6 +30,9 @@ run-jsonExample: run-javaPropertiesExample: dub run --build=release --config=javaPropertiesExample +run-iniExample: + dub run --build=release --config=iniExample + run-valueSubstitutionExample: dub run --build=release --config=valueSubstitutionExample