Skip to content

Commit

Permalink
Add non-interactive mode; Ability to set wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Sep 16, 2024
1 parent 4452bd1 commit ce0c710
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 52 deletions.
91 changes: 53 additions & 38 deletions FacadeHeadlessApp/Facade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Golem.Tools.ViewModels;

using Microsoft.Extensions.Logging;
using Golem.Tools;

namespace FacadeHeadlessApp;

Expand All @@ -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; }
}


Expand All @@ -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();
}
}
}

Expand All @@ -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");
}
}
20 changes: 20 additions & 0 deletions Golem.Tools/Console.cs
Original file line number Diff line number Diff line change
@@ -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)));
}
}



17 changes: 3 additions & 14 deletions example/ExampleRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using Golem;

using GolemLib;
using Golem.Tools;

using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -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(() =>
Expand All @@ -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);
Expand All @@ -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)));
}
}

0 comments on commit ce0c710

Please sign in to comment.