diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index d943a8e..cf7e5f9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -11,6 +11,7 @@ NU1701 + 1.0.2 diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index c4a792b..aae0978 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -9,10 +9,10 @@ namespace Geekbot.net.Lib private readonly NLog.Logger _logger; private readonly JsonSerializerSettings _serializerSettings; - public GeekbotLogger(bool sumologicActive) + public GeekbotLogger(RunParameters runParameters, bool sumologicActive) { _sumologicActive = sumologicActive; - _logger = LoggerFactory.CreateNLog(sumologicActive); + _logger = LoggerFactory.CreateNLog(runParameters, sumologicActive); _serializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index 8631c11..b0b5060 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -9,7 +9,7 @@ namespace Geekbot.net.Lib { public class LoggerFactory { - public static Logger CreateNLog(bool sumologicActive) + public static Logger CreateNLog(RunParameters runParameters, bool sumologicActive) { var config = new LoggingConfiguration(); @@ -36,8 +36,9 @@ namespace Geekbot.net.Lib } else { + var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info; config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, + new LoggingRule("*", minLevel, LogLevel.Fatal, new ColoredConsoleTarget { Name = "Console", @@ -47,7 +48,7 @@ namespace Geekbot.net.Lib ); config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, + new LoggingRule("*", minLevel, LogLevel.Fatal, new FileTarget { Name = "File", diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs new file mode 100644 index 0000000..0549ac5 --- /dev/null +++ b/Geekbot.net/Lib/RunParameters.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 10f0dcb..39e5e8b 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -4,6 +4,7 @@ using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; +using CommandLine; using Discord; using Discord.Commands; using Discord.WebSocket; @@ -26,11 +27,16 @@ namespace Geekbot.net private RedisValue _token; private IGeekbotLogger _logger; private IUserRepository _userRepository; - private string[] _args; private bool _firstStart; + private RunParameters _runParameters; private static void Main(string[] args) { + RunParameters runParameters = null; + Parser.Default.ParseArguments(args) + .WithParsed(e => runParameters = e) + .WithNotParsed(_ => Environment.Exit(1)); + var logo = new StringBuilder(); logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____"); logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|"); @@ -40,11 +46,11 @@ namespace Geekbot.net logo.AppendLine("========================================="); Console.WriteLine(logo.ToString()); var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); - var logger = new GeekbotLogger(sumologicActive); + var logger = new GeekbotLogger(runParameters, sumologicActive); logger.Information("Geekbot", "Starting..."); try { - new Program().MainAsync(args, logger).GetAwaiter().GetResult(); + new Program().MainAsync(runParameters, logger).GetAwaiter().GetResult(); } 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; - _args = args; + _runParameters = runParameters; logger.Information("Geekbot", "Initing Stuff"); _client = new DiscordSocketClient(new DiscordSocketConfig @@ -132,7 +138,7 @@ namespace Geekbot.net _logger.Information("Geekbot", "Registering Stuff"); 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); await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); _services.AddSingleton(_commands); @@ -153,13 +159,13 @@ namespace Geekbot.net _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; - if (_firstStart || _args.Contains("--reset")) + if (_firstStart || _runParameters.Reset) { _logger.Information("Geekbot", "Finishing setup"); await FinishSetup(); _logger.Information("Geekbot", "Setup finished"); } - if (!_args.Contains("--disable-api")) + if (!_runParameters.DisableApi) { StartWebApi(); }