コード例 #1
0
ファイル: Kernel.cs プロジェクト: IPA-CyberLab/IPA-DN-Cores
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
ファイル: Kernel.cs プロジェクト: IPA-CyberLab/IPA-DN-Cores
        public EasyExecResult(ExecInstance exec)
        {
            this.Instance = exec;

            this.OutputStr = exec.EasyOutputStr._NonNull()._NormalizeCrlf(true);
            this.ErrorStr  = exec.EasyErrorStr._NonNull()._NormalizeCrlf(true);

            this.OutputAndErrorStr = this.OutputStr + this.ErrorStr;
            this.ErrorAndOutputStr = this.ErrorStr + this.OutputStr;
        }
コード例 #4
0
        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);
        }