use std::io::{self, IsTerminal, Write}; use std::process::{Command, Stdio}; const H1: &str = "\x1b[1;91m"; const H2: &str = "\x1b[1;36m"; const CODE: &str = "\x1b[1;94m"; const MARK: &str = "\x1b[93m"; const PIPE: &str = "\x1b[94m"; const BOLD: &str = "\x1b[1m"; const RESET: &str = "\x1b[0m"; fn color_enabled() -> bool { io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none() } struct Paint { on: bool, } impl Paint { fn new() -> Self { Self { on: color_enabled(), } } fn wrap(&self, style: &str, text: &str) -> String { if self.on { format!("{style}{text}{RESET}") } else { text.to_string() } } fn h1(&self, text: &str) -> String { self.wrap(H1, text) } fn h2(&self, text: &str) -> String { self.wrap(H2, text) } fn code(&self, text: &str) -> String { self.wrap(CODE, text) } fn bold(&self, text: &str) -> String { self.wrap(BOLD, text) } fn bullet(&self) -> String { self.wrap(MARK, "·") } fn pipe(&self) -> String { self.wrap(PIPE, "│") } } pub fn maybe_print(args: &[String]) -> bool { let skip0 = !args.is_empty(); let argv: Vec<&str> = args .iter() .skip(if skip0 { 1 } else { 0 }) .map(String::as_str) .collect(); if let Some(rest) = argv.strip_prefix(&["help"][..]) { let topic = rest .iter() .copied() .find(|a| !a.starts_with('-')) .unwrap_or(""); print_topic(topic); return true; } let wants_help = argv.iter().any(|a| *a == "--help" || *a == "-h"); if !wants_help { return false; } let topic = argv .iter() .copied() .find(|a| !a.starts_with('-') && *a != "help") .unwrap_or(""); print_topic(topic); true } fn print_topic(topic: &str) { let p = Paint::new(); let body = match topic { "" | "sope" => root_help(&p), "build" => build_help(&p), "render" => render_help(&p), "plan" => plan_help(&p), "deploy" => deploy_help(&p), "status" => status_help(&p), "ui" => ui_help(&p), "destroy" => destroy_help(&p), "help" => help_help(&p), other => format!( "{} error: {} unknown help topic '{}'\n\n{}", p.wrap(H1, "error:"), p.code("sope help"), other, root_help(&p) ), }; page_or_print(&body); } fn page_or_print(text: &str) { if let Some(spec) = pager_spec() { if run_pager(&spec, text) { return; } } let _ = io::stdout().write_all(text.as_bytes()); } fn pager_spec() -> Option { if !io::stdout().is_terminal() { return None; } let raw = std::env::var("SOPE_PAGER") .or_else(|_| std::env::var("NIX_PAGER")) .or_else(|_| std::env::var("PAGER")); match raw.as_deref() { Ok("") | Ok("cat") => None, Ok(cmd) => Some(cmd.to_string()), Err(_) => Some(String::new()), } } fn run_pager(spec: &str, text: &str) -> bool { if spec.is_empty() { spawn_pager_argv(&["less"], text) || spawn_pager_argv(&["more"], text) } else { spawn_pager_argv(&["sh", "-c", spec], text) } } fn spawn_pager_argv(argv: &[&str], text: &str) -> bool { let (prog, args) = match argv.split_first() { Some((p, rest)) => (*p, rest), None => return false, }; let mut cmd = Command::new(prog); cmd.args(args).stdin(Stdio::piped()); if std::env::var_os("LESS").is_none() { cmd.env("LESS", "FRSXMK"); } let mut child = match cmd.spawn() { Ok(c) => c, Err(_) => return false, }; if let Some(mut stdin) = child.stdin.take() { let _ = stdin.write_all(text.as_bytes()); } child.wait().is_ok() } fn section(p: &Paint, title: &str) -> String { format!("\n{}\n\n", p.h1(title)) } fn h2(p: &Paint, title: &str) -> String { format!("{}\n\n", p.h2(title)) } fn bullet_cmd(p: &Paint, cmd: &str, desc: &str) -> String { format!(" {} {} - {} \n", p.bullet(), p.code(cmd), desc) } fn bullet_text(p: &Paint, text: &str) -> String { format!(" {} {}\n", p.bullet(), text) } fn console(p: &Paint, lines: &[&str]) -> String { let mut s = String::new(); for line in lines { s.push_str(&format!(" {} {}\n", p.pipe(), line)); } s } fn root_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - deploy an observability stack (hub / satellites)\n", p.code("sope") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}...] {}\n\n", p.code("sope"), p.bold("option"), p.bold("subcommand") )); s.push_str("where subcommand is one of the following:\n\n"); s.push_str(&h2(p, "Main commands:")); s.push_str(&bullet_cmd( p, "sope build", "build Nix dockerTools images and load them into Docker", )); s.push_str(&bullet_cmd( p, "sope render", "generate Compose/config artefacts without deploying", )); s.push_str(&bullet_cmd( p, "sope plan", "show required changes vs remote (configs / images)", )); s.push_str(&bullet_cmd( p, "sope deploy", "apply only needed changes (use --full to force)", )); s.push_str(&bullet_cmd( p, "sope status", "show docker compose ps on hub / satellites", )); s.push_str(&bullet_cmd( p, "sope ui", "local web UI for status, deploy, and destroy", )); s.push_str(&bullet_cmd( p, "sope destroy", "tear down remote infra with no leftover traces", )); s.push_str("\n"); s.push_str(&h2(p, "Help commands:")); s.push_str(&bullet_cmd( p, "sope help", "show help about sope or a particular subcommand", )); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Deploy hub then every satellite:")); s.push('\n'); s.push_str(&console(p, &["# sope deploy"])); s.push('\n'); s.push_str(&bullet_text(p, "Open the local admin UI:")); s.push('\n'); s.push_str(&console(p, &["# sope ui", "# open http://127.0.0.1:8743"])); s.push('\n'); s.push_str(&bullet_text(p, "Tear down everything:")); s.push('\n'); s.push_str(&console(p, &["# sope destroy all --yes"])); s.push_str(§ion(p, "Options")); s.push_str(&bullet_text( p, &format!( "{} / {} — path to sope.toml (default: {})", p.code("-c"), p.code("--config"), p.code("sope.toml") ), )); s.push_str(&bullet_text( p, &format!("{} / {} — show help", p.code("-h"), p.code("--help")), )); s.push_str(§ion(p, "Installation")); s.push_str(&bullet_text( p, &format!( "Install: {}", p.code("nix profile install 'git+https://tangled.org/primica.tngl.sh/sope'") ), )); s.push_str(&bullet_text( p, &format!( "Run once: {}", p.code("nix run 'git+https://tangled.org/primica.tngl.sh/sope' -- --help") ), )); s.push_str(&bullet_text( p, &format!( "Repo: {}", p.code("https://tangled.org/primica.tngl.sh/sope") ), )); s.push_str(§ion(p, "See also")); s.push_str(&format!( " {} {}\n {} {}\n {} {}\n", p.bullet(), p.code("man sope"), p.bullet(), p.code("man 5 sope.toml"), p.bullet(), p.code("sope help deploy") )); s } fn build_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - build sope Docker images with Nix\n", p.code("sope build") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!("{} [{}...]\n", p.code("sope build"), p.bold("option"))); s.push_str(§ion(p, "Description")); s.push_str(&format!( "Runs {} then {} for each archive. On macOS the build uses a {} container;\narchives land in {} then load locally.\n", p.code("nix build .#images"), p.code("docker load"), p.code("nixos/nix"), p.code(".sope-image-tars/") )); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Build all images:")); s.push('\n'); s.push_str(&console(p, &["# sope build"])); s.push_str(§ion(p, "Options")); s.push_str(&global_opts(p)); s } fn render_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - generate Compose and config artefacts without deploying\n", p.code("sope render") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}...] [{} {}]\n", p.code("sope render"), p.bold("option"), p.code("--out"), p.bold("dir") )); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Preview artefacts:")); s.push('\n'); s.push_str(&console( p, &["# sope render --out /tmp/sope-preview", "# ls /tmp/sope-preview/satellites/"], )); s.push_str(§ion(p, "Options")); s.push_str(&bullet_text( p, &format!( "{} / {} {} — output directory (default: {})", p.code("-o"), p.code("--out"), p.bold("dir"), p.code(".sope") ), )); s.push_str(&global_opts(p)); s } fn plan_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - show what deploy would change\n", p.code("sope plan") )); s.push_str(§ion(p, "Description")); s.push_str( "Renders sope.toml and diffs against sope.state.json (Terraform-style).\n\ With --refresh, re-reads remotes into the state first. Prints create /\n\ config-sync / image-refresh / up-to-date. Example: grafana.admin_password\n\ only lists hub compose + grafana.ini.\n", ); s.push_str(§ion(p, "Examples")); s.push_str(&console( p, &["# sope plan", "# sope plan --refresh", "# sope plan hub"], )); s.push_str(&global_opts(p)); s } fn deploy_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - deploy hub and/or satellites over SSH\n", p.code("sope deploy") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}...] [{}] [{} {}] [{}] [{}]\n", p.code("sope deploy"), p.bold("option"), p.bold("target"), p.code("--name"), p.bold("name"), p.code("--skip-build"), p.code("--full") )); s.push_str(§ion(p, "Description")); s.push_str( "Runs the same diff as sope plan, then applies only what is needed:\n\ - config changes → upload files + docker compose up -d (no image rebuild)\n\ - missing stack / image drift → full atomic deploy (stage, load, swap)\n\ Use --full to force atomic redeploy. Use --skip-build when images are ready.\n", ); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Deploy everything:")); s.push('\n'); s.push_str(&console(p, &["# sope deploy"])); s.push('\n'); s.push_str(&bullet_text(p, "Deploy one satellite without rebuilding:")); s.push('\n'); s.push_str(&console( p, &["# sope deploy satellite --name node-a --skip-build"], )); s.push_str(§ion(p, "Arguments")); s.push_str(&bullet_text( p, &format!( "{} — {}, {}, or {} (default: {})", p.bold("target"), p.code("all"), p.code("hub"), p.code("satellite"), p.code("all") ), )); s.push_str(§ion(p, "Options")); s.push_str(&bullet_text( p, &format!( "{} {} — required when target is {}", p.code("--name"), p.bold("name"), p.code("satellite") ), )); s.push_str(&bullet_text( p, &format!("{} — reuse local images", p.code("--skip-build")), )); s.push_str(&global_opts(p)); s } fn status_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - show Compose status on hub / satellites\n", p.code("sope status") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}...] [{}] [{} {}]\n", p.code("sope status"), p.bold("option"), p.bold("target"), p.code("--name"), p.bold("name") )); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Status of every node:")); s.push('\n'); s.push_str(&console(p, &["# sope status"])); s.push('\n'); s.push_str(&bullet_text(p, "One satellite:")); s.push('\n'); s.push_str(&console(p, &["# sope status satellite --name node-c"])); s.push_str(§ion(p, "Options")); s.push_str(&bullet_text( p, &format!( "{} {} — required when target is {}", p.code("--name"), p.bold("name"), p.code("satellite") ), )); s.push_str(&global_opts(p)); s } fn ui_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - local web UI for status, deploy, and destroy\n", p.code("sope ui") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}...] [{} {}] [{} {}]\n", p.code("sope ui"), p.bold("option"), p.code("--bind"), p.bold("addr"), p.code("--port"), p.bold("port") )); s.push_str(§ion(p, "Description")); s.push_str(&format!( "Starts a Topcoat UI on the operator machine. Default listen address is {}.\nNo authentication in V1 — keep it on localhost.\n", p.code("127.0.0.1:8743") )); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Start the UI:")); s.push('\n'); s.push_str(&console(p, &["# sope ui", "# sope ui --port 9000"])); s.push_str(§ion(p, "Options")); s.push_str(&bullet_text( p, &format!( "{} {} — listen address (default: {})", p.code("--bind"), p.bold("addr"), p.code("127.0.0.1") ), )); s.push_str(&bullet_text( p, &format!( "{} {} — HTTP port (default: {})", p.code("--port"), p.bold("port"), p.code("8743") ), )); s.push_str(&global_opts(p)); s } fn destroy_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - tear down remote infra with no leftover traces\n", p.code("sope destroy") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}...] [{}] [{} {}] {}\n", p.code("sope destroy"), p.bold("option"), p.bold("target"), p.code("--name"), p.bold("name"), p.code("--yes") )); s.push_str(§ion(p, "Description")); s.push_str("On each target: docker compose down -v, rm -rf the remote path, remove sope/* images.\nFor all, satellites are destroyed before the hub. Irreversible — --yes is required.\n"); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Destroy everything:")); s.push('\n'); s.push_str(&console(p, &["# sope destroy all --yes"])); s.push_str(§ion(p, "Options")); s.push_str(&bullet_text( p, &format!( "{} {} — required when target is {}", p.code("--name"), p.bold("name"), p.code("satellite") ), )); s.push_str(&bullet_text( p, &format!("{} — confirm irreversible destruction", p.code("--yes")), )); s.push_str(&global_opts(p)); s } fn help_help(p: &Paint) -> String { let mut s = String::new(); s.push_str(&format!("{}\n\n", p.h1("Name"))); s.push_str(&format!( "{} - show help about sope or a particular subcommand\n", p.code("sope help") )); s.push_str(§ion(p, "Synopsis")); s.push_str(&format!( "{} [{}]\n", p.code("sope help"), p.bold("subcommand") )); s.push_str(§ion(p, "Examples")); s.push_str(&bullet_text(p, "Top-level help:")); s.push('\n'); s.push_str(&console(p, &["# sope help"])); s.push('\n'); s.push_str(&bullet_text(p, "Help for deploy:")); s.push('\n'); s.push_str(&console(p, &["# sope help deploy", "# sope deploy --help"])); s } fn global_opts(p: &Paint) -> String { let mut s = String::new(); s.push_str(&bullet_text( p, &format!( "{} / {} — path to sope.toml (default: {})", p.code("-c"), p.code("--config"), p.code("sope.toml") ), )); s.push_str(&bullet_text( p, &format!("{} / {} — show help", p.code("-h"), p.code("--help")), )); s } pub fn clap_styles() -> clap::builder::Styles { use clap::builder::styling::{AnsiColor, Effects, Styles}; Styles::styled() .header(AnsiColor::BrightRed.on_default().effects(Effects::BOLD)) .usage(AnsiColor::Cyan.on_default().effects(Effects::BOLD)) .literal(AnsiColor::BrightBlue.on_default().effects(Effects::BOLD)) .placeholder(AnsiColor::Cyan.on_default()) .error(AnsiColor::BrightRed.on_default().effects(Effects::BOLD)) .valid(AnsiColor::Green.on_default().effects(Effects::BOLD)) .invalid(AnsiColor::Yellow.on_default().effects(Effects::BOLD)) }