public static Task <ExecStatusEventArgs> RunAsync( ExecFlags flags, Action <ConsoleRedirection.Segment> outputHandler, string command, params string [] arguments) => new Exec( ProcessArguments.FromCommandAndArguments(command, arguments), flags | RedirectStdout | RedirectStderr, new ConsoleRedirection(outputHandler)).RunAsync();
public static async Task <EasyExecResult> ExecAsync(string cmdName, string?arguments = null, string?currentDirectory = null, ExecFlags flags = ExecFlags.Default | ExecFlags.EasyInputOutputMode, int easyOutputMaxSize = Consts.Numbers.DefaultLargeBufferSize, string?easyInputStr = null, int?timeout = null, CancellationToken cancel = default, bool debug = false, bool throwOnErrorExitCode = true, string printTag = "", Func <string, Task <bool> >?easyOneLineRecvCallbackAsync = null, Func <ReadOnlyMemory <byte>, ReadOnlyMemory <byte>, Task <bool> >?easyRealtimeRecvBufCallbackAsync = null, int easyRealtimeRecvBufCallbackDelayTickMsecs = 0, StrDictionary <string>?additionalEnvVars = null) { if (timeout <= 0) { timeout = Timeout.Infinite; } ExecOptions opt = new ExecOptions(cmdName, arguments, currentDirectory, flags, easyOutputMaxSize, easyInputStr, printTag, easyOneLineRecvCallbackAsync, easyRealtimeRecvBufCallbackAsync, easyRealtimeRecvBufCallbackDelayTickMsecs, additionalEnvVars); if (debug) { Dbg.WriteLine($"ExecAsync: --- Starting process \"{cmdName}{(arguments._IsFilled() ? " " : "")}{arguments}\" ---"); } EasyExecResult result; try { using ExecInstance exec = new ExecInstance(opt); try { await exec.WaitForExitAsync(timeout._NormalizeTimeout(CoresConfig.Timeouts.DefaultEasyExecTimeout), cancel); } finally { await exec.CancelAsync(); } result = new EasyExecResult(exec); } catch (Exception ex) { Dbg.WriteLine($"Error on starting process \"{cmdName}{(arguments._IsFilled() ? " " : "")}{arguments}\". Exception: {ex.Message}"); throw; } if (debug) { Dbg.WriteLine($"ExecAsync: The result of process \"{cmdName}{(arguments._IsFilled() ? " " : "")}{arguments}\": " + result.ToString(Str.GetCrlfStr(), false)); } if (throwOnErrorExitCode) { result.ThrowExceptionIfError(); } return(result); }
internal ExecLog( int execId, ExecFlags flags, ProcessArguments arguments, int?exitCode = null) { ExecId = execId; Flags = flags; Arguments = arguments; ExitCode = exitCode; }
public Exec( ProcessArguments arguments, ExecFlags flags = None, Action <ConsoleRedirection.Segment> outputHandler = null, Action <StreamWriter> inputHandler = null, string workingDirectory = null) : this( arguments, flags, outputHandler == null ? null : new ConsoleRedirection(outputHandler), inputHandler, workingDirectory) { }
public Exec( ProcessArguments arguments, ExecFlags flags = None, ConsoleRedirection outputRedirection = null, Action <StreamWriter> inputHandler = null, string workingDirectory = null, IReadOnlyDictionary <string, string> environmentVariables = null) { Arguments = arguments; if (Arguments.Count < 1) { throw new ArgumentOutOfRangeException( nameof(arguments), "must have at least one argument (the file name to execute)"); } Flags = flags; InputHandler = inputHandler; OutputRedirection = outputRedirection; if (Flags.HasFlag(RedirectStdin) && InputHandler == null) { throw new ArgumentException( $"{nameof (RedirectStdin)} was specified " + $"but {nameof (InputHandler)} is null", nameof(flags)); } if (Flags.HasFlag(RedirectStdout) && OutputRedirection == null) { throw new ArgumentException( $"{nameof (RedirectStdout)} was specified " + $"but {nameof (OutputRedirection)} is null", nameof(flags)); } if (Flags.HasFlag(RedirectStderr) && OutputRedirection == null) { throw new ArgumentException( $"{nameof (RedirectStderr)} was specified " + $"but {nameof (OutputRedirection)} is null", nameof(flags)); } WorkingDirectory = workingDirectory; EnvironmentVariables = environmentVariables ?? new Dictionary <string, string> (); id = lastId++; }
public Exec( ProcessArguments arguments, ExecFlags flags = None, Action <ConsoleRedirection.Segment> outputHandler = null, Action <StreamWriter> inputHandler = null, string workingDirectory = null, IReadOnlyDictionary <string, string> environmentVariables = null) : this( arguments, flags, outputHandler == null ? null : new ConsoleRedirection(outputHandler), inputHandler, workingDirectory, environmentVariables) { }
public static IReadOnlyList <string> Run( ExecFlags flags, string command, params string [] arguments) { var lines = new List <string> (); new Exec( ProcessArguments.FromCommandAndArguments(command, arguments), flags | RedirectStdout | RedirectStderr, new ConsoleRedirection(segment => lines.Add(segment.Data.TrimEnd('\r', '\n')))) .RunAsync() .GetAwaiter() .GetResult(); return(lines); }
public ExecOptions(string commandName, IEnumerable <string> argumentsList, string?currentDirectory = null, ExecFlags flags = ExecFlags.Default, int easyOutputMaxSize = Consts.Numbers.DefaultLargeBufferSize, string?easyInputStr = null, string printTag = "", Func <string, Task <bool> >?easyOneLineRecvCallbackAsync = null, Func <ReadOnlyMemory <byte>, ReadOnlyMemory <byte>, Task <bool> >?easyRealtimeRecvBufCallbackAsync = null, int easyRealtimeRecvBufCallbackDelayTickMsecs = 0, StrDictionary <string>?additionalEnvVars = null) { this.CommandName = commandName._NullCheck(); if (Env.IsUnix == false || flags.Bit(ExecFlags.UnixAutoFullPath) == false) { this.FileName = this.CommandName._NullCheck(); } else { try { this.FileName = Lfs.UnixGetFullPathFromCommandName(this.CommandName._NullCheck()); } catch { this.FileName = this.CommandName._NullCheck(); } } this.Arguments = ""; this.ArgumentsList = argumentsList; this.CurrentDirectory = currentDirectory._NonNull(); this.Flags = flags; this.EasyOutputMaxSize = easyOutputMaxSize._Max(1); this.EasyInputStr = easyInputStr._NullIfZeroLen(); this.PrintTag = printTag; this.EasyOneLineRecvCallbackAsync = easyOneLineRecvCallbackAsync; this.EasyRealtimeRecvBufCallbackAsync = easyRealtimeRecvBufCallbackAsync; this.EasyRealtimeRecvBufCallbackDelayTickMsecs = easyRealtimeRecvBufCallbackDelayTickMsecs; this.AdditionalEnvVars = additionalEnvVars; }
public Exec( ProcessArguments arguments, ExecFlags flags = None, ConsoleRedirection outputRedirection = null, Action <StreamWriter> inputHandler = null, string workingDirectory = null, ProcessRunnerHandler processRunner = null) { Arguments = arguments; if (Arguments.Count < 1) { throw new ArgumentOutOfRangeException( nameof(arguments), "must have at least one argument (the file name to execute)"); } Id = lastId++; Flags = flags; Elevated = flags.HasFlag(Elevate); InputHandler = inputHandler; OutputRedirection = outputRedirection; if (isWindows) { // Ignore elevation flag if we are already running in the Administrator role var identity = WindowsIdentity.GetCurrent(); if (identity != null && new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator)) { Elevated = false; } } else { if (Path.GetExtension(arguments [0]) == ".exe") { Arguments = Arguments.Insert(0, "mono"); } if (Elevated) { if (geteuid() == 0) { Elevated = false; } else { Arguments = Arguments.Insert(0, "/usr/bin/sudo"); } } } if (Flags.HasFlag(RedirectStdin) && InputHandler == null) { throw new ArgumentException( $"{nameof (RedirectStdin)} was specified " + $"but {nameof (InputHandler)} is null", nameof(flags)); } if (Flags.HasFlag(RedirectStdout) && OutputRedirection == null) { throw new ArgumentException( $"{nameof (RedirectStdout)} was specified " + $"but {nameof (OutputRedirection)} is null", nameof(flags)); } if (Flags.HasFlag(RedirectStderr) && OutputRedirection == null) { throw new ArgumentException( $"{nameof (RedirectStderr)} was specified " + $"but {nameof (OutputRedirection)} is null", nameof(flags)); } WorkingDirectory = workingDirectory; ProcessRunner = processRunner ?? GlobalProcessRunner ?? DefaultProcessRunner; }
public ExecOptions(string fileName, IEnumerable <string> argumentsList, string?currentDirectory = null, ExecFlags flags = ExecFlags.Default, int easyOutputMaxSize = Consts.Numbers.DefaultLargeBufferSize, string?easyInputStr = null) { this.FileName = fileName._NullCheck(); this.Arguments = ""; this.ArgumentsList = argumentsList; this.CurrentDirectory = currentDirectory._NonNull(); this.Flags = flags; this.EasyOutputMaxSize = easyOutputMaxSize._Max(1); this.EasyInputStr = easyInputStr._NullIfZeroLen(); }
public static async Task <EasyExecResult> ExecAsync(string fileName, string?arguments = null, string?currentDirectory = null, ExecFlags flags = ExecFlags.Default | ExecFlags.EasyInputOutputMode, int easyOutputMaxSize = Consts.Numbers.DefaultLargeBufferSize, string?easyInputStr = null, int?timeout = null, CancellationToken cancel = default, bool debug = false, bool throwOnErrorExitCode = true) { if (timeout <= 0) { timeout = Timeout.Infinite; } ExecOptions opt = new ExecOptions(fileName, arguments, currentDirectory, flags, easyOutputMaxSize, easyInputStr); if (debug) { Dbg.WriteLine($"ExecAsync: --- Starting process \"{fileName}{(arguments._IsFilled() ? " " : "")}{arguments}\" ---"); } EasyExecResult result; try { using ExecInstance exec = new ExecInstance(opt); try { await exec.WaitForExitAsync(timeout._NormalizeTimeout(CoresConfig.Timeouts.DefaultEasyExecTimeout), cancel); } finally { exec.Cancel(); } result = new EasyExecResult(exec); } catch (Exception ex) { Dbg.WriteLine($"Error on starting process \"{fileName}{(arguments._IsFilled() ? " " : "")}{arguments}\". Exception: {ex.Message}"); throw; } if (debug) { Dbg.WriteLine($"ExecAsync: The result of process \"{fileName}{(arguments._IsFilled() ? " " : "")}{arguments}\": " + result.ToString(Str.GetCrlfStr(), false)); } if (throwOnErrorExitCode) { result.ThrowExceptionIfError(); } return(result); }
public static async Task <EasyExecResult> ExecBashAsync(string command, string?currentDirectory = null, ExecFlags flags = ExecFlags.Default | ExecFlags.EasyInputOutputMode, int easyOutputMaxSize = Consts.Numbers.DefaultLargeBufferSize, string?easyInputStr = null, int?timeout = null, CancellationToken cancel = default, bool debug = false, bool throwOnErrorExitCode = true) { if (timeout <= 0) { timeout = Timeout.Infinite; } List <string> args = new List <string>(); args.Add("-c"); args.Add(command); ExecOptions opt = new ExecOptions(Consts.LinuxCommands.Bash, args, currentDirectory, flags, easyOutputMaxSize, easyInputStr); if (debug) { Dbg.WriteLine($"ExecBashAsync: --- Starting bash command \"{command}\" ---"); } EasyExecResult result; try { using ExecInstance exec = new ExecInstance(opt); try { await exec.WaitForExitAsync(timeout._NormalizeTimeout(CoresConfig.Timeouts.DefaultEasyExecTimeout), cancel); } finally { exec.Cancel(); } result = new EasyExecResult(exec); } catch (Exception ex) { Dbg.WriteLine($"Error on bash process \"{command}\". Exception: {ex.Message}"); throw; } if (debug) { Dbg.WriteLine($"ExecAsync: The result of bash \"{command}\": " + result.ToString(Str.GetCrlfStr(), false)); } if (throwOnErrorExitCode) { result.ThrowExceptionIfError(); } return(result); }