diff --git a/FacadeHeadlessApp/Facade.cs b/FacadeHeadlessApp/Facade.cs index f99f2c4..fdcc123 100644 --- a/FacadeHeadlessApp/Facade.cs +++ b/FacadeHeadlessApp/Facade.cs @@ -8,6 +8,7 @@ using Golem.Tools.ViewModels; using Microsoft.Extensions.Logging; +using Golem.Tools; namespace FacadeHeadlessApp; @@ -21,6 +22,10 @@ public class FacadeAppArguments public required RelayType Relay { get; set; } [Option('m', "mainnet", Default = false, Required = false, HelpText = "Enables usage of mainnet")] public bool Mainnet { get; set; } + [Option('w', "wallet", Required = false, HelpText = "Wallet address to receive funds")] + public string? Wallet { get; set; } + [Option('i', "interactive", Default = false, HelpText = "Enable interactive console mode")] + public bool Interactive { get; set; } } @@ -44,39 +49,49 @@ await GolemViewModel.Load(golemPath, args.Relay, args.Mainnet) : await GolemViewModel.CreateStatic(golemPath, args.Relay, args.Mainnet); var golem = view.Golem; + if (args.Wallet != null) + golem.WalletAddress = args.Wallet; golem.PropertyChanged += new PropertyChangedHandler(logger).For(nameof(IGolem.Status)); - bool end = false; - do + if (args.Interactive) { - Console.WriteLine("Start/Stop/End?"); - var line = Console.ReadLine(); + bool end = false; - switch (line) + do { - case "Start": - await golem.Start(); - break; - case "Stop": - await golem.Stop(); - break; - case "End": - end = true; - break; - - case "Wallet": - var walletAddress = golem.WalletAddress; - golem.WalletAddress = walletAddress; - Console.WriteLine($"Wallet: {walletAddress}"); - break; - - default: Console.WriteLine($"Didn't understand: {line}"); break; - } - } while (!end); - - - Console.WriteLine("Done"); + Console.WriteLine("Start/Stop/End?"); + var line = Console.ReadLine(); + + switch (line) + { + case "Start": + await golem.Start(); + break; + case "Stop": + await golem.Stop(); + break; + case "End": + end = true; + break; + case "Wallet": + var walletAddress = golem.WalletAddress; + golem.WalletAddress = walletAddress; + Console.WriteLine($"Wallet: {walletAddress}"); + break; + + default: Console.WriteLine($"Didn't understand: {line}"); break; + } + } while (!end); + + + Console.WriteLine("Done"); + } + else + { + await golem.Start(); + ConsoleHelper.WaitForCtrlC(); + } } } @@ -85,34 +100,34 @@ public class PropertyChangedHandler public PropertyChangedHandler(ILogger logger) { - this.logger = logger; + this._logger = logger; } - readonly ILogger logger; + readonly ILogger _logger; public PropertyChangedEventHandler For(string name) { - switch (name) + return name switch { - case "Status": return Status_PropertyChangedHandler; - case "Activities": return Activities_PropertyChangedHandler; - default: return Empty_PropertyChangedHandler; - } + "Status" => Status_PropertyChangedHandler, + "Activities" => Activities_PropertyChangedHandler, + _ => Empty_PropertyChangedHandler, + }; } private void Status_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e) { - if (sender is Golem.Golem golem && e.PropertyName != "Status") - logger.LogInformation($"Status property has changed: {e.PropertyName} to {golem.Status}"); + if (sender is Golem.Golem golem && e.PropertyName == "Status") + _logger.LogInformation($"Status property has changed: {e.PropertyName} to {golem.Status}"); } private void Activities_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e) { if (sender is Golem.Golem golem && e.PropertyName != "Activities") - logger.LogInformation($"Activities property has changed: {e.PropertyName}. Current job: {golem.CurrentJob}"); + _logger.LogInformation($"Activities property has changed: {e.PropertyName}. Current job: {golem.CurrentJob}"); } private void Empty_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e) { - logger.LogInformation($"Property {e} is not supported in this context"); + _logger.LogInformation($"Property {e} is not supported in this context"); } } diff --git a/Golem.Tools/Console.cs b/Golem.Tools/Console.cs new file mode 100644 index 0000000..918fbf7 --- /dev/null +++ b/Golem.Tools/Console.cs @@ -0,0 +1,20 @@ + +namespace Golem.Tools; + + +public class ConsoleHelper +{ + public static void WaitForCtrlC() + { + Console.TreatControlCAsInput = true; + + ConsoleKeyInfo cki; + do + { + cki = Console.ReadKey(); + } while (!(((cki.Modifiers & ConsoleModifiers.Control) != 0) && (cki.Key == ConsoleKey.C))); + } +} + + + diff --git a/example/ExampleRunner/Program.cs b/example/ExampleRunner/Program.cs index 13287a8..5e55224 100644 --- a/example/ExampleRunner/Program.cs +++ b/example/ExampleRunner/Program.cs @@ -4,7 +4,7 @@ using Golem; -using GolemLib; +using Golem.Tools; using Microsoft.Extensions.Logging; @@ -63,7 +63,7 @@ static void Main(string[] args) logger.LogInformation("Press Ctrl+C To Terminate"); - waitForCtrlC(); + ConsoleHelper.WaitForCtrlC(); Task[] tasks = new Task[2]; tasks[0] = Task.Run(() => @@ -75,7 +75,7 @@ static void Main(string[] args) tasks[1] = Task.Run(() => { - waitForCtrlC(); + ConsoleHelper.WaitForCtrlC(); logger.LogInformation("Captured second Ctrl-C. Killing..."); App.Kill().Wait(100); @@ -84,15 +84,4 @@ static void Main(string[] args) Task.WaitAny(tasks); } - - static void waitForCtrlC() - { - Console.TreatControlCAsInput = true; - - ConsoleKeyInfo cki; - do - { - cki = Console.ReadKey(); - } while (!(((cki.Modifiers & ConsoleModifiers.Control) != 0) && (cki.Key == ConsoleKey.C))); - } }