Skip to content

Commit

Permalink
Log working directory for command inputs (#575)
Browse files Browse the repository at this point in the history
* Log working directory for command inputs

* ReleaseNotes.md
  • Loading branch information
thomhurst authored Aug 26, 2024
1 parent caf06bb commit 001929c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Show working directory when logging command inputs
30 changes: 17 additions & 13 deletions src/ModularPipelines/Context/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public async Task<CommandResult> 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);
Expand Down Expand Up @@ -116,10 +117,11 @@ private async Task<CommandResult> 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)
Expand All @@ -134,21 +136,23 @@ private async Task<CommandResult> 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;
}
catch (Exception e) when (e is not CommandExecutionException)
{
_commandLogger.Log(options: options,
inputToLog: inputToLog,
-1,
stopwatch.Elapsed,
standardOutput,
standardError
exitCode: -1,
runTime: stopwatch.Elapsed,
standardOutput: standardOutput,
standardError: standardError,
commandWorkingDirPath: command.WorkingDirPath
);

throw;
Expand Down
12 changes: 7 additions & 5 deletions src/ModularPipelines/Logging/CommandLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions src/ModularPipelines/Logging/ICommandLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ void Log(CommandLineToolOptions options,
int? exitCode,
TimeSpan? runTime,
string standardOutput,
string standardError
);
string standardError, string commandWorkingDirPath);
}
8 changes: 4 additions & 4 deletions test/ModularPipelines.UnitTests/CommandLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}> ********
""");
}

Expand Down

0 comments on commit 001929c

Please sign in to comment.