From aad2f391fbf5803443a6ce2dedbb5960d110a3a7 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 27 Oct 2020 23:37:53 +0100 Subject: [PATCH] Turned all environment variable names in an Enum Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MovieQuoteBot.java | 6 ++-- .../xyz/vallat/louis/database/DBManager.java | 18 +++++------- .../louis/env/EnvironmentVariables.java | 28 +++++++++++++++++++ .../vallat/louis/subtitles/OpenSubtitles.java | 10 +++---- 4 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index 4409b9a..d800e88 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import xyz.vallat.louis.commands.*; import xyz.vallat.louis.database.DBManager; +import xyz.vallat.louis.env.EnvironmentVariables; import xyz.vallat.louis.subtitles.OpenSubtitles; import java.util.HashMap; @@ -19,9 +20,6 @@ import java.util.Map; public class MovieQuoteBot { - // TODO: MOVE TO ENUM - public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN"; - public static final String PREFIX = "!"; public static final String NAME = "Movies Quote Bot"; public static final String DESCRIPTION = "I may know some quotes from some movies."; @@ -42,7 +40,7 @@ public class MovieQuoteBot { DBManager.testConnection(); DBManager.initDatabase(); OpenSubtitles.login(); - discordLogin(System.getenv(DISCORD_TOKEN_ENVIRONMENT)); + discordLogin(System.getenv(EnvironmentVariables.DISCORD_TOKEN.getValue())); registerDiscordCommands(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index f7b0abd..be68eab 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -3,6 +3,7 @@ package xyz.vallat.louis.database; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.MovieQuoteBot; +import xyz.vallat.louis.env.EnvironmentVariables; import java.sql.Connection; import java.sql.DriverManager; @@ -15,19 +16,14 @@ import static xyz.vallat.louis.database.PropertyManager.initializeProperties; public final class DBManager { - public static final String DB_USERNAME_ENVIRONMENT = "DB_USERNAME"; - public static final String DB_PASSWORD_ENVIRONMENT = "DB_PASSWORD"; - public static final String DB_PORT_ENVIRONMENT = "DB_PORT"; - public static final String DB_HOST_ENVIRONMENT = "DB_HOST"; - public static final String DB_NAME_ENVIRONMENT = "DB_NAME"; private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName()); private static final String CONNECTION_PREFIX = "postgresql"; - private static final String USERNAME = System.getenv(DB_USERNAME_ENVIRONMENT); - private static final String PASSWORD = System.getenv(DB_PASSWORD_ENVIRONMENT); - private static final String DATABASE = System.getenv(DB_NAME_ENVIRONMENT); - private static final int PORT = System.getenv(DB_PORT_ENVIRONMENT) == null ? 5432 : - Integer.parseInt(System.getenv(DB_PORT_ENVIRONMENT)); - private static final String HOST = System.getenv(DB_HOST_ENVIRONMENT); + private static final String USERNAME = System.getenv(EnvironmentVariables.DB_USERNAME.getValue()); + private static final String PASSWORD = System.getenv(EnvironmentVariables.DB_PASSWORD.getValue()); + private static final String DATABASE = System.getenv(EnvironmentVariables.DB_NAME.getValue()); + private static final int PORT = System.getenv(EnvironmentVariables.DB_PORT.getValue()) == null ? 5432 : + Integer.parseInt(System.getenv(EnvironmentVariables.DB_PORT.getValue())); + private static final String HOST = System.getenv(EnvironmentVariables.DB_HOST.getValue()); private DBManager() { } diff --git a/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java b/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java new file mode 100644 index 0000000..218d84d --- /dev/null +++ b/src/main/java/xyz/vallat/louis/env/EnvironmentVariables.java @@ -0,0 +1,28 @@ +package xyz.vallat.louis.env; + +public enum EnvironmentVariables { + DISCORD_TOKEN("DISCORD_TOKEN"), + DB_USERNAME("DB_USERNAME"), + DB_PASSWORD("DB_PASSWORD"), + DB_NAME("DB_NAME"), + DB_HOST("DB_HOST"), + DB_PORT("DB_PORT"), + OS_USERNAME("OPEN_SUBTITLES_USERNAME"), + OS_PASSWORD("OPEN_SUBTITLES_PASSWORD"), + OS_USER_AGENT("OPEN_SUBTITLES_USER_AGENT"); + + private final String value; + + EnvironmentVariables(String value) { + this.value = value; + } + + public String getValue() { + return this.value; + } + + @Override + public String toString() { + return getValue(); + } +} diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index 2c877d2..3f075b1 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -6,20 +6,18 @@ import com.github.wtekiela.opensub4j.response.*; import org.apache.xmlrpc.XmlRpcException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.vallat.louis.env.EnvironmentVariables; import java.net.MalformedURLException; import java.net.URL; public final class OpenSubtitles { - public static final String OS_USERNAME_ENVIRONMENT = "OPEN_SUBTITLES_USERNAME"; - public static final String OS_PASSWORD_ENVIRONMENT = "OPEN_SUBTITLES_PASSWORD"; - public static final String OS_UA_ENVIRONMENT = "OPEN_SUBTITLES_USER_AGENT"; private static final Logger logger = LoggerFactory.getLogger(OpenSubtitles.class.getCanonicalName()); - private static final String USERNAME = System.getenv(OS_USERNAME_ENVIRONMENT); - private static final String PASSWORD = System.getenv(OS_PASSWORD_ENVIRONMENT); + private static final String USERNAME = System.getenv(EnvironmentVariables.OS_USERNAME.getValue()); + private static final String PASSWORD = System.getenv(EnvironmentVariables.OS_PASSWORD.getValue()); private static final String LANG = "en"; - private static final String USER_AGENT = System.getenv(OS_UA_ENVIRONMENT); + private static final String USER_AGENT = System.getenv(EnvironmentVariables.OS_USER_AGENT.getValue()); private static OpenSubtitlesClient client; private OpenSubtitles() {