Add commandline parser

This commit is contained in:
Runebaas 2018-05-02 20:19:11 +02:00
parent d3fcfc8a5c
commit 2701f641cf
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
5 changed files with 44 additions and 13 deletions

View file

@ -11,6 +11,7 @@
<NoWarn>NU1701</NoWarn> <NoWarn>NU1701</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="Discord.Net"> <PackageReference Include="Discord.Net">
<Version>1.0.2</Version> <Version>1.0.2</Version>
</PackageReference> </PackageReference>

View file

@ -9,10 +9,10 @@ namespace Geekbot.net.Lib
private readonly NLog.Logger _logger; private readonly NLog.Logger _logger;
private readonly JsonSerializerSettings _serializerSettings; private readonly JsonSerializerSettings _serializerSettings;
public GeekbotLogger(bool sumologicActive) public GeekbotLogger(RunParameters runParameters, bool sumologicActive)
{ {
_sumologicActive = sumologicActive; _sumologicActive = sumologicActive;
_logger = LoggerFactory.CreateNLog(sumologicActive); _logger = LoggerFactory.CreateNLog(runParameters, sumologicActive);
_serializerSettings = new JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
ReferenceLoopHandling = ReferenceLoopHandling.Serialize, ReferenceLoopHandling = ReferenceLoopHandling.Serialize,

View file

@ -9,7 +9,7 @@ namespace Geekbot.net.Lib
{ {
public class LoggerFactory public class LoggerFactory
{ {
public static Logger CreateNLog(bool sumologicActive) public static Logger CreateNLog(RunParameters runParameters, bool sumologicActive)
{ {
var config = new LoggingConfiguration(); var config = new LoggingConfiguration();
@ -36,8 +36,9 @@ namespace Geekbot.net.Lib
} }
else else
{ {
var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info;
config.LoggingRules.Add( config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, new LoggingRule("*", minLevel, LogLevel.Fatal,
new ColoredConsoleTarget new ColoredConsoleTarget
{ {
Name = "Console", Name = "Console",
@ -47,7 +48,7 @@ namespace Geekbot.net.Lib
); );
config.LoggingRules.Add( config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, new LoggingRule("*", minLevel, LogLevel.Fatal,
new FileTarget new FileTarget
{ {
Name = "File", Name = "File",

View file

@ -0,0 +1,23 @@
using System;
using CommandLine;
namespace Geekbot.net.Lib
{
public class RunParameters
{
[Option('V', "verbose", Default = false, HelpText = "Prints all messages to standard output.")]
public bool Verbose { get; set; }
[Option('r', "reset", Default = false, HelpText = "Resets the bot")]
public bool Reset { get; set; }
[Option('j', "log-json", Default = false, HelpText = "Logs messages as json")]
public bool LogJson { get; set; }
[Option("disable-api", Default = false, HelpText = "Disables the web api")]
public bool DisableApi { get; set; }
[Option('e', "expose-errors", Default = false, HelpText = "Shows internal errors in the chat")]
public bool ExposeErrors { get; set; }
}
}

View file

@ -4,6 +4,7 @@ using System.Net;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommandLine;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
@ -26,11 +27,16 @@ namespace Geekbot.net
private RedisValue _token; private RedisValue _token;
private IGeekbotLogger _logger; private IGeekbotLogger _logger;
private IUserRepository _userRepository; private IUserRepository _userRepository;
private string[] _args;
private bool _firstStart; private bool _firstStart;
private RunParameters _runParameters;
private static void Main(string[] args) private static void Main(string[] args)
{ {
RunParameters runParameters = null;
Parser.Default.ParseArguments<RunParameters>(args)
.WithParsed(e => runParameters = e)
.WithNotParsed(_ => Environment.Exit(1));
var logo = new StringBuilder(); var logo = new StringBuilder();
logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____"); logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____");
logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|"); logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|");
@ -40,11 +46,11 @@ namespace Geekbot.net
logo.AppendLine("========================================="); logo.AppendLine("=========================================");
Console.WriteLine(logo.ToString()); Console.WriteLine(logo.ToString());
var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"));
var logger = new GeekbotLogger(sumologicActive); var logger = new GeekbotLogger(runParameters, sumologicActive);
logger.Information("Geekbot", "Starting..."); logger.Information("Geekbot", "Starting...");
try try
{ {
new Program().MainAsync(args, logger).GetAwaiter().GetResult(); new Program().MainAsync(runParameters, logger).GetAwaiter().GetResult();
} }
catch (Exception e) catch (Exception e)
{ {
@ -52,10 +58,10 @@ namespace Geekbot.net
} }
} }
private async Task MainAsync(string[] args, IGeekbotLogger logger) private async Task MainAsync(RunParameters runParameters, IGeekbotLogger logger)
{ {
_logger = logger; _logger = logger;
_args = args; _runParameters = runParameters;
logger.Information("Geekbot", "Initing Stuff"); logger.Information("Geekbot", "Initing Stuff");
_client = new DiscordSocketClient(new DiscordSocketConfig _client = new DiscordSocketClient(new DiscordSocketConfig
@ -132,7 +138,7 @@ namespace Geekbot.net
_logger.Information("Geekbot", "Registering Stuff"); _logger.Information("Geekbot", "Registering Stuff");
var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger); var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger);
var errorHandler = new ErrorHandler(_logger, translationHandler, _args.Contains("--expose-errors")); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors);
var reactionListener = new ReactionListener(_redis); var reactionListener = new ReactionListener(_redis);
await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
_services.AddSingleton(_commands); _services.AddSingleton(_commands);
@ -153,13 +159,13 @@ namespace Geekbot.net
_client.ReactionAdded += handlers.ReactionAdded; _client.ReactionAdded += handlers.ReactionAdded;
_client.ReactionRemoved += handlers.ReactionRemoved; _client.ReactionRemoved += handlers.ReactionRemoved;
if (_firstStart || _args.Contains("--reset")) if (_firstStart || _runParameters.Reset)
{ {
_logger.Information("Geekbot", "Finishing setup"); _logger.Information("Geekbot", "Finishing setup");
await FinishSetup(); await FinishSetup();
_logger.Information("Geekbot", "Setup finished"); _logger.Information("Geekbot", "Setup finished");
} }
if (!_args.Contains("--disable-api")) if (!_runParameters.DisableApi)
{ {
StartWebApi(); StartWebApi();
} }