From 1e3189bd8861ec224ca27a770499569c7dc1bae5 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 01:56:26 +0000 Subject: 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. --- src/main.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src') 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 { 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, + } +} -- cgit v1.3.1