コード例 #1
0
ファイル: ShellCommand.cs プロジェクト: mlinnell/StackWarden
        protected override ToolResult ExecuteBody()
        {
            var commandArguments = string.Join(" ", _arguments ?? new string[0]);
            var process          = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    WindowStyle = ProcessWindowStyle.Hidden,
                    FileName    = "cmd.exe",
                    Arguments   = $"/C {_command} {commandArguments}"
                }
            };

            if (process.Start())
            {
                process.WaitForExit(Timeout);
            }

            var output      = process.StandardOutput.ReadToEnd();
            var errorOutput = process.StandardError.ReadToEnd();
            var didSucceed  = string.IsNullOrWhiteSpace(errorOutput);
            var result      = new ToolResult
            {
                DidSucceed = didSucceed,
                State      = didSucceed ? SeverityState.Normal : SeverityState.Error,
                Metadata   =
                {
                    { "Exit Code", process.ExitCode.ToString() },
                    { "Output",    output                      },
                    { "Errors",    errorOutput                 }
                }
            };

            return(result);
        }
コード例 #2
0
        protected override ToolResult ExecuteBody()
        {
            var aggregateResult = new ToolResult
            {
                DidSucceed = true
            };

            for (var i = 0; aggregateResult.DidSucceed && i < _tools.Count; i++)
            {
                var result = _tools[i].Execute().Result;

                aggregateResult.DidSucceed = aggregateResult.DidSucceed && result.DidSucceed;

                foreach (var currentPair in result.Metadata)
                {
                    if (!aggregateResult.Metadata.ContainsKey(currentPair.Key))
                    {
                        aggregateResult.Metadata.Add(currentPair.Key, currentPair.Value);
                    }
                    else
                    {
                        aggregateResult.Metadata[currentPair.Key] += Environment.NewLine + currentPair.Value;
                    }
                }
            }

            return(aggregateResult);
        }
コード例 #3
0
ファイル: Tool.cs プロジェクト: mlinnell/StackWarden
        public virtual Task <ToolResult> Execute()
        {
            return(Task.Run(() =>
            {
                var result = new ToolResult();

                try
                {
                    result = ExecuteBody();
                }
                catch (Exception ex)
                {
                    result.DidSucceed = false;
                    result.State = Core.SeverityState.Error;
                    result.Metadata.Add("Exception", ex.ToDetailString());
                }

                return result;
            }));
        }
コード例 #4
0
        protected override ToolResult ExecuteBody()
        {
            var powershell = PowerShell.Create();
            var pipeline   = powershell.Runspace.CreatePipeline();

            if (IsScript)
            {
                pipeline.Commands.AddScript(_command);
            }
            else
            {
                pipeline.Commands.Add(_command);
            }

            var shellResult = pipeline.Invoke();
            var builder     = new StringBuilder();
            var result      = new ToolResult();

            if (pipeline.HasErrors())
            {
                result.DidSucceed = false;

                var errors = pipeline.GetErrors()?
                             .Select(x => x.ErrorDetails)
                             .Where(x => x != null);

                if (errors != null)
                {
                    result.Metadata.Add("Errors", JsonConvert.SerializeObject(errors));
                }
            }

            result.Metadata.Add("Details", JsonConvert.SerializeObject(shellResult));

            return(result);
        }