2021-11-08 05:46:26 +00:00
|
|
|
|
using CommandLine;
|
2022-08-10 01:09:08 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace WorldTime;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads and holds configuration values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class Configuration {
|
|
|
|
|
public string BotToken { get; }
|
|
|
|
|
public string? DBotsToken { get; }
|
|
|
|
|
|
|
|
|
|
public int ShardTotal { get; }
|
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
public string? SqlHost { get; }
|
|
|
|
|
public string? SqlDatabase { get; }
|
|
|
|
|
public string SqlUsername { get; }
|
|
|
|
|
public string SqlPassword { get; }
|
|
|
|
|
|
|
|
|
|
public Configuration() {
|
|
|
|
|
var args = CommandLineParameters.Parse(Environment.GetCommandLineArgs());
|
|
|
|
|
var path = args?.ConfigFile ?? Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)
|
|
|
|
|
+ Path.DirectorySeparatorChar + "." + Path.DirectorySeparatorChar + "settings.json";
|
2021-11-08 05:46:26 +00:00
|
|
|
|
|
|
|
|
|
// Looks for configuration file
|
2022-08-10 01:09:08 +00:00
|
|
|
|
JObject jc;
|
|
|
|
|
try {
|
|
|
|
|
var conftxt = File.ReadAllText(path);
|
|
|
|
|
jc = JObject.Parse(conftxt);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
string pfx;
|
|
|
|
|
if (ex is JsonException) pfx = "Unable to parse configuration: ";
|
|
|
|
|
else pfx = "Unable to access configuration: ";
|
2021-11-08 05:46:26 +00:00
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
throw new Exception(pfx + ex.Message, ex);
|
|
|
|
|
}
|
2021-11-08 05:46:26 +00:00
|
|
|
|
|
|
|
|
|
BotToken = ReadConfKey<string>(jc, nameof(BotToken), true);
|
|
|
|
|
DBotsToken = ReadConfKey<string>(jc, nameof(DBotsToken), false);
|
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
ShardTotal = args.ShardTotal ?? ReadConfKey<int?>(jc, nameof(ShardTotal), false) ?? 1;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
if (ShardTotal < 1) throw new Exception($"'{nameof(ShardTotal)}' must be a positive integer.");
|
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
SqlHost = ReadConfKey<string>(jc, nameof(SqlHost), false);
|
|
|
|
|
SqlDatabase = ReadConfKey<string?>(jc, nameof(SqlDatabase), false);
|
|
|
|
|
SqlUsername = ReadConfKey<string>(jc, nameof(SqlUsername), true);
|
|
|
|
|
SqlPassword = ReadConfKey<string>(jc, nameof(SqlPassword), true);
|
2021-11-08 05:46:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static T? ReadConfKey<T>(JObject jc, string key, [DoesNotReturnIf(true)] bool failOnEmpty) {
|
|
|
|
|
if (jc.ContainsKey(key)) return jc[key]!.Value<T>();
|
|
|
|
|
if (failOnEmpty) throw new Exception($"'{key}' must be specified.");
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
class CommandLineParameters {
|
|
|
|
|
[Option('c', "config")]
|
|
|
|
|
public string? ConfigFile { get; set; }
|
2021-11-08 05:46:26 +00:00
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
[Option("shardtotal")]
|
2021-11-08 05:46:26 +00:00
|
|
|
|
public int? ShardTotal { get; set; }
|
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
public static CommandLineParameters? Parse(string[] args) {
|
|
|
|
|
CommandLineParameters? result = null;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
|
2022-08-10 01:09:08 +00:00
|
|
|
|
new Parser(settings => {
|
|
|
|
|
settings.IgnoreUnknownArguments = true;
|
|
|
|
|
settings.AutoHelp = false;
|
|
|
|
|
settings.AutoVersion = false;
|
|
|
|
|
}).ParseArguments<CommandLineParameters>(args)
|
|
|
|
|
.WithParsed(p => result = p)
|
|
|
|
|
.WithNotParsed(e => { /* ignore */ });
|
|
|
|
|
return result;
|
2021-11-08 05:46:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|