Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO.Abstractions;
using Elastic.Documentation.Diagnostics;
using ProcNet;
using ProcNet.Std;

namespace Elastic.Documentation.Tooling.ExternalCommands;

Expand Down Expand Up @@ -38,11 +37,12 @@ protected void ExecInSilent(Dictionary<string, string> 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
{
Expand All @@ -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 [];
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void UpdateRedirects(string kvsName, IReadOnlyDictionary<string, string>
ConsoleApp.Log("Describing KeyValueStore");
try
{
var json = Capture("aws", "cloudfront", "describe-key-value-store", "--name", kvsName, "|", "jq", "-c");
var describeResponse = JsonSerializer.Deserialize<DescribeKeyValueStoreResponse>(json, AwsCloudFrontKeyValueStoreJsonContext.Default.DescribeKeyValueStoreResponse);
var json = CaptureMultiple("aws", "cloudfront", "describe-key-value-store", "--name", kvsName);
var describeResponse = JsonSerializer.Deserialize<DescribeKeyValueStoreResponse>(string.Concat(json), AwsCloudFrontKeyValueStoreJsonContext.Default.DescribeKeyValueStoreResponse);
if (describeResponse?.ETag is not null && describeResponse.KeyValueStore is { ARN.Length: > 0 })
return (describeResponse.KeyValueStore.ARN, describeResponse.ETag);

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

if (response?.Items != null)
{
Expand Down Expand Up @@ -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<UpdateKeysResponse>(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<UpdateKeysResponse>(string.Concat(responseJson), AwsCloudFrontKeyValueStoreJsonContext.Default.UpdateKeysResponse);

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