From: Ian Jackson Date: Wed, 19 May 2021 21:35:17 +0000 (+0100) Subject: otter(1): Provide command-{json,ron} for adhoc commands X-Git-Tag: otter-0.6.0~139 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=c19cdfb0220e697bbf48677ed8177312151e6403;p=otter.git otter(1): Provide command-{json,ron} for adhoc commands Signed-off-by: Ian Jackson --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 59bf2a69..d98af03b 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1410,6 +1410,57 @@ impl AdhocFormat { } } +//---------- adhoc command ---------- + +mod command_adhoc { + use super::*; + + #[derive(Default,Debug)] + struct Args { + cmds: Vec, + } + + fn subargs<'ap,'a:'ap,'m:'ap>( + sa: &'a mut Args, + ahf: AdhocFormat, + ) -> ArgumentParser<'ap> { + use argparse::*; + let mut ap = ArgumentParser::new(); + ap.refer(&mut sa.cmds).required() + .add_argument(format!("{}-COMMAND", ahf.metavar()).leak(), + Collect, + format!("{}-encoded MgmtCommand", ahf.name()) + .leak()); + ap + } + + #[throws(AE)] + fn call(sc: &'static Subcommand, ma: MainOpts, args: Vec) { + let ahf = sc.into(); + + let subargs: ApMaker<_> = &|sa| subargs(sa,ahf); + let args = parse_args::(args, subargs, &ok_id, None); + let mut conn = connect(&ma)?; + + let cmds: Vec = ahf.parse(args.cmds, "cmd")?; + for (i, cmd) in cmds.into_iter().enumerate() { + let resp = conn.cmd(&cmd).with_context(|| format!("cmd #{}", i))?; + ahf.report(resp)?; + } + } + + inventory::submit!{Subcommand( + "command-json", + "run ad-hoc management command(s) (JSON)", + call, + )} + inventory::submit!{Subcommand( + "command-ron", + "run ad-hoc management command(s) (Rusty Object Notation)", + call, + )} +} + //---------- alter game ---------- mod alter_game_adhoc {