From aa74c13049ac6633472d2bab925b5dafc2334aac Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 29 Jul 2023 18:31:23 +0200 Subject: [PATCH] feat: split headings into local and global Signed-off-by: kjuulh --- crates/cuddle-please/src/command.rs | 74 +++++++++++++++++++---------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/crates/cuddle-please/src/command.rs b/crates/cuddle-please/src/command.rs index c862efe..8e04736 100644 --- a/crates/cuddle-please/src/command.rs +++ b/crates/cuddle-please/src/command.rs @@ -6,7 +6,7 @@ use std::{ }; use anyhow::Context; -use clap::{Parser, Subcommand}; +use clap::{Args, Parser, Subcommand}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::ui::{ConsoleUi, DynUi}; @@ -14,25 +14,8 @@ use crate::ui::{ConsoleUi, DynUi}; #[derive(Parser)] #[command(author, version, about, long_about = None)] pub struct Command { - /// token is the personal access token from gitea. - #[arg( - env = "CUDDLE_PLEASE_TOKEN", - long, - long_help = "token is the personal access token from gitea. It requires at least repository write access, it isn't required by default, but for most usecases the flow will fail without it", - global = true - )] - token: Option, - - /// Which repository to publish against. If not supplied remote url will be used. - #[arg(long, global = true)] - repo_url: Option, - - /// which source directory to use, if not set `std::env::current_dir` is used instead. - #[arg(long, global = true)] - source: Option, - - #[arg(long, global = true)] - config_stdin: bool, + #[command(flatten)] + global: GlobalArgs, #[command(subcommand)] commands: Option, @@ -44,6 +27,44 @@ pub struct Command { stdin: Option anyhow::Result + Send + Sync + 'static>>>, } +#[derive(Args)] +struct GlobalArgs { + /// token is the personal access token from gitea. + #[arg( + env = "CUDDLE_PLEASE_TOKEN", + long, + long_help = "token is the personal access token from gitea. It requires at least repository write access, it isn't required by default, but for most usecases the flow will fail without it", + global = true, + group = "global", + help_heading = "Global" + )] + token: Option, + + /// Which repository to publish against. If not supplied remote url will be used. + #[arg(long, global = true, group = "global", help_heading = "Global")] + repo_url: Option, + + /// which source directory to use, if not set `std::env::current_dir` is used instead. + #[arg(long, global = true, group = "global", help_heading = "Global")] + source: Option, + + /// Inject configuration from stdin + #[arg( + long, + global = true, + group = "global", + help_heading = "Global", + long_help = "inject via stdin +cat < Self { let args = std::env::args(); @@ -99,8 +120,8 @@ impl Command { pub fn execute(self, current_dir: Option<&Path>) -> anyhow::Result<()> { // 1. Parse the current directory - let current_dir = get_current_path(current_dir, self.source.clone())?; - let stdin = if self.config_stdin { + let current_dir = get_current_path(current_dir, self.global.source.clone())?; + let stdin = if self.global.config_stdin { if let Some(stdin_fn) = self.stdin.clone() { let output = (stdin_fn.lock().unwrap().deref())(); Some(output.unwrap()) @@ -113,7 +134,7 @@ impl Command { match &self.commands { Some(Commands::Config { command }) => match command { - ConfigCommand::List {} => { + ConfigCommand::List { .. } => { tracing::debug!("running command: config list"); let _config = self.get_config(current_dir.as_path(), stdin)?; @@ -149,6 +170,7 @@ impl Command { #[derive(Debug, Clone, Subcommand)] enum Commands { + /// Config is mostly used for debugging the final config output Config { #[command(subcommand)] command: ConfigCommand, @@ -156,7 +178,11 @@ enum Commands { } #[derive(Subcommand, Debug, Clone)] enum ConfigCommand { - List {}, + /// List will list the final configuration + List { + #[arg(long)] + someArg: String, + }, } fn get_current_path(