diff options
| author | tv <tv@krebsco.de> | 2026-12-01 01:56:26 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 01:56:26 +0000 |
| commit | 1e3189bd8861ec224ca27a770499569c7dc1bae5 (patch) | |
| tree | 349015edc2c8cb265ed6bdce13d06723c7dd23ab | |
| parent | 0a32f2f57d9e928b7f657decd81c0658eb416e37 (diff) | |
enstate: a setting the command doesn't use, as an argument, is an error
--state-dir and --run-dir with --pack, --systemctl but with --apply and
--migrate, --unit but with --apply. From a configuration file, which
holds them for every command, they are fine. The completion offers
each command the settings it uses.
| -rw-r--r-- | share/bash-completion/completions/enstate | 18 | ||||
| -rw-r--r-- | src/main.rs | 24 | ||||
| -rw-r--r-- | tests/receptor.rs | 17 |
3 files changed, 51 insertions, 8 deletions
diff --git a/share/bash-completion/completions/enstate b/share/bash-completion/completions/enstate index 25a8976..2900ba1 100644 --- a/share/bash-completion/completions/enstate +++ b/share/bash-completion/completions/enstate @@ -1,11 +1,14 @@ # bash completion for enstate: a command until one is given, then the options it -# takes (each once, but --config and --unset), and for PATH a submission (a -# directory) or a source list (a file). +# takes and the settings it uses (each once, but --config and --unset), and for +# PATH a submission (a directory) or a source list (a file). _enstate() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} local commands="--plan --apply --stage --status --materialize --migrate --pack" - local settings="--config --unset --state-dir --run-dir --schema --unit --print-config" + # the settings, and those each command uses + local general="--config --unset --print-config" + local settings="$general --state-dir --run-dir --schema --unit" + local store="$general --state-dir --run-dir --schema" local i w cmd= path= used=" " # bash splits --option=VALUE into --option, =, VALUE if [ "$prev" = = ] && [ "$COMP_CWORD" -ge 2 ]; then @@ -36,10 +39,11 @@ _enstate() { local options case $cmd in "") options="$commands -h --help $settings" ;; - plan | apply | stage) options="--json $settings" ;; - status | materialize) options="--json $settings" ;; - migrate) options="--json --dry-run $settings" ;; - pack) options="--out $settings" ;; + plan | stage) options="--json $store" ;; + apply) options="--json $store --unit" ;; + status | materialize) options="--json $store" ;; + migrate) options="--json --dry-run $store" ;; + pack) options="--out $general --schema" ;; esac local o candidates= for o in $options; do diff --git a/src/main.rs b/src/main.rs index 5ae94e1..9133cb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf}; use std::process::ExitCode; use anyhow::{Context, Result, bail}; +use clap::parser::ValueSource; use clap::{Arg, ArgAction, ArgGroup, Command}; use config::option::{Kind, OptionSpec, option}; use serde::Deserialize; @@ -226,6 +227,15 @@ fn run() -> Result<ExitCode> { if json && command == "pack" { bail!("--json is not for --pack"); } + // A setting given as an argument that the command doesn't use is an error; from a + // configuration file, which holds them for every command, it isn't. + for spec in OPTIONS { + if matches.value_source(spec.name) == Some(ValueSource::CommandLine) + && !uses(command, spec.name) + { + bail!("--{} is not for --{command}", spec.name); + } + } if command == "pack" { let out = out.context("--pack needs --out DIR")?; let schema = settings.schema.as_deref().map(Schema::load).transpose()?; @@ -620,3 +630,17 @@ fn print_migration(migration: &enstate::migrate::Migration) -> Result<()> { } Ok(()) } + +/// Whether `command` uses the setting `setting`. +fn uses(command: &str, setting: &str) -> bool { + match setting { + // Where the snapshots and views are: everything but making a submission. + "state-dir" | "run-dir" => command != "pack", + // The schema to plan against, or, for --pack, to check against. + "schema" => true, + // Reactions run. + "systemctl" => matches!(command, "apply" | "migrate"), + "unit" => command == "apply", + _ => true, + } +} diff --git a/tests/receptor.rs b/tests/receptor.rs index 56a1666..f8417a3 100644 --- a/tests/receptor.rs +++ b/tests/receptor.rs @@ -508,7 +508,11 @@ fn command_line() { "--schema={}", host.root.join("schema.json").display() )) - .arg(format!("--systemctl={}", host.systemctl().display())) + .args( + arguments + .contains(&"--apply") + .then(|| format!("--systemctl={}", host.systemctl().display())), + ) .output() .unwrap(); ( @@ -556,6 +560,17 @@ fn command_line() { "{stdout}" ); // Options not for the command, and two commands. + // Settings the command doesn't use, as arguments; from a file they are fine. + let (code, _, stderr) = enstate(&["--status", "--unit=x.service"]); + assert_eq!(code, Some(2)); + assert!(stderr.contains("--unit is not for --status"), "{stderr}"); + let (code, _, stderr) = enstate(&["--plan", missing, "--systemctl=/bin/true"]); + assert_eq!(code, Some(2)); + assert!(stderr.contains("--systemctl is not for --plan"), "{stderr}"); + std::fs::write(host.root.join("unit.json"), r#"{"unit": "x.service"}"#).unwrap(); + let config = format!("--config={}", host.root.join("unit.json").display()); + let (code, _, stderr) = enstate(&["--status", &config]); + assert_eq!(code, Some(0), "{stderr}"); let (code, _, stderr) = enstate(&["--status", "--out", "x"]); assert_eq!(code, Some(2)); assert!(stderr.contains("--out is for --pack only"), "{stderr}"); |
