From 788669ec06618ca45a2eee8d99893fa356d29fda Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Tue, 30 Jul 2024 16:32:33 -0400 Subject: [PATCH] fix serde_json::Value clap parsing --- zone-setup/src/bin/zone-setup.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/zone-setup/src/bin/zone-setup.rs b/zone-setup/src/bin/zone-setup.rs index b2b1941f86..f335512d83 100644 --- a/zone-setup/src/bin/zone-setup.rs +++ b/zone-setup/src/bin/zone-setup.rs @@ -5,7 +5,9 @@ //! CLI to set up zone configuration use anyhow::{anyhow, Context}; -use clap::builder::NonEmptyStringValueParser; +use clap::builder::{ + NonEmptyStringValueParser, StringValueParser, TypedValueParser, +}; use clap::{ArgAction, Args, Parser, Subcommand}; use illumos_utils::addrobj::{AddrObject, IPV6_LINK_LOCAL_ADDROBJ_NAME}; use illumos_utils::ipadm::Ipadm; @@ -104,13 +106,19 @@ struct ChronySetupArgs { allow: Ipv6Net, } +// The default clap parser for `serde_json::Value` is to wrap the argument in a +// `Value::String`; this value parser parses the argument as JSON instead. +fn json_value_parser() -> impl TypedValueParser { + StringValueParser::new().try_map(|s| s.parse()) +} + #[derive(Debug, Args)] struct SwitchZoneArgs { /// path to the baseboard file #[arg(short, long, default_value = SWITCH_ZONE_BASEBOARD_FILE)] baseboard_file: PathBuf, /// baseboard info JSON blob - #[arg(short = 'i', long)] + #[arg(short = 'i', long, value_parser = json_value_parser())] baseboard_info: serde_json::Value, /// bootstrap IPv6 address #[arg(short = 'a', long)]