:: lib::std::os::arguments
:: lib::std::os::environment;
const stringv ROOT_ENVIRONMENT = "FLECHA_GUARD_ROOT";
values, captureError := arguments::capture();
if captureError != none {
fmt::eprintln("workspace_guard: cannot capture command-line arguments");
command := values[1].view();
if command == "snapshot" or command == "verify" {
result = snapshot_or_verify(values[:], command == "snapshot");
} else if command == "diff" and values.len() == 4 {
result = commands::diff(values[2].view(), values[3].view());
} else if command == "run" and values.len() >= 6 and values[4] == "--" {
childStatus, result = commands::run(
return childStatus.cast<i32>;
} else if command == "help" or command == "--help" or command == "-h" {
result = model::GuardError.Usage;
if result == model::GuardError.Usage {
if result == model::GuardError.InvalidManifest {
fmt::eprintln("workspace_guard: invalid manifest");
if result == model::GuardError.Mismatch {
if result == model::GuardError.UnsupportedEntry {
if result == model::GuardError.ProcessFailure {
fmt::eprintln("workspace_guard: child process terminated abnormally");
fmt::eprintln("workspace_guard: operation failed");
error snapshot_or_verify([]const string values, bool createSnapshot) {
stringv manifestPath = "";
root, copyError = model::copy_text(values[2].view());
manifestPath = values[3].view();
} else if values.len() == 3 {
root, present, environmentError = environment::get(ROOT_ENVIRONMENT);
if environmentError != none {
if !present or root.len() == 0 {
fmt::eprint("workspace_guard: set ");
fmt::eprintln(ROOT_ENVIRONMENT);
return model::GuardError.Usage;
manifestPath = values[2].view();
return model::GuardError.Usage;
return commands::snapshot(root.view(), manifestPath);
return commands::verify(root.view(), manifestPath);
fmt::println("workspace_guard - deterministic workspace snapshots");
fmt::println(" workspace_guard snapshot <root> <manifest>");
fmt::println(" workspace_guard snapshot <manifest> # root from FLECHA_GUARD_ROOT");
fmt::println(" workspace_guard verify <root> <manifest>");
fmt::println(" workspace_guard verify <manifest> # root from FLECHA_GUARD_ROOT");
fmt::println(" workspace_guard diff <old-manifest> <new-manifest>");
fmt::println(" workspace_guard run <root> <manifest> -- <program> [arguments...]");