From 4e7c22194fa308b1b1d57be2cdcbf247ee060d5f Mon Sep 17 00:00:00 2001 From: Samuel Thomas Date: Thu, 15 Aug 2024 13:56:01 -0500 Subject: [PATCH] just use subcommands instead of dynamic subcommands --- fud2/fud-core/src/cli.rs | 61 ++++++++++++----------------------- fud2/fud-core/src/lib.rs | 1 + fud2/src/cli_pyenv.rs | 69 ++++------------------------------------ fud2/src/main.rs | 7 ++-- 4 files changed, 31 insertions(+), 107 deletions(-) diff --git a/fud2/fud-core/src/cli.rs b/fud2/fud-core/src/cli.rs index 9a885755bd..b0d835931c 100644 --- a/fud2/fud-core/src/cli.rs +++ b/fud2/fud-core/src/cli.rs @@ -2,7 +2,7 @@ use crate::config; use crate::exec::{plan, Driver, Request, StateRef}; use crate::run::Run; use anyhow::{anyhow, bail}; -use argh::{DynamicSubCommand, FromArgs}; +use argh::{FromArgs, SubCommand}; use camino::Utf8PathBuf; use std::fmt::Display; use std::str::FromStr; @@ -104,33 +104,16 @@ pub struct GetResource { #[argh(subcommand, name = "list")] pub struct ListCommand {} -pub struct DefaultDynamic {} - -pub trait FakeDynamic: DynamicSubCommand { +pub trait FakeSubCommand: SubCommand { fn run(&self, driver: &Driver) -> anyhow::Result<()>; } -impl DynamicSubCommand for DefaultDynamic { - fn commands() -> &'static [&'static argh::CommandInfo] { - &[] - } - - fn try_redact_arg_values( - _command_name: &[&str], - _args: &[&str], - ) -> Option, argh::EarlyExit>> { - None - } - - fn try_from_args( - _command_name: &[&str], - _args: &[&str], - ) -> Option> { - None - } -} +/// no extra command +#[derive(FromArgs)] +#[argh(subcommand, name = "none")] +pub struct DefaultDynamic {} -impl FakeDynamic for DefaultDynamic { +impl FakeSubCommand for DefaultDynamic { fn run(&self, _driver: &Driver) -> anyhow::Result<()> { Ok(()) } @@ -141,7 +124,7 @@ impl FakeDynamic for DefaultDynamic { #[argh(subcommand)] pub enum Subcommand where - T: FakeDynamic, + T: FakeSubCommand, { /// edit the configuration file EditConfig(EditConfig), @@ -152,15 +135,14 @@ where /// list the available states and ops List(ListCommand), - #[argh(dynamic)] - Dynamic(T), + Extended(T), } #[derive(FromArgs)] /// A generic compiler driver. pub struct FakeArgs where - T: FakeDynamic, + T: FakeSubCommand, { #[argh(subcommand)] pub sub: Option>, @@ -246,7 +228,7 @@ fn get_states_with_errors( Ok(states) } -fn from_states( +fn from_states( driver: &Driver, args: &FakeArgs, ) -> anyhow::Result> { @@ -260,7 +242,7 @@ fn from_states( ) } -fn to_state( +fn to_state( driver: &Driver, args: &FakeArgs, ) -> anyhow::Result> { @@ -274,7 +256,7 @@ fn to_state( ) } -fn get_request( +fn get_request( driver: &Driver, args: &FakeArgs, ) -> anyhow::Result { @@ -362,8 +344,10 @@ fn get_resource(driver: &Driver, cmd: GetResource) -> anyhow::Result<()> { } /// Given the name of a Driver, returns a config based on that name and CLI arguments. -pub fn config_from_cli(name: &str) -> anyhow::Result { - let args: FakeArgs = argh::from_env(); +pub fn config_from_cli( + name: &str, +) -> anyhow::Result { + let args: FakeArgs = argh::from_env(); let mut config = config::load_config(name); // Use `--set` arguments to override configuration values. @@ -380,16 +364,11 @@ pub fn config_from_cli(name: &str) -> anyhow::Result { Ok(config) } -pub fn cli(driver: &Driver, config: &figment::Figment) -> anyhow::Result<()> { - let args: FakeArgs = argh::from_env(); - cli_dynamic(args, driver, config) -} - -pub fn cli_dynamic( - args: FakeArgs, +pub fn cli( driver: &Driver, config: &figment::Figment, ) -> anyhow::Result<()> { + let args: FakeArgs = argh::from_env(); // Configure logging. env_logger::Builder::new() .format_timestamp(None) @@ -409,7 +388,7 @@ pub fn cli_dynamic( driver.print_info(); return Ok(()); } - Some(Subcommand::Dynamic(cmd)) => { + Some(Subcommand::Extended(cmd)) => { return cmd.run(driver); } None => {} diff --git a/fud2/fud-core/src/lib.rs b/fud2/fud-core/src/lib.rs index 161b8463f1..a7b9ed3ba8 100644 --- a/fud2/fud-core/src/lib.rs +++ b/fud2/fud-core/src/lib.rs @@ -5,4 +5,5 @@ pub mod run; pub mod script; pub mod utils; +pub use cli::DefaultDynamic; pub use exec::{Driver, DriverBuilder}; diff --git a/fud2/src/cli_pyenv.rs b/fud2/src/cli_pyenv.rs index 7aea3c9e59..80bf4ae4ae 100644 --- a/fud2/src/cli_pyenv.rs +++ b/fud2/src/cli_pyenv.rs @@ -1,69 +1,14 @@ -use std::{fs, path::Path, process::Command, sync::OnceLock}; +use std::{fs, path::Path, process::Command}; -use argh::{CommandInfo, DynamicSubCommand, EarlyExit}; -use fud_core::{cli::FakeDynamic, config}; +use argh::FromArgs; +use fud_core::{cli::FakeSubCommand, config}; -#[derive(PartialEq, Debug)] +/// initialize a fud2 python environment +#[derive(FromArgs)] +#[argh(subcommand, name = "env")] pub struct PyenvCommand {} -impl DynamicSubCommand for PyenvCommand { - fn commands() -> &'static [&'static CommandInfo] { - static RET: OnceLock> = OnceLock::new(); - RET.get_or_init(|| { - let mut commands = Vec::new(); - - let env_cmdinfo = CommandInfo { - name: "env", - description: "initialize fud2 python environment", - }; - - commands.push(&*Box::leak(Box::new(env_cmdinfo))); - - commands - }) - } - - fn try_redact_arg_values( - command_name: &[&str], - args: &[&str], - ) -> Option, EarlyExit>> { - for command in Self::commands() { - if command_name.last() == Some(&command.name) { - // Process arguments and redact values here. - if !args.is_empty() { - return Some(Err( - "Our example dynamic command never takes arguments!" - .to_string() - .into(), - )); - } - return Some(Ok(Vec::new())); - } - } - None - } - - fn try_from_args( - command_name: &[&str], - args: &[&str], - ) -> Option> { - for command in Self::commands() { - if command_name.last() == Some(&command.name) { - if !args.is_empty() { - return Some(Err( - "Our example dynamic command never takes arguments!" - .to_string() - .into(), - )); - } - return Some(Ok(PyenvCommand {})); - } - } - None - } -} - -impl FakeDynamic for PyenvCommand { +impl FakeSubCommand for PyenvCommand { fn run(&self, driver: &fud_core::Driver) -> anyhow::Result<()> { let data_dir = config::data_dir(&driver.name); diff --git a/fud2/src/main.rs b/fud2/src/main.rs index 61ef39e591..c3f92b1c6c 100644 --- a/fud2/src/main.rs +++ b/fud2/src/main.rs @@ -1,6 +1,6 @@ use fud2::PyenvCommand; use fud_core::{ - cli::{self, FakeArgs}, + cli::{self}, DriverBuilder, }; @@ -41,7 +41,7 @@ fn main() -> anyhow::Result<()> { } // Get config values from cli. - let config = cli::config_from_cli(&bld.name)?; + let config = cli::config_from_cli::(&bld.name)?; #[cfg(feature = "migrate_to_scripts")] { @@ -49,6 +49,5 @@ fn main() -> anyhow::Result<()> { } let driver = bld.build(); - let args: FakeArgs = argh::from_env(); - cli::cli_dynamic(args, &driver, &config) + cli::cli::(&driver, &config) }