Path: redb-route/redb.Route.Exec/Fluent/ExecDsl.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Exec/Fluent/ExecDsl.cs
----
using System.Text;
namespace redb.Route.Exec;
///
/// Fluent entry point for exec endpoints. Build URIs without manual string concatenation.
///
/// // Producer — request/response
/// .To(ExecDsl.Run("git").Args("status").AllowedCommands("git").TimeoutMs(5000))
///
/// // Consumer — scheduled
/// .From(ExecDsl.Run("./health-check.sh").Schedule("5m"))
///
///
public static class ExecDsl
{
/// Build an exec endpoint URI for the given command. The host part is fixed at run.
public static ExecBuilder Run(string? command = null) => new(command);
}
/// Fluent builder for exec endpoint URIs. Scheme: exec.
public sealed class ExecBuilder
{
private string? _command;
private string? _args;
private string? _allowedCommands;
private string? _workingDirectory;
private string? _envOverrides;
private bool _scrubEnv;
private int? _timeoutMs;
private int? _maxStdoutBytes;
private int? _maxStderrBytes;
private bool? _captureStderrInBody;
private bool? _jsonResponse;
private string? _schedule;
internal ExecBuilder(string? command) => _command = command;
// ── Command target ─────────────────────────────────────────────
/// The command to execute (overrides the constructor argument).
public ExecBuilder Command(string command) { _command = command; return this; }
/// Default argument list (whitespace-separated; quotes preserved).
public ExecBuilder Args(params string[] args)
{
_args = args.Length == 0 ? null : string.Join(' ', args.Select(QuoteIfNeeded));
return this;
}
// ── Allowlist ──────────────────────────────────────────────────
/// Restrict execution to the given command names (file-name-only, case-insensitive).
public ExecBuilder AllowedCommands(params string[] commands)
{
_allowedCommands = commands.Length == 0 ? null : string.Join(',', commands);
return this;
}
// ── Process environment ────────────────────────────────────────
/// Working directory for the spawned process.
public ExecBuilder WorkingDirectory(string path) { _workingDirectory = path; return this; }
/// Add or override an environment variable.
public ExecBuilder EnvOverride(string key, string value)
{
var entry = $"{key}={value}";
_envOverrides = string.IsNullOrEmpty(_envOverrides) ? entry : _envOverrides + "," + entry;
return this;
}
/// When set, the spawned process inherits no host environment variables.
public ExecBuilder ScrubEnvironment(bool value = true) { _scrubEnv = value; return this; }
// ── Limits ─────────────────────────────────────────────────────
/// Wall-clock timeout in milliseconds (0 = unlimited).
public ExecBuilder TimeoutMs(int ms) { _timeoutMs = ms; return this; }
/// Cap stdout capture (bytes).
public ExecBuilder MaxStdoutBytes(int bytes) { _maxStdoutBytes = bytes; return this; }
/// Cap stderr capture (bytes).
public ExecBuilder MaxStderrBytes(int bytes) { _maxStderrBytes = bytes; return this; }
/// Whether to fold stderr into the body (default true).
public ExecBuilder CaptureStderrInBody(bool value = true) { _captureStderrInBody = value; return this; }
// ── Output shape ───────────────────────────────────────────────
/// Whether the outbound body is a JSON object (default true).
public ExecBuilder JsonResponse(bool value = true) { _jsonResponse = value; return this; }
// ── Scheduled consumer ─────────────────────────────────────────
/// Schedule the consumer to fire on a fixed interval (e.g. "30s", "5m").
public ExecBuilder Schedule(string schedule) { _schedule = schedule; return this; }
// ── Build ──────────────────────────────────────────────────────
/// Build the URI string.
public string Build()
{
var sb = new StringBuilder("exec://run");
var sep = '?';
void Append(string key, string v)
{
sb.Append(sep);
sb.Append(key);
sb.Append('=');
sb.Append(Uri.EscapeDataString(v));
sep = '&';
}
void AppendStr(string key, string? v) { if (!string.IsNullOrEmpty(v)) Append(key, v); }
void AppendInt(string key, int? v) { if (v.HasValue) Append(key, v.Value.ToString()); }
void AppendBool(string key, bool v) { if (v) Append(key, "true"); }
void AppendBoolN(string key, bool? v) { if (v.HasValue) Append(key, v.Value.ToString().ToLowerInvariant()); }
AppendStr("command", _command);
AppendStr("args", _args);
AppendStr("allowedCommands", _allowedCommands);
AppendStr("workingDirectory", _workingDirectory);
AppendStr("environmentOverrides", _envOverrides);
AppendBool("scrubEnvironment", _scrubEnv);
AppendInt("timeoutMs", _timeoutMs);
AppendInt("maxStdoutBytes", _maxStdoutBytes);
AppendInt("maxStderrBytes", _maxStderrBytes);
AppendBoolN("captureStderrInBody", _captureStderrInBody);
AppendBoolN("jsonResponse", _jsonResponse);
AppendStr("schedule", _schedule);
return sb.ToString();
}
private static string QuoteIfNeeded(string a)
=> a.Any(char.IsWhiteSpace) ? "\"" + a + "\"" : a;
/// Implicit cast to string — use the builder anywhere a URI string is accepted.
public static implicit operator string(ExecBuilder b) => b.Build();
///
public override string ToString() => Build();
}