diff --git a/.gitignore b/.gitignore index 5ff6309..a879ee7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ target/ !**/src/test/**/target/ ### IntelliJ IDEA ### +.idea/ .idea/modules.xml .idea/jarRepositories.xml .idea/compiler.xml diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b34a98 --- /dev/null +++ b/README.md @@ -0,0 +1,89 @@ + +# ConfigurationUtilities + +A java library to load config from cli-args, .env, toml-config and default values. + +![GitHub License](https://img.shields.io/github/license/B00tLoad/ConfigurationUtilities) +![GitHub Issues or Pull Requests](https://img.shields.io/github/issues/B00tLoad/ConfigurationUtilities) +![GitHub repo size](https://img.shields.io/github/repo-size/B00tLoad/ConfigurationUtilities) +![Maven Central Version](https://img.shields.io/maven-central/v/space.b00tload.utils/ConfigurationUtilities) + + + + +## Acknowledgements +This project depends on the following libraries: + + - org.slf4j:slf4f-api + - com.electronwill.night-config:toml + + +## Installation + +Install ConfigurationUtilities with Maven + +```xml + + space.b00tload.utils + ConfigurationUtilities + 1.0.0 + +``` + +## Usage/Examples +Create an enum implementing the interface `space.b00tload.utils.configuration.ConfigValues` (e.g. as shown below). +```java +public enum ConfigurationValues implements ConfigValues { + + DISCORD_TOKEN("discord-token", "db", "DISCORD_BOT_TOKEN", "discord.token.bot", null), + DISCORD_APP_ID("discord-app-id", "da", "DISCORD_APPLICATION_ID", "discord.token.applicationid", null), + DISCORD_PUB_KEY("discord-public-key", "dp", "DISCORD_PUBLIC_KEY", "discord.token.publickey", null), + W2G_TOKEN("w2g-token", "w", "W2G_API_TOKEN", "w2g.token", null), + ENDPOINT_PORT("webserver-port", "p", "WEBSERVER_PORT", "webserver.port", "86542"), + ; + + private final String flag, flagAlias, env, toml, defaultValue; + + ConfigValues(String flag, String flagAlias, String env, String toml, String defaultValue) { + this.flag = flag; + this.flagAlias = flagAlias; + this.env = env; + this.toml = toml; + this.defaultValue = defaultValue; + } + + [... implement override methods] + + @Override + public String toString() { + return "ConfigValues{" + + ", defaultValue='" + defaultValue + '\'' + + "tomlPath='" + toml + '\'' + + ", environmentVarName='" + env + '\'' + + ", flagAlias='" + flagAlias + '\'' + + ", flag='" + flag + '\'' + + '}'; + } +} +``` + +Afterwards initialize the Configuration using `Configuration.init(args, SOFTWARE_VERSION, APPLICATION_BASE, ConfigurationValues.values());`. +- args = args passed to main-methods +- SOFTWARE_VERSION = the version of your software +- APPLICATION_BASE = the base directory for app data (config.toml will be saved in APPLICATION_BASE/config/) +- ConfigurationValues.values() = values() method of the enum + +## License + +[GNU GPL v3](https://github.com/B00tLoad/ConfigurationUtilities/blob/master/LICENSE) + + +## Maintainers + +- [@B00tLoad](https://www.github.com/B00tLoad) + + +## Support + +For support, open an issue or contact me at alix (at) ja-lol-ey (dot) de. + diff --git a/pom.xml b/pom.xml index d487549..e4dbc26 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,13 @@ 2024 https://github.com/B00tLoad/ConfigurationUtilities + + + GNU GPL v3.0 + https://github.com/B00tLoad/ConfigurationUtilities/blob/master/LICENSE + + + B00tLoad_ @@ -32,9 +39,16 @@ + + scm:git:git://github.com/B00tLoad/ConfigurationUtilities/.git + scm:git:ssh://github.com:B00tLoad/ConfigurationUtilities.git + https://github.com/B00tLoad/ConfigurationUtilities/tree/master + + + GitHub - https://github.com/B00tLoad/SnowflakeService/issues + https://github.com/B00tLoad/ConfigurationUtilities/issues @@ -47,12 +61,12 @@ - ch.qos.logback - logback-classic - 1.5.5 + org.slf4j + slf4j-api + 2.0.13 - + com.electronwill.night-config toml @@ -60,4 +74,73 @@ + + + + org.sonatype.central + central-publishing-maven-plugin + 0.4.0 + true + + central + true + + + + org.apache.maven.plugins + maven-source-plugin + 3.3.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.6.3 + + + + example + a + Usage example: + + + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 3.2.4 + + + sign-artifacts + verify + + sign + + + ${gpg.keyname} + ${gpg.keyname} + + + + + + + + \ No newline at end of file diff --git a/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigException.java b/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigException.java index ae70f44..1d3d257 100644 --- a/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigException.java +++ b/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigException.java @@ -7,4 +7,79 @@ package space.b00tload.utils.configuration.exceptions; * @since 1.0.0 * @version 1.0.0 */ -public class ConfigException extends RuntimeException {} +public class ConfigException extends RuntimeException { + + /** + * Constructs a new config exception with {@code null} as its + * detail message. The cause is not initialized, and may subsequently be + * initialized by a call to {@link #initCause}. + */ + public ConfigException() { + super(); + } + + /** + * Constructs a new config exception with the specified detail message. + * The cause is not initialized, and may subsequently be initialized by a + * call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for + * later retrieval by the {@link #getMessage()} method. + */ + public ConfigException(String message) { + super(message); + } + + /** + * Constructs a new config exception with the specified detail message and + * cause.

Note that the detail message associated with + * {@code cause} is not automatically incorporated in + * this config exception's detail message. + * + * @param message the detail message (which is saved for later retrieval + * by the {@link #getMessage()} method). + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A {@code null} value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + * @since 1.4 + */ + public ConfigException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructs a new config exception with the specified cause and a + * detail message of {@code (cause==null ? null : cause.toString())} + * (which typically contains the class and detail message of + * {@code cause}). This constructor is useful for config exceptions + * that are little more than wrappers for other throwables. + * + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A {@code null} value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + * @since 1.4 + */ + public ConfigException(Throwable cause) { + super(cause); + } + + /** + * Constructs a new config exception with the specified detail + * message, cause, suppression enabled or disabled, and writable + * stack trace enabled or disabled. + * + * @param message the detail message. + * @param cause the cause. (A {@code null} value is permitted, + * and indicates that the cause is nonexistent or unknown.) + * @param enableSuppression whether or not suppression is enabled + * or disabled + * @param writableStackTrace whether or not the stack trace should + * be writable + * @since 1.7 + */ + protected ConfigException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigIncompleteException.java b/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigIncompleteException.java index 76f47be..d06a0ba 100644 --- a/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigIncompleteException.java +++ b/src/main/java/space/b00tload/utils/configuration/exceptions/ConfigIncompleteException.java @@ -15,6 +15,9 @@ import java.util.stream.Collectors; */ public class ConfigIncompleteException extends RuntimeException { + /** + * A {@code java.util.List} of {@code space.b00tload.utils.configuration.ConfigValues} for storing missing values. + */ private final List missingValues; /**