// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace AWS.Deploy.Orchestration.Utilities
{
public interface ICommandLineWrapper
{
///
/// Forks a new shell process and executes .
///
///
/// Shell script to execute
///
///
/// Default directory for the shell. This needs to have the correct pathing for
/// the current OS
///
///
/// By default standard out/error will be piped to a .
/// Set this to false to disable sending output.
///
///
/// Async callback to inspect/manipulate the completed . Useful
/// if you need to get an exit code or .
///
///
/// By default, , and will be redirected.
/// Set this to false to avoid redirection.
///
///
/// is executed as a child process of running process which inherits the parent process's environment variables.
/// allows to add (replace if exists) extra environment variables to the child process.
///
/// AWS Execution Environment string to append in AWS_EXECUTION_ENV env var.
/// AWS SDK calls made while executing will have User-Agent string containing
///
///
///
///
///
public Task Run(
string command,
string workingDirectory = "",
bool streamOutputToInteractiveService = true,
Action? onComplete = null,
bool redirectIO = true,
string? stdin = null,
IDictionary? environmentVariables = null,
CancellationToken cancelToken = default,
bool needAwsCredentials = false);
///
/// Configure the child process that executes the command passed as parameter in method.
///
/// Child process that executes the command
void ConfigureProcess(Action processStartInfoAction);
}
public static class CommandLineWrapperExtensions
{
///
/// Convenience extension to
/// that returns a with the full contents
/// of and
///
///
/// See
///
///
/// Shell script to execute
///
///
/// Default directory for the shell. This needs to have the correct pathing for
/// the current OS
///
///
/// By default standard out/error will be piped to a .
/// Set this to false to disable sending output.
///
///
/// By default, , and will be redirected.
/// Set this to false to avoid redirection.
///
///
/// Text to pass into the process through standard input.
///
///
/// is executed as a child process of running process which inherits the parent process's environment variables.
/// allows to add (replace if exists) extra environment variables to the child process.
///
/// AWS Execution Environment string to append in AWS_EXECUTION_ENV env var.
/// AWS SDK calls made while executing will have User-Agent string containing
///
///
///
///
///
public static async Task TryRunWithResult(
this ICommandLineWrapper commandLineWrapper,
string command,
string workingDirectory = "",
bool streamOutputToInteractiveService = false,
bool redirectIO = true,
string? stdin = null,
IDictionary? environmentVariables = null,
CancellationToken cancelToken = default,
bool needAwsCredentials = false)
{
var result = new TryRunResult();
await commandLineWrapper.Run(
command,
workingDirectory,
streamOutputToInteractiveService,
onComplete: runResult => result = runResult,
redirectIO: redirectIO,
stdin: stdin,
environmentVariables: environmentVariables,
cancelToken: cancelToken,
needAwsCredentials: needAwsCredentials);
return result;
}
}
public class TryRunResult
{
///
/// Indicates if this command was run successfully. This checks that
/// is empty.
///
public bool Success => string.IsNullOrWhiteSpace(StandardError);
///
/// Fully read
///
public string? StandardOut { get; set; }
///
/// Fully read
///
public string? StandardError { get; set; }
///
/// Fully read
///
public int ExitCode { get; set; }
}
}