From c19cdfb0220e697bbf48677ed8177312151e6403 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 19 May 2021 22:35:17 +0100 Subject: [PATCH] otter(1): Provide command-{json,ron} for adhoc commands Signed-off-by: Ian Jackson --- src/bin/otter.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) 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 { -- 2.30.2