-
Notifications
You must be signed in to change notification settings - Fork 2
/
Credentials.cs
executable file
·47 lines (40 loc) · 1.62 KB
/
Credentials.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
namespace RRBot;
internal sealed class Credentials
{
private static readonly string AssemblyDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
private static readonly string CredentialsPath = Path.Combine(AssemblyDir, "credentials.json");
private static Credentials _instance;
public static Credentials Instance => _instance ??= new Credentials(CredentialsPath);
public enum ValidationResult
{
Success,
MissingCredentialsFile,
NeedMongoConnectionString,
NeedToken
}
[JsonProperty("mongoConnectionString")] public string MongoConnectionString { get; set; } = "";
[JsonProperty("token")] public string Token { get; set; } = "";
[JsonConstructor] private Credentials() {}
private Credentials(string jsonPath)
{
Credentials c = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText(jsonPath));
if (c is null)
{
Console.WriteLine("credentials.json file not found or is not a valid JSON file.");
Environment.Exit(1);
}
MongoConnectionString = c.MongoConnectionString;
Token = c.Token;
}
[SuppressMessage("ReSharper", "ConvertIfStatementToReturnStatement")]
public ValidationResult Validate()
{
if (!File.Exists(CredentialsPath))
return ValidationResult.MissingCredentialsFile;
if (string.IsNullOrWhiteSpace(MongoConnectionString))
return ValidationResult.NeedMongoConnectionString;
if (string.IsNullOrWhiteSpace(Token))
return ValidationResult.NeedToken;
return ValidationResult.Success;
}
}