Skip to content

Commit fab8e6b

Browse files
committed
Add console stop and version commands
Introduced new commands for stopping the FlowSynx Console and displaying its version. Updated `ConsoleCommand` to include options for background execution and address specification. Modified `ConsoleCommandOptions` to support these new properties. Added `ConsoleStopCommand` along with its handler and options classes. Included new localized strings for user interface support. #108
1 parent 14c87da commit fab8e6b

8 files changed

Lines changed: 126 additions & 7 deletions
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FlowCtl.Commands.Console.Version;
1+
using FlowCtl.Commands.Console.Stop;
2+
using FlowCtl.Commands.Console.Version;
23
using System.CommandLine;
34

45
namespace FlowCtl.Commands.Console;
@@ -7,10 +8,17 @@ internal class ConsoleCommand : BaseCommand<ConsoleCommandOptions, ConsoleComman
78
{
89
public ConsoleCommand() : base("console", Resources.Command_Console_Description)
910
{
11+
var backgroundOption = new Option<bool>(new[] { "-b", "--background" },
12+
getDefaultValue: () => false,
13+
description: Resources.Command_Console_BackgroundOption);
14+
1015
var addressOption = new Option<string?>(new[] { "-a", "--address" },
1116
description: Resources.Commands_FlowSynxAddress);
1217

18+
AddOption(backgroundOption);
1319
AddOption(addressOption);
20+
21+
AddCommand(new CosnoleStopCommand());
1422
AddCommand(new ConsoleVersionCommand());
1523
}
1624
}

src/FlowCtl/Commands/Console/ConsoleCommandOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
internal class ConsoleCommandOptions : ICommandOptions
44
{
5+
public bool Background { get; set; } = false;
56
public string? Address { get; set; } = string.Empty;
67
}

src/FlowCtl/Commands/Console/ConsoleCommandOptionsHandler.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using FlowCtl.Core.Services.Location;
22
using FlowCtl.Core.Services.Logger;
33
using System.Diagnostics;
4-
using System.Runtime.InteropServices;
54

65
namespace FlowCtl.Commands.Console;
76

@@ -46,13 +45,21 @@ private Task Execute(ConsoleCommandOptions options)
4645
};
4746

4847
var process = new Process { StartInfo = startInfo };
49-
process.OutputDataReceived += OutputDataHandler;
50-
process.ErrorDataReceived += ErrorDataHandler;
48+
49+
if (!options.Background)
50+
{
51+
process.OutputDataReceived += OutputDataHandler;
52+
process.ErrorDataReceived += ErrorDataHandler;
53+
}
54+
5155
var processStarted = process.Start();
5256

53-
process.BeginOutputReadLine();
54-
process.BeginErrorReadLine();
55-
process?.WaitForExit();
57+
if (!options.Background)
58+
{
59+
process.BeginOutputReadLine();
60+
process.BeginErrorReadLine();
61+
process?.WaitForExit();
62+
}
5663
}
5764
catch (Exception e)
5865
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FlowCtl.Commands.Console.Stop;
2+
3+
internal class CosnoleStopCommand : BaseCommand<ConsoleStopCommandOptions, ConsoleStopCommandOptionsHandler>
4+
{
5+
public CosnoleStopCommand() : base("stop", Resources.Commands_StopConsole_Description)
6+
{
7+
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace FlowCtl.Commands.Console.Stop;
2+
3+
internal class ConsoleStopCommandOptions : ICommandOptions {}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Diagnostics;
2+
using FlowCtl.Core.Services.Location;
3+
using FlowCtl.Core.Services.Logger;
4+
5+
namespace FlowCtl.Commands.Console.Stop;
6+
7+
internal class ConsoleStopCommandOptionsHandler : ICommandOptionsHandler<ConsoleStopCommandOptions>
8+
{
9+
private readonly IFlowCtlLogger _flowCtlLogger;
10+
private readonly ILocation _location;
11+
12+
public ConsoleStopCommandOptionsHandler(IFlowCtlLogger flowCtlLogger, ILocation location)
13+
{
14+
_flowCtlLogger = flowCtlLogger ?? throw new ArgumentNullException(nameof(flowCtlLogger));
15+
_location = location ?? throw new ArgumentNullException(nameof(location));
16+
}
17+
18+
public async Task<int> HandleAsync(ConsoleStopCommandOptions options, CancellationToken cancellationToken)
19+
{
20+
await Execute();
21+
return 0;
22+
}
23+
24+
private Task Execute()
25+
{
26+
try
27+
{
28+
TerminateProcess(_location.ConsoleBinaryName, ".");
29+
}
30+
catch (Exception e)
31+
{
32+
_flowCtlLogger.WriteError(e.Message);
33+
}
34+
return Task.CompletedTask;
35+
}
36+
37+
private void TerminateProcess(string processName, string machineAddress)
38+
{
39+
var processes = Process.GetProcessesByName(processName, machineAddress);
40+
41+
if (processes.Length == 0) return;
42+
43+
try
44+
{
45+
foreach (var process in processes)
46+
process.Kill();
47+
48+
_flowCtlLogger.Write(Resources.Commands_Stop_StopConsoleSuccessfully);
49+
}
50+
catch (Exception ex)
51+
{
52+
_flowCtlLogger.WriteError(ex.Message);
53+
}
54+
}
55+
}

src/FlowCtl/Resources.Designer.cs

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FlowCtl/Resources.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@
306306
<data name="Commands_Run_FlowSynxIsNotInstalled" xml:space="preserve">
307307
<value>The FlowSynx system is not installed. Please run the 'synx init -h' command for more details.</value>
308308
</data>
309+
<data name="Commands_StopConsole_Description" xml:space="preserve">
310+
<value>Stop the FlowSynx Console running under the current user profile.</value>
311+
</data>
309312
<data name="Commands_Stop_Description" xml:space="preserve">
310313
<value>Stop the FlowSynx system and its associated applications running under the current user profile.</value>
311314
</data>
@@ -480,12 +483,18 @@
480483
<data name="Command_ConsoleVersion_Description" xml:space="preserve">
481484
<value>Display the version for FlowSynx Console</value>
482485
</data>
486+
<data name="Command_Console_BackgroundOption" xml:space="preserve">
487+
<value>Run the console in the background</value>
488+
</data>
483489
<data name="Command_Console_ConsleIsNotInstalled" xml:space="preserve">
484490
<value>FlowSynx Console is not installed. Please run the 'synx init -h' command to see the details.</value>
485491
</data>
486492
<data name="Command_Console_Description" xml:space="preserve">
487493
<value>Run and execute the FlowSynx console</value>
488494
</data>
495+
<data name="Command_Run_BackgroundOption" xml:space="preserve">
496+
<value>Run the FlowSynx system in the background</value>
497+
</data>
489498
<data name="Command_Workflow_AddCommand_FileNotExist" xml:space="preserve">
490499
<value>The provided definition file '{0}' does not exist or could not be located.</value>
491500
</data>

0 commit comments

Comments
 (0)