Skip to content

Commit 001929c

Browse files
authored
Log working directory for command inputs (#575)
* Log working directory for command inputs * ReleaseNotes.md
1 parent caf06bb commit 001929c

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Show working directory when logging command inputs

src/ModularPipelines/Context/Command.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public async Task<CommandResult> ExecuteCommandLineTool(CommandLineToolOptions o
6666
exitCode: 0,
6767
runTime: TimeSpan.Zero,
6868
standardOutput: "Dummy Output Response",
69-
standardError: "Dummy Error Response"
69+
standardError: "Dummy Error Response",
70+
command.WorkingDirPath
7071
);
7172

7273
return new CommandResult(command);
@@ -116,10 +117,11 @@ private async Task<CommandResult> Of(CliWrap.Command command,
116117

117118
_commandLogger.Log(options: options,
118119
inputToLog: inputToLog,
119-
result.ExitCode,
120-
result.RunTime,
121-
standardOutput,
122-
standardError
120+
exitCode: result.ExitCode,
121+
runTime: result.RunTime,
122+
standardOutput: standardOutput,
123+
standardError: standardError,
124+
commandWorkingDirPath: command.WorkingDirPath
123125
);
124126

125127
if (result.ExitCode != 0 && options.ThrowOnNonZeroExitCode)
@@ -134,21 +136,23 @@ private async Task<CommandResult> Of(CliWrap.Command command,
134136
{
135137
_commandLogger.Log(options: options,
136138
inputToLog: inputToLog,
137-
e.ExitCode,
138-
stopwatch.Elapsed,
139-
standardOutput,
140-
standardError
139+
exitCode: e.ExitCode,
140+
runTime: stopwatch.Elapsed,
141+
standardOutput: standardOutput,
142+
standardError: standardError,
143+
commandWorkingDirPath: command.WorkingDirPath
141144
);
142145
throw;
143146
}
144147
catch (Exception e) when (e is not CommandExecutionException)
145148
{
146149
_commandLogger.Log(options: options,
147150
inputToLog: inputToLog,
148-
-1,
149-
stopwatch.Elapsed,
150-
standardOutput,
151-
standardError
151+
exitCode: -1,
152+
runTime: stopwatch.Elapsed,
153+
standardOutput: standardOutput,
154+
standardError: standardError,
155+
commandWorkingDirPath: command.WorkingDirPath
152156
);
153157

154158
throw;

src/ModularPipelines/Logging/CommandLogger.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public CommandLogger(IModuleLoggerProvider moduleLoggerProvider,
2525

2626
private ILogger Logger => _moduleLoggerProvider.GetLogger();
2727

28-
public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCode, TimeSpan? runTime, string standardOutput, string standardError)
28+
public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCode, TimeSpan? runTime,
29+
string standardOutput, string standardError, string commandWorkingDirPath)
2930
{
3031
if (options.CommandLogging == CommandLogging.None)
3132
{
@@ -38,7 +39,7 @@ public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCod
3839
{
3940
if (options.InternalDryRun && ShouldLogInput(optionsCommandLogging))
4041
{
41-
Logger.LogInformation("---Executing Command---\r\n\t{Input}", inputToLog);
42+
Logger.LogInformation("---Executing Command---\r\n\t{WorkingDirectory}> {Input}", commandWorkingDirPath, inputToLog);
4243
Logger.LogInformation("---Dry-Run Command - No Output---");
4344
return;
4445
}
@@ -47,16 +48,17 @@ public void Log(CommandLineToolOptions options, string? inputToLog, int? exitCod
4748
{
4849
Logger.LogInformation("""
4950
---Executing Command---
50-
{Input}
51+
{WorkingDirectory}> {Input}
5152
""",
53+
commandWorkingDirPath,
5254
_secretObfuscator.Obfuscate(inputToLog, options));
5355
}
5456
else
5557
{
5658
Logger.LogInformation("""
5759
---Executing Command---
58-
********
59-
""");
60+
{WorkingDirectory}> ********
61+
""", commandWorkingDirPath);
6062
}
6163

6264
if (optionsCommandLogging.HasFlag(CommandLogging.ExitCode))

src/ModularPipelines/Logging/ICommandLogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ void Log(CommandLineToolOptions options,
99
int? exitCode,
1010
TimeSpan? runTime,
1111
string standardOutput,
12-
string standardError
13-
);
12+
string standardError, string commandWorkingDirPath);
1413
}

test/ModularPipelines.UnitTests/CommandLoggerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ echo Hello world!
3636

3737
if (logInput)
3838
{
39-
await Assert.That(logFile).Does.Contain("""
39+
await Assert.That(logFile).Does.Contain($"""
4040
---Executing Command---
41-
pwsh -Command "echo Hello world!
41+
{Environment.CurrentDirectory}> pwsh -Command "echo Hello world!
4242
throw \"Error!\""
4343
""");
4444
}
4545
else
4646
{
47-
await Assert.That(logFile).Does.Contain("""
47+
await Assert.That(logFile).Does.Contain($"""
4848
---Executing Command---
49-
********
49+
{Environment.CurrentDirectory}> ********
5050
""");
5151
}
5252

0 commit comments

Comments
 (0)