From 055d09d77a3f16f9e5feec070edca1dda8c1207f Mon Sep 17 00:00:00 2001 From: Michael Green <84688932+michael-j-green@users.noreply.github.com> Date: Wed, 1 Mar 2023 10:22:19 +1100 Subject: [PATCH] feat: Added config file class to keep track of local settings such as database connectivity --- gaseous-signature-ingestor/Program.cs | 11 +-- gaseous-tools/Config.cs | 112 ++++++++++++++++++++++++++ gaseous-tools/gaseous-tools.csproj | 1 + 3 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 gaseous-tools/Config.cs diff --git a/gaseous-signature-ingestor/Program.cs b/gaseous-signature-ingestor/Program.cs index 60a09da..878bfb9 100644 --- a/gaseous-signature-ingestor/Program.cs +++ b/gaseous-signature-ingestor/Program.cs @@ -6,8 +6,6 @@ using gaseous_signature_parser.parsers; using gaseous_tools; using MySqlX.XDevAPI; -string configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gaseous-server"); - // process command line string[] commandLineArgs = Environment.GetCommandLineArgs(); @@ -47,15 +45,14 @@ foreach (string commandLineArg in commandLineArgs) } } -// check if configPath is valid and create it if not -if (!Directory.Exists(configPath)) +// check if Config.ConfigurationPath is valid and create it if not +if (!Directory.Exists(Config.ConfigurationPath)) { - Directory.CreateDirectory(configPath); + Directory.CreateDirectory(Config.ConfigurationPath); } // connect to database -string cs = @"server=localhost;userid=gaseous;password=gaseous;database=gaseous"; -Database db = new gaseous_tools.Database(Database.databaseType.MySql, cs); +Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString); // process provided files Console.WriteLine("Processing input files:"); diff --git a/gaseous-tools/Config.cs b/gaseous-tools/Config.cs new file mode 100644 index 0000000..c9e8e4e --- /dev/null +++ b/gaseous-tools/Config.cs @@ -0,0 +1,112 @@ +using System; +using Newtonsoft.Json; + +namespace gaseous_tools +{ + public static class Config + { + static ConfigFile _config; + + public static string ConfigurationPath + { + get + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gaseous-server"); + } + } + + static string ConfigurationFilePath + { + get + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gaseous-server", "config.json"); + } + } + + static string ConfigurationFilePath_Backup + { + get + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gaseous-server", "config.json.backup"); + } + } + + public static ConfigFile.Database DatabaseConfiguration + { + get + { + return _config.DatabaseConfiguration; + } + } + + static Config() + { + if (_config == null) + { + // load the config file + if (File.Exists(ConfigurationFilePath)) + { + string configRaw = File.ReadAllText(ConfigurationFilePath); + ConfigFile? _tempConfig = Newtonsoft.Json.JsonConvert.DeserializeObject(configRaw); + if (_tempConfig != null) + { + _config = _tempConfig; + } else + { + throw new Exception("There was an error reading the config file: Json returned null"); + } + } else + { + // no config file! + // use defaults and save + _config = new ConfigFile(); + string configRaw = Newtonsoft.Json.JsonConvert.SerializeObject(_config, Newtonsoft.Json.Formatting.Indented); + File.WriteAllText(ConfigurationFilePath, configRaw); + } + } + + Console.WriteLine("Using configuration:"); + Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(_config, Formatting.Indented)); + } + + public static void UpdateConfig() + { + // save any updates to the configuration + string configRaw = Newtonsoft.Json.JsonConvert.SerializeObject(_config, Newtonsoft.Json.Formatting.Indented); + if (File.Exists(ConfigurationFilePath_Backup)) + { + File.Delete(ConfigurationFilePath_Backup); + } + if (File.Exists(ConfigurationFilePath)) + { + File.Move(ConfigurationFilePath, ConfigurationFilePath_Backup); + } + File.WriteAllText(ConfigurationFilePath, configRaw); + } + + public class ConfigFile + { + public Database DatabaseConfiguration = new Database(); + + public class Database + { + public string HostName = "localhost"; + public string UserName = "gaseous"; + public string Password = "gaseous"; + public string DatabaseName = "gaseous"; + public int Port = 3306; + + [JsonIgnore] + public string ConnectionString + { + get + { + string dbConnString = "server=" + HostName + ";port=" + Port + ";userid=" + UserName + ";password=" + Password + ";database=" + DatabaseName + ""; + return dbConnString; + } + } + } + } + } +} + diff --git a/gaseous-tools/gaseous-tools.csproj b/gaseous-tools/gaseous-tools.csproj index bf015c7..5420952 100644 --- a/gaseous-tools/gaseous-tools.csproj +++ b/gaseous-tools/gaseous-tools.csproj @@ -9,6 +9,7 @@ +