Skip to content

Commit

Permalink
feat: refactor cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
tautcony committed Jan 20, 2025
1 parent 02da6fe commit 2b94f92
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 41 deletions.
16 changes: 6 additions & 10 deletions src/ISTA-Patcher/Commands/CerebrumancyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace ISTAPatcher.Commands;
using ISTAlter.Models.Rheingold.LicenseManagement;
using ISTAlter.Models.Rheingold.LicenseManagement.CoreFramework;
using ISTAlter.Utils;
using ISTAPatcher.Commands.Options;
using Serilog;

[CliCommand(
Expand All @@ -22,27 +23,22 @@ namespace ISTAPatcher.Commands;
ShortFormAutoGenerate = false,
Parent = typeof(RootCommand)
)]
public class CerebrumancyCommand : OptionalCommandBase
public class CerebrumancyCommand : OptionalPatchOption, ICommonPatchOption
{
[CliOption(Description = "Specify the verbosity level of the output.")]
public Serilog.Events.LogEventLevel Verbosity { get; set; } = Serilog.Events.LogEventLevel.Information;
public RootCommand ParentCommand { get; set; }

[CliOption(Description = "Restore the patched files to their original state.")]
public bool Restore { get; set; }

[CliOption(Description = "Specify the maximum degree of parallelism for patching.")]
public int MaxDegreeOfParallelism { get; set; } = Environment.ProcessorCount;

[CliOption(Name = "--type", Description = "Specify the patch type.")]
public ISTAOptions.PatchType PatchType { get; set; } = ISTAOptions.PatchType.B;

[CliOption(Description = "Force patching on application and libraries.")]
public ISTAOptions.ModeType Mode { get; set; } = ISTAOptions.ModeType.Standalone;

public bool Force { get; set; }

[CliOption(Description = "Specify the libraries to skip patching.")]
public string[] SkipLibrary { get; set; } = [];

[CliArgument(Description = "Specify the path for ISTA-P.", Required = true)]
public string? TargetPath { get; set; }

[CliOption(Description = "Conduct mentalysing on the mystical stream.")]
Expand Down Expand Up @@ -82,7 +78,7 @@ public void Run()
{
var opts = new ISTAOptions.CerebrumancyOptions
{
Verbosity = this.Verbosity,
Verbosity = this.ParentCommand.Verbosity,
Restore = this.Restore,
ENET = this.Enet,
FinishedOperations = this.FinishedOperations,
Expand Down
5 changes: 2 additions & 3 deletions src/ISTA-Patcher/Commands/CryptoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ namespace ISTAPatcher.Commands;
)]
public class CryptoCommand
{
[CliOption(Description = "Specify the verbosity level of the output.")]
public Serilog.Events.LogEventLevel Verbosity { get; set; } = Serilog.Events.LogEventLevel.Information;
public RootCommand? ParentCommand { get; set; }

[CliOption(Description = "Decrypt the integrity checklist.")]
public bool Decrypt { get; set; }
Expand All @@ -49,7 +48,7 @@ public void Run()
{
var opts = new ISTAOptions.CryptoOptions
{
Verbosity = this.Verbosity,
Verbosity = this.ParentCommand!.Verbosity,
Decrypt = this.Decrypt,
Integrity = this.Integrity,
CreateKeyPair = this.CreateKeyPair,
Expand Down
31 changes: 31 additions & 0 deletions src/ISTA-Patcher/Commands/Options/ICommonPatchOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 TautCony

namespace ISTAPatcher.Commands.Options;

using DotMake.CommandLine;
using ISTAlter;

public interface ICommonPatchOption
{
[CliOption(Description = "Restore the patched files to their original state.")]
public bool Restore { get; set; }

[CliOption(Description = "Specify the maximum degree of parallelism for patching.")]
public int MaxDegreeOfParallelism { get; set; }

[CliOption(Name = "--type", Description = "Specify the patch type.")]
public ISTAOptions.PatchType PatchType { get; set; }

[CliOption(Description = "Specify the mode type.")]
public ISTAOptions.ModeType Mode { get; set; }

[CliOption(Description = "Force patching on application and libraries.")]
public bool Force { get; set; }

[CliOption(Description = "Specify the libraries to skip patching.")]
public string[] SkipLibrary { get; set; }

[CliArgument(Description = "Specify the path for ISTA-P.", Required = true)]
public string? TargetPath { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 TautCony

namespace ISTAPatcher.Commands;
namespace ISTAPatcher.Commands.Options;

using DotMake.CommandLine;

public class OptionalCommandBase
public class OptionalPatchOption
{
[CliOption(Name = "--enable-enet", Description = "Enable ENET programming functionality.")]
public bool Enet { get; set; }
Expand Down
24 changes: 8 additions & 16 deletions src/ISTA-Patcher/Commands/PatchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace ISTAPatcher.Commands;
using ISTAlter.Core;
using ISTAlter.Core.Patcher.Provider;
using ISTAlter.Utils;
using ISTAPatcher.Commands.Options;
using Serilog;
using Serilog.Events;

[CliCommand(
Name="patch",
Expand All @@ -19,40 +19,32 @@ namespace ISTAPatcher.Commands;
ShortFormAutoGenerate = false,
Parent = typeof(RootCommand)
)]
public class PatchCommand : OptionalCommandBase
public class PatchCommand : OptionalPatchOption, ICommonPatchOption
{
[CliOption(Description = "Specify the verbosity level of the output.")]
public LogEventLevel Verbosity { get; set; } = LogEventLevel.Information;
public RootCommand? ParentCommand { get; set; }

[CliOption(Description = "Restore the patched files to their original state.")]
public bool Restore { get; set; }

[CliOption(Description = "Specify the mode type.")]
public ISTAOptions.ModeType Mode { get; set; } = ISTAOptions.ModeType.Standalone;

[CliOption(Description = "Specify the maximum degree of parallelism for patching.")]
public int MaxDegreeOfParallelism { get; set; } = Environment.ProcessorCount;

[CliOption(Name="--type", Description = "Specify the patch type.")]
public ISTAOptions.PatchType PatchType { get; set; } = ISTAOptions.PatchType.B;

[CliOption(Description = "Generate a registry file.")]
public bool GenerateMockRegFile { get; set; }
public ISTAOptions.ModeType Mode { get; set; } = ISTAOptions.ModeType.Standalone;

[CliOption(Description = "Force patching on application and libraries.")]
public bool Force { get; set; }

[CliOption(Description = "Specify the libraries to skip patching.")]
public string[] SkipLibrary { get; set; } = [];

[CliArgument(Description = "Specify the path for ISTA-P.", Required = true)]
public string? TargetPath { get; set; }

[CliOption(Description = "Generate a registry file.")]
public bool GenerateMockRegFile { get; set; }

public void Run()
{
var opts = new ISTAOptions.PatchOptions
{
Verbosity = this.Verbosity,
Verbosity = this.ParentCommand!.Verbosity,
Restore = this.Restore,
ENET = this.Enet,
FinishedOperations = this.FinishedOperations,
Expand Down
13 changes: 11 additions & 2 deletions src/ISTA-Patcher/Commands/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
namespace ISTAPatcher.Commands;

using DotMake.CommandLine;
using Serilog.Events;

[CliCommand]
public class RootCommand;
[CliCommand(
NameCasingConvention = CliNameCasingConvention.KebabCase,
NamePrefixConvention = CliNamePrefixConvention.DoubleHyphen,
ShortFormAutoGenerate = false
)]
public class RootCommand
{
[CliOption(Description = "Specify the verbosity level of the output.")]
public Serilog.Events.LogEventLevel Verbosity { get; set; } = LogEventLevel.Information;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ namespace ISTAPatcher.Commands;
ShortFormAutoGenerate = false,
Parent = typeof(RootCommand)
)]
public class ILeanCommand
public class iLeanCommand
{
[CliOption(Description = "Specify the verbosity level of the output.")]
public Serilog.Events.LogEventLevel Verbosity { get; set; } = Serilog.Events.LogEventLevel.Information;
public RootCommand? ParentCommand { get; set; }

[CliOption(Description = "Specify the machine GUID.")]
public string? MachineGuid { get; set; }
Expand All @@ -32,21 +31,20 @@ public class ILeanCommand
[CliOption(Description = "Show the machine information.")]
public bool ShowMachineInfo { get; set; }

[CliOption(Description = "Encrypt the provided file.")]
[CliOption(Description = "Encrypt the provided file/content.")]
public string? Encrypt { get; set; }

[CliOption(Description = "Decrypt the provided file.")]
[CliOption(Description = "Decrypt the provided file/content.")]
public string? Decrypt { get; set; }

public void Run()
{
var opts = new ISTAOptions.ILeanOptions
{
Verbosity = this.Verbosity,
Verbosity = this.ParentCommand!.Verbosity,
MachineGuid = this.MachineGuid,
VolumeSerialNumber = this.VolumeSerialNumber,
ShowMachineInfo = this.ShowMachineInfo,
Encrypt = this.Encrypt,
Decrypt = this.Decrypt,
};

Expand Down
10 changes: 9 additions & 1 deletion src/ISTA-Patcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ internal static class Program
public static async Task<int> Main(string[] args)
{
Global.ServicesProvider.GetServices<IStartupTask>().Run();
return await Cli.RunAsync<RootCommand>(args, new CliSettings { Theme = CliTheme.Default });
var theme = new CliTheme
{
DefaultColor = ConsoleColor.White,
DefaultBgColor = null,
HeadingColor = ConsoleColor.Blue,
FirstColumnColor = ConsoleColor.Cyan,
SecondColumnColor = ConsoleColor.Green,
};
return await Cli.RunAsync<RootCommand>(args, new CliSettings { Theme = theme });
}
}

0 comments on commit 2b94f92

Please sign in to comment.