Files
CommandForge/src/CommandForge.Application/UseCases/RunCommandUseCase.cs
T
poli0981andClaude Opus 5.5 37950a05ff feat(execution): trusted executable paths, encoding-aware output, kill-on-close job
- ExecutableResolver: catalog executables resolve to absolute paths in System32/Windows (and
  the winget alias), so CreateProcess never searches the per-user, user-writable app folder —
  a planted reg.exe/powershell.exe there would otherwise run elevated. Launch-mode commands now
  pin their working directory too.
- OutputDecoder: stdout/stderr are read as raw bytes and decoded per line (UTF-16LE when the
  stream is UTF-16, else strict UTF-8 with an OEM-code-page fallback). sfc/wsl output and every
  native tool on Vietnamese/Japanese/Chinese Windows used to show as mojibake. Lone \r (progress
  updates) still breaks lines.
- ProcessJob: captured commands join a kill-on-close job object, so closing or crashing the app
  can no longer leave a half-finished command running.
- ArgumentTokens ({SystemDrive}, {SystemRoot}, {UserProfile}) filled from SystemPaths, which reads
  the kernel-reported system directory, known folders and HKLM — never %SystemRoot%-style
  variables a standard user can redefine. The command preview shows the expanded values.
- Exit code 3010 always flags a restart; Launch commands honour RestartPolicy.Always; output is
  only captured (bounded) when a restart regex needs it. ExecutionResult.OutputLines is gone
  (nothing read it, and it held the entire output in memory).
- RunCommandUseCase always completes the output channel and maps cancellation, so a failing run
  can no longer leave the console waiting forever.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 20:19:03 +07:00

52 lines
1.9 KiB
C#

using System.Threading.Channels;
using CommandForge.Application.Ports;
using CommandForge.Domain;
namespace CommandForge.Application.UseCases;
/// <summary>
/// Routes a command to the right executor: admin commands go through the elevation broker,
/// everything else runs in-process (golden rule #2 — elevate on demand, not the whole app).
/// </summary>
public sealed class RunCommandUseCase
{
private readonly ICommandExecutor _executor;
private readonly IElevationService _elevation;
public RunCommandUseCase(ICommandExecutor executor, IElevationService elevation)
{
ArgumentNullException.ThrowIfNull(executor);
ArgumentNullException.ThrowIfNull(elevation);
_executor = executor;
_elevation = elevation;
}
/// <summary>
/// Runs <paramref name="command"/>, streaming output, choosing the executor by <c>RequiresAdmin</c>.
/// The output channel is always completed — even when the run throws — so a reader never waits forever.
/// </summary>
public async Task<ExecutionResult> RunAsync(
CommandDefinition command,
ChannelWriter<OutputLine> output,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(command);
ArgumentNullException.ThrowIfNull(output);
try
{
return command.RequiresAdmin
? await _elevation.RunElevatedAsync(command, output, cancellationToken).ConfigureAwait(false)
: await _executor.ExecuteAsync(command, output, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
return new ExecutionResult { ExitCode = -1, Success = false, Duration = TimeSpan.Zero, Cancelled = true };
}
finally
{
output.TryComplete();
}
}
}