Skip to content

Commit

Permalink
feat: execute and return native process
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Nov 9, 2024
1 parent eb1c64e commit fbae2d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Flucli/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,29 @@ public async Task<CliResult> ExecutePipeAsync(CancellationToken cancellationToke
return await compositeCli.ExecuteAsync(cancellationToken);
}

public Process Execute()
{
ProcessEx process = new(CreateStartInfo());

process.Start();
return process.NativeProcess;
}

public CliResult ExecuteSync()
{
using ProcessEx process = new(CreateStartInfo());

process.Start();
process.NativeProcess.WaitForExit();
return new CliResult(process.ExitCode, process.StartTime, process.ExitTime);
}

public async Task<CliResult> ExecuteAsync(CancellationToken cancellationToken = default) =>
await ExecuteAsync(cancellationToken, CancellationToken.None);

public async Task<CliResult> ExecuteAsync(CancellationToken forcefulCancellationToken, CancellationToken gracefulCancellationToken)
{
ProcessEx process = new(CreateStartInfo());
using ProcessEx process = new(CreateStartInfo());

process.Start();
return await ExecuteAsync(process, forcefulCancellationToken, gracefulCancellationToken);
Expand Down
2 changes: 2 additions & 0 deletions src/Flucli/Utils/ProcessEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal sealed class ProcessEx(ProcessStartInfo startInfo) : IDisposable
private readonly TaskCompletionSource<object?> _exitTcs =
new(TaskCreationOptions.RunContinuationsAsynchronously);

public Process NativeProcess => _nativeProcess;

public int Id => _nativeProcess.Id;

public string Name =>
Expand Down

0 comments on commit fbae2d2

Please sign in to comment.