Allow configuration to be passed as environment variables
This commit is contained in:
parent
d91c21e607
commit
8018d5e750
2 changed files with 70 additions and 34 deletions
|
@ -1,4 +1,5 @@
|
||||||
using CommandLine;
|
using System;
|
||||||
|
using CommandLine;
|
||||||
|
|
||||||
namespace Geekbot.net.Lib
|
namespace Geekbot.net.Lib
|
||||||
{
|
{
|
||||||
|
@ -8,56 +9,89 @@ namespace Geekbot.net.Lib
|
||||||
* General *
|
* General *
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
[Option('V', "verbose", Default = false, HelpText = "Logs everything.")]
|
[Option("token", HelpText = "Set a new bot token. By default it will use your previous bot token which was stored in the database (default: null) (env: TOKEN)")]
|
||||||
public bool Verbose { get; set; }
|
public string Token { get; set; } = ParamFallback("TOKEN");
|
||||||
|
|
||||||
[Option('j', "log-json", Default = false, HelpText = "Logger outputs json")]
|
[Option('V', "verbose", HelpText = "Logs everything. (default: false) (env: LOG_VERBOSE)")]
|
||||||
public bool LogJson { get; set; }
|
public bool Verbose { get; set; } = ParamFallback("LOG_VERBOSE", false);
|
||||||
|
|
||||||
[Option('a', "disable-api", Default = false, HelpText = "Disables the web api")]
|
[Option('j', "log-json", HelpText = "Logger outputs json (default: false ) (env: LOG_JSON)")]
|
||||||
public bool DisableApi { get; set; }
|
public bool LogJson { get; set; } = ParamFallback("LOG_JSON", false);
|
||||||
|
|
||||||
|
[Option('e', "expose-errors", HelpText = "Shows internal errors in the chat (default: false) (env: EXPOSE_ERRORS)")]
|
||||||
|
public bool ExposeErrors { get; set; } = ParamFallback("EXPOSE_ERRORS", false);
|
||||||
|
|
||||||
[Option('e', "expose-errors", Default = false, HelpText = "Shows internal errors in the chat")]
|
|
||||||
public bool ExposeErrors { get; set; }
|
|
||||||
|
|
||||||
[Option("token", Default = null, HelpText = "Set a new bot token")]
|
|
||||||
public string Token { get; set; }
|
|
||||||
|
|
||||||
/************************************
|
/************************************
|
||||||
* Database *
|
* Database *
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
[Option("in-memory", Default = false, HelpText = "Uses the in-memory database instead of postgresql")]
|
[Option("in-memory", HelpText = "Uses the in-memory database instead of postgresql (default: false) (env: DB_INMEMORY)")]
|
||||||
public bool InMemory { get; set; }
|
public bool InMemory { get; set; } = ParamFallback("DB_INMEMORY", false);
|
||||||
|
|
||||||
// Postresql connection
|
// Postresql connection
|
||||||
[Option("database", Default = "geekbot", HelpText = "Select a postgresql database")]
|
[Option("database", HelpText = "Select a postgresql database (default: geekbot) (env: DB_DATABASE)")]
|
||||||
public string DbDatabase { get; set; }
|
public string DbDatabase { get; set; } = ParamFallback("DB_DATABASE", "geekbot");
|
||||||
|
|
||||||
[Option("db-host", Default = "localhost", HelpText = "Set a postgresql host (e.g. 127.0.0.1)")]
|
[Option("db-host", HelpText = "Set a postgresql host (default: localhost) (env: DB_HOST)")]
|
||||||
public string DbHost { get; set; }
|
public string DbHost { get; set; } = ParamFallback("DB_HOST", "localhost");
|
||||||
|
|
||||||
[Option("db-port", Default = "5432", HelpText = "Set a postgresql host (e.g. 5432)")]
|
[Option("db-port", HelpText = "Set a postgresql host (default: 5432) (env: DB_PORT)")]
|
||||||
public string DbPort { get; set; }
|
public string DbPort { get; set; } = ParamFallback("DB_PORT", "5432");
|
||||||
|
|
||||||
[Option("db-user", Default = "geekbot", HelpText = "Set a postgresql user")]
|
[Option("db-user", HelpText = "Set a postgresql user (default: geekbot) (env: DB_USER)")]
|
||||||
public string DbUser { get; set; }
|
public string DbUser { get; set; } = ParamFallback("DB_USER", "geekbot");
|
||||||
|
|
||||||
[Option("db-password", Default = "", HelpText = "Set a posgresql password")]
|
[Option("db-password", HelpText = "Set a posgresql password (default: empty) (env: DB_PASSWORD)")]
|
||||||
public string DbPassword { get; set; }
|
public string DbPassword { get; set; } = ParamFallback("DB_PASSWORD", "");
|
||||||
|
|
||||||
// Logging
|
// Logging
|
||||||
[Option("db-logging", Default = false, HelpText = "Enable database logging")]
|
[Option("db-logging", HelpText = "Enable database logging (default: false) (env: DB_LOGGING)")]
|
||||||
public bool DbLogging { get; set; }
|
public bool DbLogging { get; set; } = ParamFallback("DB_LOGGING", false);
|
||||||
|
|
||||||
/************************************
|
/************************************
|
||||||
* WebApi *
|
* WebApi *
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
[Option("api-host", Default = "localhost", HelpText = "Host on which the WebApi listens")]
|
|
||||||
public string ApiHost { get; set; }
|
|
||||||
|
|
||||||
[Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")]
|
[Option('a', "disable-api", HelpText = "Disables the WebApi (default: false) (env: API_DISABLE)")]
|
||||||
public string ApiPort { get; set; }
|
public bool DisableApi { get; set; } = ParamFallback("API_DISABLE", false);
|
||||||
|
|
||||||
|
[Option("api-host", HelpText = "Host on which the WebApi listens (default: localhost) (env: API_HOST)")]
|
||||||
|
public string ApiHost { get; set; } = ParamFallback("API_HOST", "localhost");
|
||||||
|
|
||||||
|
[Option("api-port", HelpText = "Port on which the WebApi listens (default: 12995) (env: API_PORT)")]
|
||||||
|
public string ApiPort { get; set; } = ParamFallback("API_PORT", "12995");
|
||||||
|
|
||||||
|
/************************************
|
||||||
|
* Helper Functions *
|
||||||
|
************************************/
|
||||||
|
|
||||||
|
private static string ParamFallback(string key, string defaultValue = null)
|
||||||
|
{
|
||||||
|
var envVar = GetEnvironmentVariable(key);
|
||||||
|
return !string.IsNullOrEmpty(envVar) ? envVar : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool ParamFallback(string key, bool defaultValue)
|
||||||
|
{
|
||||||
|
var envVar = GetEnvironmentVariable(key);
|
||||||
|
if (!string.IsNullOrEmpty(envVar))
|
||||||
|
{
|
||||||
|
return envVar.ToLower() switch
|
||||||
|
{
|
||||||
|
"true" => true,
|
||||||
|
"1" => true,
|
||||||
|
"false" => false,
|
||||||
|
"0" => false,
|
||||||
|
_ => defaultValue
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetEnvironmentVariable(string name)
|
||||||
|
{
|
||||||
|
return Environment.GetEnvironmentVariable($"GEEKBOT_{name}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,9 +18,11 @@ You can start geekbot with: `dotnet run`
|
||||||
|
|
||||||
On your first run geekbot will ask for your bot token.
|
On your first run geekbot will ask for your bot token.
|
||||||
|
|
||||||
You might need to pass some additional configuration (e.g. database credentials), these can be passed as commandline arguments.
|
You might need to pass some additional configuration (e.g. database credentials), these can be passed as commandline arguments or environment variables.
|
||||||
|
|
||||||
For a list of commandline arguments use `dotnet run -- -h`
|
For a list of commandline arguments and environment variables use `dotnet run -- -h`
|
||||||
|
|
||||||
|
All Environment Variables must be prefixed with `GEEKBOT_`
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue