From 7e1fd431f0d946cf561af5bd024729e26807ccf0 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Mon, 16 Dec 2024 17:40:14 +1100 Subject: [PATCH] Use output provider for Git and GitHub operations (#141) --- src/Stack/Commands/Branch/AddBranchCommand.cs | 5 +++-- src/Stack/Commands/Branch/NewBranchCommand.cs | 5 +++-- .../Commands/Branch/RemoveBranchCommand.cs | 5 +++-- .../PullRequests/CreatePullRequestsCommand.cs | 7 ++++--- .../PullRequests/OpenPullRequestsCommand.cs | 7 ++++--- .../Commands/Stack/CleanupStackCommand.cs | 8 +++++--- src/Stack/Commands/Stack/DeleteStackCommand.cs | 8 +++++--- src/Stack/Commands/Stack/ListStacksCommand.cs | 4 +++- src/Stack/Commands/Stack/NewStackCommand.cs | 6 ++++-- src/Stack/Commands/Stack/StackStatusCommand.cs | 7 ++++--- src/Stack/Commands/Stack/StackSwitchCommand.cs | 3 ++- src/Stack/Commands/Stack/UpdateStackCommand.cs | 8 +++++--- src/Stack/Git/GitHubOperations.cs | 15 ++++++++------- src/Stack/Git/GitOperations.cs | 18 +++++++++--------- 14 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/Stack/Commands/Branch/AddBranchCommand.cs b/src/Stack/Commands/Branch/AddBranchCommand.cs index 098453d..d397038 100644 --- a/src/Stack/Commands/Branch/AddBranchCommand.cs +++ b/src/Stack/Commands/Branch/AddBranchCommand.cs @@ -26,11 +26,12 @@ public override async Task ExecuteAsync(CommandContext context, AddBranchCo await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new AddBranchCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new AddBranchCommandInputs(settings.Stack, settings.Name)); diff --git a/src/Stack/Commands/Branch/NewBranchCommand.cs b/src/Stack/Commands/Branch/NewBranchCommand.cs index 5b241aa..00f9c96 100644 --- a/src/Stack/Commands/Branch/NewBranchCommand.cs +++ b/src/Stack/Commands/Branch/NewBranchCommand.cs @@ -30,11 +30,12 @@ public override async Task ExecuteAsync(CommandContext context, NewBranchCo await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new NewBranchCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new NewBranchCommandInputs(settings.Stack, settings.Name, settings.Force)); diff --git a/src/Stack/Commands/Branch/RemoveBranchCommand.cs b/src/Stack/Commands/Branch/RemoveBranchCommand.cs index 1e1d494..2bbd8da 100644 --- a/src/Stack/Commands/Branch/RemoveBranchCommand.cs +++ b/src/Stack/Commands/Branch/RemoveBranchCommand.cs @@ -30,11 +30,12 @@ public override async Task ExecuteAsync(CommandContext context, RemoveBranc await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new RemoveBranchCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new RemoveBranchCommandInputs(settings.Stack, settings.Name, settings.Force)); diff --git a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs index 5b5b132..9f28f50 100644 --- a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs @@ -20,12 +20,13 @@ public class CreatePullRequestsCommand : AsyncCommand ExecuteAsync(CommandContext context, CreatePullRequestsCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new CreatePullRequestsCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new FileOperations(), new StackConfig()); diff --git a/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs b/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs index fddc5a6..f1bf100 100644 --- a/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs @@ -20,12 +20,13 @@ public class OpenPullRequestsCommand : AsyncCommand ExecuteAsync(CommandContext context, OpenPullRequestsCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new OpenPullRequestsCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new OpenPullRequestsCommandInputs(settings.Name)); diff --git a/src/Stack/Commands/Stack/CleanupStackCommand.cs b/src/Stack/Commands/Stack/CleanupStackCommand.cs index 8d3b715..03f684d 100644 --- a/src/Stack/Commands/Stack/CleanupStackCommand.cs +++ b/src/Stack/Commands/Stack/CleanupStackCommand.cs @@ -27,11 +27,13 @@ public override async Task ExecuteAsync(CommandContext context, CleanupStac await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new CleanupStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new CleanupStackCommandInputs(settings.Name, settings.Force)); diff --git a/src/Stack/Commands/Stack/DeleteStackCommand.cs b/src/Stack/Commands/Stack/DeleteStackCommand.cs index 7f4133e..1061147 100644 --- a/src/Stack/Commands/Stack/DeleteStackCommand.cs +++ b/src/Stack/Commands/Stack/DeleteStackCommand.cs @@ -27,11 +27,13 @@ public override async Task ExecuteAsync(CommandContext context, DeleteStack await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new DeleteStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); var response = await handler.Handle(new DeleteStackCommandInputs(settings.Name, settings.Force)); diff --git a/src/Stack/Commands/Stack/ListStacksCommand.cs b/src/Stack/Commands/Stack/ListStacksCommand.cs index 57cd795..d259a48 100644 --- a/src/Stack/Commands/Stack/ListStacksCommand.cs +++ b/src/Stack/Commands/Stack/ListStacksCommand.cs @@ -3,6 +3,7 @@ using Spectre.Console.Cli; using Stack.Config; using Stack.Git; +using Stack.Infrastructure; namespace Stack.Commands; @@ -14,7 +15,8 @@ public override async Task ExecuteAsync(CommandContext context, ListStacksC { await Task.CompletedTask; var console = AnsiConsole.Console; - var gitOperations = new GitOperations(console, settings.GetGitOperationSettings()); + var outputProvider = new ConsoleOutputProvider(console); + var gitOperations = new GitOperations(outputProvider, settings.GetGitOperationSettings()); var stackConfig = new StackConfig(); var stacks = stackConfig.Load(); diff --git a/src/Stack/Commands/Stack/NewStackCommand.cs b/src/Stack/Commands/Stack/NewStackCommand.cs index c5fd5a5..619b1d8 100644 --- a/src/Stack/Commands/Stack/NewStackCommand.cs +++ b/src/Stack/Commands/Stack/NewStackCommand.cs @@ -40,10 +40,12 @@ public class NewStackCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, NewStackCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new NewStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); var response = await handler.Handle( diff --git a/src/Stack/Commands/Stack/StackStatusCommand.cs b/src/Stack/Commands/Stack/StackStatusCommand.cs index e5e1e88..8c2b66a 100644 --- a/src/Stack/Commands/Stack/StackStatusCommand.cs +++ b/src/Stack/Commands/Stack/StackStatusCommand.cs @@ -24,12 +24,13 @@ public class StackStatusCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, StackStatusCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new StackStatusCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new StackStatusCommandInputs(settings.Name, settings.All)); diff --git a/src/Stack/Commands/Stack/StackSwitchCommand.cs b/src/Stack/Commands/Stack/StackSwitchCommand.cs index ed206a9..082f808 100644 --- a/src/Stack/Commands/Stack/StackSwitchCommand.cs +++ b/src/Stack/Commands/Stack/StackSwitchCommand.cs @@ -20,10 +20,11 @@ public class StackSwitchCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, StackSwitchCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new StackSwitchCommandHandler( new ConsoleInputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new StackSwitchCommandInputs(settings.Branch)); diff --git a/src/Stack/Commands/Stack/UpdateStackCommand.cs b/src/Stack/Commands/Stack/UpdateStackCommand.cs index 1a0c8c8..b4ae374 100644 --- a/src/Stack/Commands/Stack/UpdateStackCommand.cs +++ b/src/Stack/Commands/Stack/UpdateStackCommand.cs @@ -25,11 +25,13 @@ public class UpdateStackCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, UpdateStackCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new UpdateStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new UpdateStackCommandInputs(settings.Name, settings.Force)); diff --git a/src/Stack/Git/GitHubOperations.cs b/src/Stack/Git/GitHubOperations.cs index a927109..e8957d8 100644 --- a/src/Stack/Git/GitHubOperations.cs +++ b/src/Stack/Git/GitHubOperations.cs @@ -1,6 +1,7 @@ using System.Text; using Octopus.Shellfish; using Spectre.Console; +using Stack.Infrastructure; namespace Stack.Git; @@ -48,7 +49,7 @@ public interface IGitHubOperations void OpenPullRequest(GitHubPullRequest pullRequest); } -public class GitHubOperations(IAnsiConsole console, GitHubOperationSettings settings) : IGitHubOperations +public class GitHubOperations(IOutputProvider outputProvider, GitHubOperationSettings settings) : IGitHubOperations { public GitHubPullRequest? GetPullRequest(string branch) { @@ -91,7 +92,7 @@ public void OpenPullRequest(GitHubPullRequest pullRequest) private string ExecuteGitHubCommandAndReturnOutput(string command) { if (settings.Verbose) - console.MarkupLine($"[grey]gh {command}[/]"); + outputProvider.Debug($"gh {command}"); var infoBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); @@ -106,13 +107,13 @@ private string ExecuteGitHubCommandAndReturnOutput(string command) if (result != 0) { - console.MarkupLine($"[red]{errorBuilder}[/]"); + outputProvider.Error($"{errorBuilder}"); throw new Exception("Failed to execute gh command."); } if (settings.Verbose && infoBuilder.Length > 0) { - console.MarkupLine($"[grey]{infoBuilder}[/]"); + outputProvider.Debug($"{infoBuilder}"); } return infoBuilder.ToString(); @@ -121,7 +122,7 @@ private string ExecuteGitHubCommandAndReturnOutput(string command) private void ExecuteGitHubCommand(string command) { if (settings.Verbose) - console.MarkupLine($"[grey]gh {command}[/]"); + outputProvider.Debug($"gh {command}"); if (!settings.DryRun) { @@ -144,14 +145,14 @@ private void ExecuteGitHubCommandInternal(string command) if (result != 0) { - console.MarkupLine($"[red]{errorBuilder}[/]"); + outputProvider.Error($"{errorBuilder}"); throw new Exception("Failed to execute gh command."); } else { if (infoBuilder.Length > 0) { - console.MarkupLine($"[grey]{infoBuilder}[/]"); + outputProvider.Debug($"{infoBuilder}"); } } } diff --git a/src/Stack/Git/GitOperations.cs b/src/Stack/Git/GitOperations.cs index e9a2aff..27be14a 100644 --- a/src/Stack/Git/GitOperations.cs +++ b/src/Stack/Git/GitOperations.cs @@ -1,7 +1,7 @@ using System.Diagnostics; using System.Text; using Octopus.Shellfish; -using Spectre.Console; +using Stack.Infrastructure; namespace Stack.Git; @@ -38,7 +38,7 @@ public interface IGitOperations void OpenFileInEditorAndWaitForClose(string path); } -public class GitOperations(IAnsiConsole console, GitOperationSettings settings) : IGitOperations +public class GitOperations(IOutputProvider outputProvider, GitOperationSettings settings) : IGitOperations { public void CreateNewBranch(string branchName, string sourceBranch) { @@ -165,7 +165,7 @@ public void OpenFileInEditorAndWaitForClose(string path) var editor = GetConfiguredEditor(); if (string.IsNullOrWhiteSpace(editor)) { - console.MarkupLine("[red]No editor is configured in git. Please configure an editor using 'git config --global core.editor '.[/]"); + outputProvider.Error("No editor is configured in git. Please configure an editor using 'git config --global core.editor '."); return; } @@ -185,7 +185,7 @@ public void OpenFileInEditorAndWaitForClose(string path) if (process == null) { - console.MarkupLine("[red]Failed to start editor process.[/]"); + outputProvider.Error("Failed to start editor process."); return; } @@ -195,7 +195,7 @@ public void OpenFileInEditorAndWaitForClose(string path) private string ExecuteGitCommandAndReturnOutput(string command) { if (settings.Verbose) - console.MarkupLine($"[grey]git {command}[/]"); + outputProvider.Debug($"git {command}"); var infoBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); @@ -210,13 +210,13 @@ private string ExecuteGitCommandAndReturnOutput(string command) if (result != 0) { - console.MarkupLine($"[red]{errorBuilder}[/]"); + outputProvider.Error($"{errorBuilder}"); throw new Exception("Failed to execute git command."); } if (settings.Verbose && infoBuilder.Length > 0) { - console.MarkupLine($"[grey]{infoBuilder}[/]"); + outputProvider.Debug($"{infoBuilder}"); } return infoBuilder.ToString(); @@ -227,7 +227,7 @@ private void ExecuteGitCommand(string command) if (settings.DryRun) { if (settings.Verbose) - console.MarkupLine($"[grey]git {command}[/]"); + outputProvider.Debug($"git {command}"); } else { @@ -239,7 +239,7 @@ private void ExecuteGitCommand(string command) // changes to the Git repository as the output might be important. // In verbose mode we would have already written the output // of the command so don't write it again. - console.MarkupLine($"[grey]{output}[/]"); + outputProvider.Debug($"{output}"); } } }