From cf1c5aafd50d9364a6115794b8cf72033a544b2c Mon Sep 17 00:00:00 2001 From: Neelay Date: Tue, 5 Nov 2024 21:49:36 -0800 Subject: [PATCH] default subcommand is run --- insanity-native-tui-app/src/main.rs | 62 ++++++++++++++--------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/insanity-native-tui-app/src/main.rs b/insanity-native-tui-app/src/main.rs index 2f30007..74ec0fb 100644 --- a/insanity-native-tui-app/src/main.rs +++ b/insanity-native-tui-app/src/main.rs @@ -17,23 +17,9 @@ static BREAKING_CHANGE_VERSION: &str = "1"; #[clap(version = built_info::GIT_VERSION, author = "Nicolas Chan ")] struct Cli { #[command(subcommand)] - command: Commands, -} + command: Option, -#[derive(Subcommand, Debug)] -enum Commands { - Run(RunOptions), - Update { - #[clap(long, default_value_t = false)] - dry_run: bool, - - #[clap(long, default_value_t = false)] - force: bool, - }, -} - -#[derive(Parser, Debug)] -struct RunOptions { + /// Listen port. #[clap(short, long, default_value_t = 0)] port: u16, @@ -62,6 +48,26 @@ struct RunOptions { ip_version: IpVersion, } +#[derive(Subcommand, Debug)] +enum Commands { + Update { + #[clap(long, default_value_t = false)] + dry_run: bool, + + #[clap(long, default_value_t = false)] + force: bool, + }, +} + +#[derive(Debug)] +struct RunOptions { + port: u16, + no_tui: bool, + bridge: Vec, + room: Option, + ip_version: IpVersion, +} + // RunOptions that can be specified via config file // Has to exclude: config file path, dir #[derive(Deserialize, Debug, Default)] @@ -82,11 +88,7 @@ fn merge_values(primary: T, secondary: Option, value_source: Option RunOptions { +fn merge_configs(primary: Cli, secondary: OptionalRunOptions, matches: &ArgMatches) -> RunOptions { log::debug!("Room: {:?}", matches.value_source("room")); RunOptions { port: merge_values(primary.port, secondary.port, matches.value_source("port")), @@ -106,22 +108,20 @@ fn merge_configs( secondary.ip_version, matches.value_source("ip_version"), ), - dir: None, - config_file: None, } } #[tokio::main] async fn main() -> anyhow::Result<()> { - let opts: Cli = Cli::parse(); + let cli_opts: Cli = Cli::parse(); - match opts.command { - Commands::Run(run_opts) => run(run_opts).await, - Commands::Update { dry_run, force } => update::update(dry_run, force).await, + match cli_opts.command { + None => run(cli_opts).await, + Some(Commands::Update { dry_run, force }) => update::update(dry_run, force).await, } } -async fn run(unprocessed_opts: RunOptions) -> anyhow::Result<()> { +async fn run(unprocessed_opts: Cli) -> anyhow::Result<()> { let main_cancellation_token = CancellationToken::new(); // Configure insanity data directory @@ -171,11 +171,7 @@ async fn run(unprocessed_opts: RunOptions) -> anyhow::Result<()> { }; // Merge configs - let opts = merge_configs( - unprocessed_opts, - config_file, - Cli::command().get_matches().subcommand().unwrap().1, - ); + let opts = merge_configs(unprocessed_opts, config_file, &Cli::command().get_matches()); let display_name = format!( "{} [{}]",