surjection 1.1.0

Surjection CLI: manage MCP servers, skills and secret references for AI coding clients
//! `loadout`: the command of Loadout 1.0.x, kept so client entries,
//! scripts and habits that still run it keep working. It forwards its
//! arguments, stdin, stdout, stderr and exit code to `surject` (the one
//! next to this binary first, then `PATH`). It prints one line,
//! "loadout is now surject", on stderr, except for `exec` and
//! `secret get`, whose stdout is an MCP transport or a value a client
//! reads and whose stderr a client may log.
//!
//! Built only with the `legacy-shim` feature.

use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{Command, ExitCode};

const TARGET: &str = "surject";
const NOTICE: &str = "loadout is now surject";

fn main() -> ExitCode {
    run()
}

/// The whole shim (also built as the testkit's `loadout-shim` for tests).
pub fn run() -> ExitCode {
    let args: Vec<OsString> = std::env::args_os().skip(1).collect();
    if !is_silent(&args) {
        eprintln!("{NOTICE}");
    }
    let program = sibling().unwrap_or_else(|| PathBuf::from(TARGET));
    match Command::new(&program).args(&args).status() {
        Ok(status) => match status.code() {
            Some(code) => ExitCode::from(u8::try_from(code & 0xff).unwrap_or(1)),
            None => ExitCode::from(1),
        },
        Err(err) => {
            eprintln!("loadout: cannot run {}: {err}", program.display());
            ExitCode::from(127)
        }
    }
}

/// `surject` in this binary's folder, when it exists.
fn sibling() -> Option<PathBuf> {
    let exe = std::env::current_exe().ok()?;
    let path = exe
        .parent()?
        .join(format!("{TARGET}{}", std::env::consts::EXE_SUFFIX));
    path.is_file().then_some(path)
}

/// Whether the command is `exec` or `secret get` (after global flags; the
/// value of `--home <PATH>` is skipped).
fn is_silent(args: &[OsString]) -> bool {
    let mut words = Vec::new();
    let mut iter = args.iter().map(|arg| arg.to_string_lossy());
    while let Some(arg) = iter.next() {
        if arg == "--home" {
            iter.next();
        } else if !arg.starts_with('-') {
            words.push(arg);
            if words.len() == 2 {
                break;
            }
        }
    }
    match words.first().map(AsRef::as_ref) {
        Some("exec") => true,
        Some("secret") => words.get(1).map(AsRef::as_ref) == Some("get"),
        _ => false,
    }
}