コード例 #1
0
ファイル: ProcessUtil.cs プロジェクト: baronfel/runmongorun
        public static Result<bool, int> ExecProcess(string fqnMongoPath, string args, ConsoleOps op, Action<string> logFunc, Action<string> errFunc)
        {
            bool errored = false;
            var startInfo = new ProcessStartInfo()
            {
                FileName = fqnMongoPath,
                Arguments = args,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                UseShellExecute = false
            };

            var process = new Process
            {
                StartInfo = startInfo,
            };

            process.OutputDataReceived += (o, d) =>
            {
                if (string.IsNullOrEmpty(d.Data)) return;
                if (TryParseErrorLine(d.Data))
                {
                    errored = true;
                    errFunc("-----" + d.Data);
                }
                else
                {
                    logFunc("-----" + d.Data);
                }
            };
            process.ErrorDataReceived += (o, d) =>
            {
                if (string.IsNullOrEmpty(d.Data) || string.IsNullOrWhiteSpace(d.Data)) return;
                errored = true;
                errFunc("-----" + d.Data);
            };
            process.Start();

            logFunc(op.Header);
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            var command = string.Join(string.Empty, op.Ops);
            process.StandardInput.WriteLine(command);
            process.StandardInput.WriteLine("exit");
            process.WaitForExit();
            logFunc(op.Footer);

            if (errored)
            {
                return Result<bool, int>.FailWith(process.ExitCode);
            }
            else
            {
                return Result<bool, int>.Succeed(true, 0);
            }
        }
コード例 #2
0
        static Result <bool, int> ExecCommandChangeSet(string pathToMongo, string hostName, int port, string database, ChangeSet changeSet, Action <string> logFunc, Action <string> errFunc)
        {
            var connstring = MakeCommandLineConnectionString(hostName, port, database);
            var ops        = new ConsoleOps(
                string.Format("Executing Changeset '{0}' from file '{1}'", changeSet.ChangeId, changeSet.File),
                changeSet.Content,
                string.Format("Finished Changeset '{0}' from file '{1}'", changeSet.ChangeId, changeSet.File)
                );

            return(ExecProcess(pathToMongo, connstring, ops, logFunc, errFunc));
        }
コード例 #3
0
        public static Result <bool, int> ExecProcess(string fqnMongoPath, string args, ConsoleOps op, Action <string> logFunc, Action <string> errFunc)
        {
            bool errored   = false;
            var  startInfo = new ProcessStartInfo()
            {
                FileName               = fqnMongoPath,
                Arguments              = args,
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
                RedirectStandardInput  = true,
                UseShellExecute        = false
            };

            var process = new Process
            {
                StartInfo = startInfo,
            };

            process.OutputDataReceived += (o, d) =>
            {
                if (string.IsNullOrEmpty(d.Data))
                {
                    return;
                }
                if (TryParseErrorLine(d.Data))
                {
                    errored = true;
                    errFunc("-----" + d.Data);
                }
                else
                {
                    logFunc("-----" + d.Data);
                }
            };
            process.ErrorDataReceived += (o, d) =>
            {
                if (string.IsNullOrEmpty(d.Data) || string.IsNullOrWhiteSpace(d.Data))
                {
                    return;
                }
                errored = true;
                errFunc("-----" + d.Data);
            };
            process.Start();

            logFunc(op.Header);
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            var command = string.Join(string.Empty, op.Ops);

            process.StandardInput.WriteLine(command);
            process.StandardInput.WriteLine("exit");
            process.WaitForExit();
            logFunc(op.Footer);

            if (errored)
            {
                return(Result <bool, int> .FailWith(process.ExitCode));
            }
            else
            {
                return(Result <bool, int> .Succeed(true, 0));
            }
        }