From 8ac495bee4f09a8a1ff239f120800f5ae738afc1 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 26 May 2021 21:55:21 -0700 Subject: refactor(cli): restructure subcommand matching, removes shell completions subcommand --- crates/whirl/src/cli.rs | 62 +++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/crates/whirl/src/cli.rs b/crates/whirl/src/cli.rs index 861fb13..52a1858 100644 --- a/crates/whirl/src/cli.rs +++ b/crates/whirl/src/cli.rs @@ -1,7 +1,7 @@ // Copyleft (ɔ) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only -use structopt::clap::{App, AppSettings, Arg, ArgMatches, Shell, SubCommand}; +use structopt::clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; use whirl_config::Config; use crate::subs::run; @@ -25,37 +25,27 @@ impl Cli { trace!("trace"); } - if matches.is_present("run") { - run().await; - } else if let Some(cmd) = matches.subcommand_matches("config") { - if cmd.is_present("show") { - println!("{:#?}", Config::get()); - } - } else if let Some(shell) = matches.subcommand_matches("completions") { - if shell.is_present("powershell") { - Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::PowerShell, "."); - } else if shell.is_present("bash") { - Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Bash, "."); - } else if shell.is_present("elvish") { - Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Elvish, "."); - } else if shell.is_present("zsh") { - Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Zsh, "."); - } else if shell.is_present("fish") { - Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Fish, "."); - } - debug!("generated shell completions"); - } else if matches.is_present("clean") { - let cleanable_directories = vec!["log/"]; - for dir in cleanable_directories { - let mut file_type = "directory"; - if !dir.ends_with('/') { - file_type = "file"; - } - println!("cleaning {}: {}", file_type, dir); - if let Err(e) = std::fs::remove_dir_all(dir) { - warn!("cannot delete {}: {}: {}", file_type, dir, e); + match matches.subcommand() { + ("run", _) => run().await, + ("config", Some(s_matches)) => + match s_matches.subcommand() { + ("show", _) => println!("{:#?}", Config::get()), + _ => unreachable!(), + }, + ("clean", _) => { + let cleanable_directories = vec!["log/"]; + for dir in cleanable_directories { + let mut file_type = "directory"; + if !dir.ends_with('/') { + file_type = "file"; + } + info!("cleaning {}: {}", file_type, dir); + if let Err(e) = std::fs::remove_dir_all(dir) { + warn!("cannot delete {}: {}: {}", file_type, dir, e); + } } } + _ => unreachable!(), } Ok(()) @@ -72,18 +62,8 @@ impl Cli { SubCommand::with_name("config") .setting(AppSettings::SubcommandRequiredElseHelp) .subcommands(vec![SubCommand::with_name("show")]), - SubCommand::with_name("completions") - .setting(AppSettings::SubcommandRequiredElseHelp) - .about("Generate shell completions") - .subcommands(vec![ - SubCommand::with_name("powershell"), - SubCommand::with_name("bash"), - SubCommand::with_name("elvish"), - SubCommand::with_name("zsh"), - SubCommand::with_name("fish"), - ]), SubCommand::with_name("clean") - .about("Delete Whirl generated files/ directories which are NOT critical. E.g., logs/"), + .about("Delete Whirl-generated files/ directories which are NOT critical. E.g., logs/"), ]) .args(&[ Arg::with_name("debug").short("d").long("debug"), -- cgit v1.2.3