From 001929cad6811df5d91cccda9f1792a439f1f4c5 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Mon, 26 Aug 2024 22:08:06 +0100 Subject: [PATCH] Log working directory for command inputs (#575) * Log working directory for command inputs * ReleaseNotes.md --- src/ModularPipelines.Build/ReleaseNotes.md | 1 + src/ModularPipelines/Context/Command.cs | 30 +++++++++++-------- src/ModularPipelines/Logging/CommandLogger.cs | 12 ++++---- .../Logging/ICommandLogger.cs | 3 +- .../CommandLoggerTests.cs | 8 ++--- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/ModularPipelines.Build/ReleaseNotes.md b/src/ModularPipelines.Build/ReleaseNotes.md index e69de29bb2..a3223d7ac1 100644 --- a/src/ModularPipelines.Build/ReleaseNotes.md +++ b/src/ModularPipelines.Build/ReleaseNotes.md @@ -0,0 +1 @@ +* Show working directory when logging command inputs \ No newline at end of file diff --git a/src/ModularPipelines/Context/Command.cs b/src/ModularPipelines/Context/Command.cs index e3bee8d668..95a8cc89ec 100644 --- a/src/ModularPipelines/Context/Command.cs +++ b/src/ModularPipelines/Context/Command.cs @@ -66,7 +66,8 @@ public async Task ExecuteCommandLineTool(CommandLineToolOptions o exitCode: 0, runTime: TimeSpan.Zero, standardOutput: "Dummy Output Response", - standardError: "Dummy Error Response" + standardError: "Dummy Error Response", + command.WorkingDirPath ); return new CommandResult(command); @@ -116,10 +117,11 @@ private async Task Of(CliWrap.Command command, _commandLogger.Log(options: options, inputToLog: inputToLog, - result.ExitCode, - result.RunTime, - standardOutput, - standardError + exitCode: result.ExitCode, + runTime: result.RunTime, + standardOutput: standardOutput, + standardError: standardError, + commandWorkingDirPath: command.WorkingDirPath ); if (result.ExitCode != 0 && options.ThrowOnNonZeroExitCode) @@ -134,10 +136,11 @@ private async Task Of(CliWrap.Command command, { _commandLogger.Log(options: options, inputToLog: inputToLog, - e.ExitCode, - stopwatch.Elapsed, - standardOutput, - standardError + exitCode: e.ExitCode, + runTime: stopwatch.Elapsed, + standardOutput: standardOutput, + standardError: standardError, + commandWorkingDirPath: command.WorkingDirPath ); throw; } @@ -145,10 +148,11 @@ private async Task Of(CliWrap.Command command, { _commandLogger.Log(options: options, inputToLog: inputToLog, - -1, - stopwatch.Elapsed, - standardOutput, - standardError + exitCode: -1, + runTime: stopwatch.Elapsed, + standardOutput: standardOutput, + standardError: standardError, + commandWorkingDirPath: command.WorkingDirPath ); throw; diff --git a/src/ModularPipelines/Logging/CommandLogger.cs b/src/ModularPipelines/Logging/CommandLogger.cs index b6999347d0..4644c72b32 100644 --- a/src/ModularPipelines/Logging/CommandLogger.cs +++ b/src/ModularPipelines/Logging/CommandLogger.cs @@ -25,7 +25,8 @@ public CommandLogger(IModuleLoggerProvider moduleLoggerProvider, private ILogger Logger => _moduleLoggerProvider.GetLogger(); - public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCode, TimeSpan? runTime, string standardOutput, string standardError) + public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCode, TimeSpan? runTime, + string standardOutput, string standardError, string commandWorkingDirPath) { if (options.CommandLogging == CommandLogging.None) { @@ -38,7 +39,7 @@ public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCod { if (options.InternalDryRun && ShouldLogInput(optionsCommandLogging)) { - Logger.LogInformation("---Executing Command---\r\n\t{Input}", inputToLog); + Logger.LogInformation("---Executing Command---\r\n\t{WorkingDirectory}> {Input}", commandWorkingDirPath, inputToLog); Logger.LogInformation("---Dry-Run Command - No Output---"); return; } @@ -47,16 +48,17 @@ public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCod { Logger.LogInformation(""" ---Executing Command--- - {Input} + {WorkingDirectory}> {Input} """, + commandWorkingDirPath, _secretObfuscator.Obfuscate(inputToLog, options)); } else { Logger.LogInformation(""" ---Executing Command--- - ******** - """); + {WorkingDirectory}> ******** + """, commandWorkingDirPath); } if (optionsCommandLogging.HasFlag(CommandLogging.ExitCode)) diff --git a/src/ModularPipelines/Logging/ICommandLogger.cs b/src/ModularPipelines/Logging/ICommandLogger.cs index 0f7d422b8c..e32a81cc4b 100644 --- a/src/ModularPipelines/Logging/ICommandLogger.cs +++ b/src/ModularPipelines/Logging/ICommandLogger.cs @@ -9,6 +9,5 @@ void Log(CommandLineToolOptions options, int? exitCode, TimeSpan? runTime, string standardOutput, - string standardError - ); + string standardError, string commandWorkingDirPath); } \ No newline at end of file diff --git a/test/ModularPipelines.UnitTests/CommandLoggerTests.cs b/test/ModularPipelines.UnitTests/CommandLoggerTests.cs index dc8073fb87..118882c0be 100644 --- a/test/ModularPipelines.UnitTests/CommandLoggerTests.cs +++ b/test/ModularPipelines.UnitTests/CommandLoggerTests.cs @@ -36,17 +36,17 @@ echo Hello world! if (logInput) { - await Assert.That(logFile).Does.Contain(""" + await Assert.That(logFile).Does.Contain($""" ---Executing Command--- - pwsh -Command "echo Hello world! + {Environment.CurrentDirectory}> pwsh -Command "echo Hello world! throw \"Error!\"" """); } else { - await Assert.That(logFile).Does.Contain(""" + await Assert.That(logFile).Does.Contain($""" ---Executing Command--- - ******** + {Environment.CurrentDirectory}> ******** """); }