Skip to content

Commit bb7abcd

Browse files
authored
Adjust for multiple line output from CLI (#1506)
* Remove pipe, adjust for multiple line output from CLI
1 parent d565b78 commit bb7abcd

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/tooling/Elastic.Documentation.Tooling/ExternalCommands/ExternalCommandExecutor.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.IO.Abstractions;
66
using Elastic.Documentation.Diagnostics;
77
using ProcNet;
8-
using ProcNet.Std;
98

109
namespace Elastic.Documentation.Tooling.ExternalCommands;
1110

@@ -38,11 +37,12 @@ protected void ExecInSilent(Dictionary<string, string> environmentVars, string b
3837
collector.EmitError("", $"Exit code: {result.ExitCode} while executing {binary} {string.Join(" ", args)} in {workingDirectory}");
3938
}
4039

41-
protected string[] CaptureMultiple(string binary, params string[] args)
40+
protected string[] CaptureMultiple(string binary, params string[] args) => CaptureMultiple(false, 10, binary, args);
41+
protected string[] CaptureMultiple(bool muteExceptions, int attempts, string binary, params string[] args)
4242
{
4343
// Try 10 times to capture the output of the command, if it fails, we'll throw an exception on the last try
4444
Exception? e = null;
45-
for (var i = 0; i <= 9; i++)
45+
for (var i = 1; i <= attempts; i++)
4646
{
4747
try
4848
{
@@ -55,7 +55,7 @@ protected string[] CaptureMultiple(string binary, params string[] args)
5555
}
5656
}
5757

58-
if (e is not null)
58+
if (e is not null && !muteExceptions)
5959
collector.EmitError("", "failure capturing stdout", e);
6060

6161
return [];
@@ -70,9 +70,12 @@ string[] CaptureOutput()
7070
ConsoleOutWriter = NoopConsoleWriter.Instance
7171
};
7272
var result = Proc.Start(arguments);
73-
var output = result.ExitCode != 0
74-
? throw new Exception($"Exit code is not 0. Received {result.ExitCode} from {binary}: {workingDirectory}")
75-
: result.ConsoleOut.Select(x => x.Line).ToArray() ?? throw new Exception($"No output captured for {binary}: {workingDirectory}");
73+
74+
var output = (result.ExitCode, muteExceptions) switch
75+
{
76+
(0, _) or (not 0, true) => result.ConsoleOut.Select(x => x.Line).ToArray() ?? throw new Exception($"No output captured for {binary}: {workingDirectory}"),
77+
(not 0, false) => throw new Exception($"Exit code is not 0. Received {result.ExitCode} from {binary}: {workingDirectory}")
78+
};
7679
return output;
7780
}
7881
}

src/tooling/docs-assembler/Deploying/AwsCloudFrontKeyValueStoreProxy.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void UpdateRedirects(string kvsName, IReadOnlyDictionary<string, string>
4242
ConsoleApp.Log("Describing KeyValueStore");
4343
try
4444
{
45-
var json = Capture("aws", "cloudfront", "describe-key-value-store", "--name", kvsName, "|", "jq", "-c");
46-
var describeResponse = JsonSerializer.Deserialize<DescribeKeyValueStoreResponse>(json, AwsCloudFrontKeyValueStoreJsonContext.Default.DescribeKeyValueStoreResponse);
45+
var json = CaptureMultiple("aws", "cloudfront", "describe-key-value-store", "--name", kvsName);
46+
var describeResponse = JsonSerializer.Deserialize<DescribeKeyValueStoreResponse>(string.Concat(json), AwsCloudFrontKeyValueStoreJsonContext.Default.DescribeKeyValueStoreResponse);
4747
if (describeResponse?.ETag is not null && describeResponse.KeyValueStore is { ARN.Length: > 0 })
4848
return (describeResponse.KeyValueStore.ARN, describeResponse.ETag);
4949

@@ -67,8 +67,8 @@ private HashSet<string> ListAllKeys(string kvsArn)
6767
{
6868
do
6969
{
70-
var json = Capture("aws", [.. baseArgs, .. nextToken is not null ? (string[])["--starting-token", nextToken] : [], "|", "jq", "-c"]);
71-
var response = JsonSerializer.Deserialize<ListKeysResponse>(json, AwsCloudFrontKeyValueStoreJsonContext.Default.ListKeysResponse);
70+
var json = CaptureMultiple("aws", [.. baseArgs, .. nextToken is not null ? (string[])["--starting-token", nextToken] : []]);
71+
var response = JsonSerializer.Deserialize<ListKeysResponse>(string.Concat(json), AwsCloudFrontKeyValueStoreJsonContext.Default.ListKeysResponse);
7272

7373
if (response?.Items != null)
7474
{
@@ -108,9 +108,9 @@ private string ProcessBatchUpdates(
108108
AwsCloudFrontKeyValueStoreJsonContext.Default.ListDeleteKeyRequestListItem),
109109
_ => string.Empty
110110
};
111-
var responseJson = Capture(false, 1, "aws", "cloudfront-keyvaluestore", "update-keys", "--kvs-arn", kvsArn, "--if-match", eTag,
112-
$"--{operation.ToString().ToLowerInvariant()}", "--payload", payload, "|", "jq", "-c");
113-
var updateResponse = JsonSerializer.Deserialize<UpdateKeysResponse>(responseJson, AwsCloudFrontKeyValueStoreJsonContext.Default.UpdateKeysResponse);
111+
var responseJson = CaptureMultiple(false, 1, "aws", "cloudfront-keyvaluestore", "update-keys", "--kvs-arn", kvsArn, "--if-match", eTag,
112+
$"--{operation.ToString().ToLowerInvariant()}", "--payload", payload);
113+
var updateResponse = JsonSerializer.Deserialize<UpdateKeysResponse>(string.Concat(responseJson), AwsCloudFrontKeyValueStoreJsonContext.Default.UpdateKeysResponse);
114114

115115
if (string.IsNullOrEmpty(updateResponse?.ETag))
116116
throw new Exception("Failed to get new ETag after update operation.");

0 commit comments

Comments
 (0)