Turned all environment variable names in an Enum

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-27 23:37:53 +01:00
parent afe16f9eb1
commit aad2f391fb
4 changed files with 41 additions and 21 deletions

View File

@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import xyz.vallat.louis.commands.*; import xyz.vallat.louis.commands.*;
import xyz.vallat.louis.database.DBManager; import xyz.vallat.louis.database.DBManager;
import xyz.vallat.louis.env.EnvironmentVariables;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import java.util.HashMap; import java.util.HashMap;
@ -19,9 +20,6 @@ import java.util.Map;
public class MovieQuoteBot { 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 PREFIX = "!";
public static final String NAME = "Movies Quote Bot"; public static final String NAME = "Movies Quote Bot";
public static final String DESCRIPTION = "I may know some quotes from some movies."; public static final String DESCRIPTION = "I may know some quotes from some movies.";
@ -42,7 +40,7 @@ public class MovieQuoteBot {
DBManager.testConnection(); DBManager.testConnection();
DBManager.initDatabase(); DBManager.initDatabase();
OpenSubtitles.login(); OpenSubtitles.login();
discordLogin(System.getenv(DISCORD_TOKEN_ENVIRONMENT)); discordLogin(System.getenv(EnvironmentVariables.DISCORD_TOKEN.getValue()));
registerDiscordCommands(); registerDiscordCommands();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {

View File

@ -3,6 +3,7 @@ package xyz.vallat.louis.database;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.MovieQuoteBot; import xyz.vallat.louis.MovieQuoteBot;
import xyz.vallat.louis.env.EnvironmentVariables;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -15,19 +16,14 @@ import static xyz.vallat.louis.database.PropertyManager.initializeProperties;
public final class DBManager { 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 Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName());
private static final String CONNECTION_PREFIX = "postgresql"; private static final String CONNECTION_PREFIX = "postgresql";
private static final String USERNAME = System.getenv(DB_USERNAME_ENVIRONMENT); private static final String USERNAME = System.getenv(EnvironmentVariables.DB_USERNAME.getValue());
private static final String PASSWORD = System.getenv(DB_PASSWORD_ENVIRONMENT); private static final String PASSWORD = System.getenv(EnvironmentVariables.DB_PASSWORD.getValue());
private static final String DATABASE = System.getenv(DB_NAME_ENVIRONMENT); private static final String DATABASE = System.getenv(EnvironmentVariables.DB_NAME.getValue());
private static final int PORT = System.getenv(DB_PORT_ENVIRONMENT) == null ? 5432 : private static final int PORT = System.getenv(EnvironmentVariables.DB_PORT.getValue()) == null ? 5432 :
Integer.parseInt(System.getenv(DB_PORT_ENVIRONMENT)); Integer.parseInt(System.getenv(EnvironmentVariables.DB_PORT.getValue()));
private static final String HOST = System.getenv(DB_HOST_ENVIRONMENT); private static final String HOST = System.getenv(EnvironmentVariables.DB_HOST.getValue());
private DBManager() { private DBManager() {
} }

View File

@ -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();
}
}

View File

@ -6,20 +6,18 @@ import com.github.wtekiela.opensub4j.response.*;
import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.XmlRpcException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.env.EnvironmentVariables;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
public final class OpenSubtitles { 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 Logger logger = LoggerFactory.getLogger(OpenSubtitles.class.getCanonicalName());
private static final String USERNAME = System.getenv(OS_USERNAME_ENVIRONMENT); private static final String USERNAME = System.getenv(EnvironmentVariables.OS_USERNAME.getValue());
private static final String PASSWORD = System.getenv(OS_PASSWORD_ENVIRONMENT); private static final String PASSWORD = System.getenv(EnvironmentVariables.OS_PASSWORD.getValue());
private static final String LANG = "en"; 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 static OpenSubtitlesClient client;
private OpenSubtitles() { private OpenSubtitles() {