diff --git a/Commands/GitRequiredInterceptor.cs b/Commands/GitRequiredInterceptor.cs new file mode 100644 index 0000000..2ce52f4 --- /dev/null +++ b/Commands/GitRequiredInterceptor.cs @@ -0,0 +1,52 @@ +using CliWrap; +using CliWrap.Buffered; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace FCPModUpdater.Commands; + +public sealed class GitRequiredInterceptor : ICommandInterceptor +{ + public void Intercept(CommandContext context, CommandSettings settings) + { + if (IsGitInstalled()) return; + + // It wasn't installed.. + AnsiConsole.MarkupLine("[yellow]This application requires Git to manage mods.[/]"); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("Install Git from: [link]https://git-scm.com/downloads[/]"); + + throw new GitNotFoundException(); + } + + private static bool IsGitInstalled() + { + try + { + var result = Cli.Wrap("git") + .WithArguments("--version") + .WithValidation(CommandResultValidation.None) + .ExecuteBufferedAsync() + .GetAwaiter() + .GetResult(); + + return result.ExitCode == 0; + } + catch + { + return false; + } + } +} + +public class GitNotFoundException : Exception +{ + public GitNotFoundException() + : base("Git is not installed or not found in PATH.") { } + + public GitNotFoundException(string message) + : base(message) { } + + public GitNotFoundException(string message, Exception inner) + : base(message, inner) { } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 0cd509d..7aaf867 100644 --- a/Program.cs +++ b/Program.cs @@ -9,6 +9,7 @@ app.Configure(config => { config.SetApplicationName("fcp-mod-manager"); config.SetApplicationVersion("1.0.0"); + config.SetInterceptor(new GitRequiredInterceptor()); config.AddCommand("scan") .WithDescription("Scan mods and show interactive menu (DEFAULT)")