Creds *should* now be loadable from environment variables starting with NadekoBot:

This commit is contained in:
Kwoth 2016-11-15 09:42:06 +01:00
parent 5d7ce508dd
commit 1538364f00
4 changed files with 53 additions and 25 deletions

View File

@ -20,6 +20,7 @@ using NadekoBot.TypeReaders;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using NadekoBot.Modules.Music; using NadekoBot.Modules.Music;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using Microsoft.Extensions.Configuration;
namespace NadekoBot namespace NadekoBot
{ {
@ -51,6 +52,8 @@ namespace NadekoBot
public async Task RunAsync(params string[] args) public async Task RunAsync(params string[] args)
{ {
SetupLogger(); SetupLogger();
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();

View File

@ -5,6 +5,7 @@ using System.IO;
using Discord; using Discord;
using System.Linq; using System.Linq;
using NLog; using NLog;
using Microsoft.Extensions.Configuration;
namespace NadekoBot.Services.Impl namespace NadekoBot.Services.Impl
{ {
@ -31,34 +32,54 @@ namespace NadekoBot.Services.Impl
public int TotalShards { get; } public int TotalShards { get; }
public string CarbonKey { get; } public string CarbonKey { get; }
public string credsFileName { get; } = Path.Combine(Directory.GetCurrentDirectory(), "credentials.json");
public BotCredentials() public BotCredentials()
{ {
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
try { File.WriteAllText("./credentials_example.json", JsonConvert.SerializeObject(new CredentialsModel(), Formatting.Indented)); } catch { } try { File.WriteAllText("./credentials_example.json", JsonConvert.SerializeObject(new CredentialsModel(), Formatting.Indented)); } catch { }
if (File.Exists("./credentials.json")) if(!File.Exists(credsFileName))
_log.Warn($"credentials.json is missing. Attempting to load creds from environment variables prefixed with 'NadekoBot:'. Example is in {Path.GetFullPath("./credentials_example.json")}");
try
{ {
var cm = JsonConvert.DeserializeObject<CredentialsModel>(File.ReadAllText("./credentials.json")); var configBuilder = new ConfigurationBuilder();
Token = cm.Token; configBuilder.AddJsonFile(credsFileName)
OwnerIds = cm.OwnerIds; .AddEnvironmentVariables("NadekoBot:");
LoLApiKey = cm.LoLApiKey;
GoogleApiKey = cm.GoogleApiKey; var data = configBuilder.Build();
MashapeKey = cm.MashapeKey;
OsuApiKey = cm.OsuApiKey; Token = data[nameof(Token)];
TotalShards = cm.TotalShards < 1 ? 1 : cm.TotalShards; if (string.IsNullOrWhiteSpace(Token))
BotId = cm.BotId ?? cm.ClientId; throw new ArgumentNullException(nameof(Token), "Token is missing from credentials.json or Environment varibles.");
ClientId = cm.ClientId; OwnerIds = data.GetSection("OwnerIds").GetChildren().Select(c => ulong.Parse(c.Value)).ToArray();
SoundCloudClientId = cm.SoundCloudClientId; LoLApiKey = data[nameof(LoLApiKey)];
CarbonKey = cm.CarbonKey; GoogleApiKey = data[nameof(GoogleApiKey)];
if (cm.Db == null) MashapeKey = data[nameof(MashapeKey)];
Db = new DB("sqlite", ""); OsuApiKey = data[nameof(OsuApiKey)];
else
Db = new DB(cm.Db.Type, cm.Db.ConnectionString); int ts = 1;
int.TryParse(data[nameof(TotalShards)], out ts);
TotalShards = ts < 1 ? 1 : ts;
ulong clId = 0;
ulong.TryParse(data[nameof(ClientId)], out clId);
ClientId = ulong.Parse(data[nameof(ClientId)]);
SoundCloudClientId = data[nameof(SoundCloudClientId)];
CarbonKey = data[nameof(CarbonKey)];
var dbSection = data.GetSection("db");
Db = new DB(string.IsNullOrWhiteSpace(dbSection["Type"])
? "sqlite"
: dbSection["Type"],
string.IsNullOrWhiteSpace(dbSection["ConnectionString"])
? "Filename=./data/NadekoBot.db"
: dbSection["ConnectionString"]);
} }
else catch (Exception ex)
{ {
_log.Fatal($"credentials.json is missing. Failed to start. Example is in {Path.GetFullPath("./credentials_example.json")}"); _log.Warn(ex);
throw new FileNotFoundException(); throw;
} }
} }
@ -66,7 +87,6 @@ namespace NadekoBot.Services.Impl
private class CredentialsModel private class CredentialsModel
{ {
public ulong ClientId { get; set; } = 123123123; public ulong ClientId { get; set; } = 123123123;
public ulong? BotId { get; set; }
public string Token { get; set; } = ""; public string Token { get; set; } = "";
public ulong[] OwnerIds { get; set; } = new ulong[1]; public ulong[] OwnerIds { get; set; } = new ulong[1];
public string LoLApiKey { get; set; } = ""; public string LoLApiKey { get; set; } = "";
@ -75,7 +95,7 @@ namespace NadekoBot.Services.Impl
public string OsuApiKey { get; set; } = ""; public string OsuApiKey { get; set; } = "";
public string SoundCloudClientId { get; set; } = ""; public string SoundCloudClientId { get; set; } = "";
public string CarbonKey { get; set; } = ""; public string CarbonKey { get; set; } = "";
public DB Db { get; set; } public DB Db { get; set; } = new DB("sqlite", "Filename=./data/NadekoBot.db");
public int TotalShards { get; set; } = 1; public int TotalShards { get; set; } = 1;
} }

View File

@ -1,6 +1,5 @@
{ {
"ClientId": 123123123, "ClientId": 123123123,
"BotId": null,
"Token": "", "Token": "",
"OwnerIds": [ "OwnerIds": [
0 0
@ -11,6 +10,9 @@
"OsuApiKey": "", "OsuApiKey": "",
"SoundCloudClientId": "", "SoundCloudClientId": "",
"CarbonKey": "", "CarbonKey": "",
"Db": null, "Db": {
"Type": "sqlite",
"ConnectionString": "Filename=./data/NadekoBot.db"
},
"TotalShards": 1 "TotalShards": 1
} }

View File

@ -42,7 +42,10 @@
"Discord.Net": { "Discord.Net": {
"target": "project" "target": "project"
}, },
"System.Xml.XPath": "4.0.1" "System.Xml.XPath": "4.0.1",
"Microsoft.Extensions.Configuration": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0"
}, },
"tools": { "tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"