From ba9fde9cac38557f1d8f89900d391541dbab62db Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Thu, 30 Sep 2021 17:05:05 +0100 Subject: [PATCH] Add object for storing API secrets. --- .../nekojimi/musicsearcher/SecretStore.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/moe/nekojimi/musicsearcher/SecretStore.java diff --git a/src/main/java/moe/nekojimi/musicsearcher/SecretStore.java b/src/main/java/moe/nekojimi/musicsearcher/SecretStore.java new file mode 100644 index 0000000..1b6555f --- /dev/null +++ b/src/main/java/moe/nekojimi/musicsearcher/SecretStore.java @@ -0,0 +1,59 @@ +package moe.nekojimi.musicsearcher; + +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlMapping; +import com.amihaiemil.eoyaml.YamlNode; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author jim + */ +public class SecretStore +{ + private static SecretStore secretStore; + + public static SecretStore get() + { + if (secretStore == null) + secretStore = new SecretStore(); + return secretStore; + } + + private Map secrets = new HashMap<>(); + + private SecretStore() + { + try + { + File file = new File("secrets.yml"); + if (!file.exists()) + { + System.out.println("WARNING: couldn't find secrets.yml. No API secrets available."); + return; + } + + YamlMapping yaml = Yaml.createYamlInput(file).readYamlMapping(); + for (YamlNode key : yaml.keys()) + secrets.put(key.asScalar().value(), yaml.string(key)); + } catch (IOException ex) { + Logger.getLogger(SecretStore.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public String getSecret(String key) + { + return secrets.getOrDefault(key,""); + } +}