diff --git a/src/tooling/Elastic.Documentation.Tooling/ExternalCommands/ExternalCommandExecutor.cs b/src/tooling/Elastic.Documentation.Tooling/ExternalCommands/ExternalCommandExecutor.cs index 5c51e9940..2e16f0793 100644 --- a/src/tooling/Elastic.Documentation.Tooling/ExternalCommands/ExternalCommandExecutor.cs +++ b/src/tooling/Elastic.Documentation.Tooling/ExternalCommands/ExternalCommandExecutor.cs @@ -5,7 +5,6 @@ using System.IO.Abstractions; using Elastic.Documentation.Diagnostics; using ProcNet; -using ProcNet.Std; namespace Elastic.Documentation.Tooling.ExternalCommands; @@ -38,11 +37,12 @@ protected void ExecInSilent(Dictionary environmentVars, string b collector.EmitError("", $"Exit code: {result.ExitCode} while executing {binary} {string.Join(" ", args)} in {workingDirectory}"); } - protected string[] CaptureMultiple(string binary, params string[] args) + protected string[] CaptureMultiple(string binary, params string[] args) => CaptureMultiple(false, 10, binary, args); + protected string[] CaptureMultiple(bool muteExceptions, int attempts, string binary, params string[] args) { // Try 10 times to capture the output of the command, if it fails, we'll throw an exception on the last try Exception? e = null; - for (var i = 0; i <= 9; i++) + for (var i = 1; i <= attempts; i++) { try { @@ -55,7 +55,7 @@ protected string[] CaptureMultiple(string binary, params string[] args) } } - if (e is not null) + if (e is not null && !muteExceptions) collector.EmitError("", "failure capturing stdout", e); return []; @@ -70,9 +70,12 @@ string[] CaptureOutput() ConsoleOutWriter = NoopConsoleWriter.Instance }; var result = Proc.Start(arguments); - var output = result.ExitCode != 0 - ? throw new Exception($"Exit code is not 0. Received {result.ExitCode} from {binary}: {workingDirectory}") - : result.ConsoleOut.Select(x => x.Line).ToArray() ?? throw new Exception($"No output captured for {binary}: {workingDirectory}"); + + var output = (result.ExitCode, muteExceptions) switch + { + (0, _) or (not 0, true) => result.ConsoleOut.Select(x => x.Line).ToArray() ?? throw new Exception($"No output captured for {binary}: {workingDirectory}"), + (not 0, false) => throw new Exception($"Exit code is not 0. Received {result.ExitCode} from {binary}: {workingDirectory}") + }; return output; } } diff --git a/src/tooling/docs-assembler/Deploying/AwsCloudFrontKeyValueStoreProxy.cs b/src/tooling/docs-assembler/Deploying/AwsCloudFrontKeyValueStoreProxy.cs index 75f005f6e..d016a1a43 100644 --- a/src/tooling/docs-assembler/Deploying/AwsCloudFrontKeyValueStoreProxy.cs +++ b/src/tooling/docs-assembler/Deploying/AwsCloudFrontKeyValueStoreProxy.cs @@ -42,8 +42,8 @@ public void UpdateRedirects(string kvsName, IReadOnlyDictionary ConsoleApp.Log("Describing KeyValueStore"); try { - var json = Capture("aws", "cloudfront", "describe-key-value-store", "--name", kvsName, "|", "jq", "-c"); - var describeResponse = JsonSerializer.Deserialize(json, AwsCloudFrontKeyValueStoreJsonContext.Default.DescribeKeyValueStoreResponse); + var json = CaptureMultiple("aws", "cloudfront", "describe-key-value-store", "--name", kvsName); + var describeResponse = JsonSerializer.Deserialize(string.Concat(json), AwsCloudFrontKeyValueStoreJsonContext.Default.DescribeKeyValueStoreResponse); if (describeResponse?.ETag is not null && describeResponse.KeyValueStore is { ARN.Length: > 0 }) return (describeResponse.KeyValueStore.ARN, describeResponse.ETag); @@ -67,8 +67,8 @@ private HashSet ListAllKeys(string kvsArn) { do { - var json = Capture("aws", [.. baseArgs, .. nextToken is not null ? (string[])["--starting-token", nextToken] : [], "|", "jq", "-c"]); - var response = JsonSerializer.Deserialize(json, AwsCloudFrontKeyValueStoreJsonContext.Default.ListKeysResponse); + var json = CaptureMultiple("aws", [.. baseArgs, .. nextToken is not null ? (string[])["--starting-token", nextToken] : []]); + var response = JsonSerializer.Deserialize(string.Concat(json), AwsCloudFrontKeyValueStoreJsonContext.Default.ListKeysResponse); if (response?.Items != null) { @@ -108,9 +108,9 @@ private string ProcessBatchUpdates( AwsCloudFrontKeyValueStoreJsonContext.Default.ListDeleteKeyRequestListItem), _ => string.Empty }; - var responseJson = Capture(false, 1, "aws", "cloudfront-keyvaluestore", "update-keys", "--kvs-arn", kvsArn, "--if-match", eTag, - $"--{operation.ToString().ToLowerInvariant()}", "--payload", payload, "|", "jq", "-c"); - var updateResponse = JsonSerializer.Deserialize(responseJson, AwsCloudFrontKeyValueStoreJsonContext.Default.UpdateKeysResponse); + var responseJson = CaptureMultiple(false, 1, "aws", "cloudfront-keyvaluestore", "update-keys", "--kvs-arn", kvsArn, "--if-match", eTag, + $"--{operation.ToString().ToLowerInvariant()}", "--payload", payload); + var updateResponse = JsonSerializer.Deserialize(string.Concat(responseJson), AwsCloudFrontKeyValueStoreJsonContext.Default.UpdateKeysResponse); if (string.IsNullOrEmpty(updateResponse?.ETag)) throw new Exception("Failed to get new ETag after update operation.");