From 0d4e4cae7fd9e7e66fed67e7577d0c6f35a6dfe4 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 3 Feb 2021 08:54:12 +0100 Subject: [PATCH] tape: improve pmt command line completion --- src/bin/pmt.rs | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/bin/pmt.rs b/src/bin/pmt.rs index 8d651064..13bb057c 100644 --- a/src/bin/pmt.rs +++ b/src/bin/pmt.rs @@ -48,6 +48,7 @@ pub const DRIVE_OPTION_SCHEMA: Schema = StringSchema::new( pub const DRIVE_OPTION_LIST_SCHEMA: Schema = ArraySchema::new("Drive Option List.", &DRIVE_OPTION_SCHEMA) + .min_length(1) .schema(); use proxmox_backup::{ @@ -702,25 +703,38 @@ fn status(param: Value) -> Result<(), Error> { schema: DRIVE_OPTION_LIST_SCHEMA, optional: true, }, + defaults: { + description: "Set default options (buffer-writes async-writes read-ahead can-bsr).", + type: bool, + optional: true, + }, }, }, )] /// Set device driver options (root only) -/// -/// If no options specified, we reset to default options -/// (buffer-writes async-writes read-ahead can-bsr) -fn st_options(options: Option>, param: Value) -> Result<(), Error> { +fn st_options( + options: Option>, + defaults: Option, + param: Value) -> Result<(), Error> { let handle = get_tape_handle(¶m)?; - let options = options.unwrap_or_else(|| { - let mut list = Vec::new(); - list.push(String::from("buffer-writes")); - list.push(String::from("async-writes")); - list.push(String::from("read-ahead")); - list.push(String::from("can-bsr")); - list - }); + let options = match defaults { + Some(true) => { + if options.is_some() { + bail!("option --defaults conflicts with specified options"); + } + let mut list = Vec::new(); + list.push(String::from("buffer-writes")); + list.push(String::from("async-writes")); + list.push(String::from("read-ahead")); + list.push(String::from("can-bsr")); + list + } + Some(false) | None => { + options.unwrap_or_else(|| Vec::new()) + } + }; let value = parse_drive_options(options)?; @@ -904,6 +918,7 @@ fn main() -> Result<(), Error> { CliCommand::new(method) .completion_cb("drive", complete_drive_name) .completion_cb("device", complete_drive_path) + .completion_cb("options", complete_option_name) }; let cmd_def = CliCommandMap::new() @@ -939,3 +954,11 @@ fn main() -> Result<(), Error> { Ok(()) } + +// Completion helpers +pub fn complete_option_name(_arg: &str, _param: &HashMap) -> Vec { + DRIVE_OPTIONS + .keys() + .map(String::from) + .collect() +}