Skip to content

Instantly share code, notes, and snippets.

@CodeMouse92
Created July 19, 2022 13:41
Show Gist options
  • Save CodeMouse92/a96f407bcc27480f819935e1cf5a3099 to your computer and use it in GitHub Desktop.
Save CodeMouse92/a96f407bcc27480f819935e1cf5a3099 to your computer and use it in GitHub Desktop.
using System.IO;
using System.Text;
using CliWrap;
namespace Corgibytes.Freshli.Cli.Functionality;
public static class InvokeCommand
{
public static string InvokeFreshli(string arguments)
{
var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();
var executionLocation = new FileInfo(
System.Reflection.Assembly.GetExecutingAssembly().Location
).Directory!; // null is forgivable here, because if we get null, there are far weirder problems afoot
var executable = new FileInfo(executionLocation.FullName + "/freshli");
var command = CliWrap.Cli.Wrap(executable.FullName).WithArguments(
args => args
.Add(arguments.Split())
)
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer));
using var task = command.ExecuteAsync().Task;
task.Wait();
if (task.Result.ExitCode != 0)
{
throw new IOException(
$"Invoking 'freshli {arguments}' failed with the following output:\n{stdErrBuffer}"
);
}
return stdOutBuffer.ToString();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment