Created
July 19, 2022 13:41
-
-
Save CodeMouse92/a96f407bcc27480f819935e1cf5a3099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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