feat: split headings into local and global

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-07-29 18:31:23 +02:00
parent ccb026365a
commit aa74c13049
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394

View File

@ -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<String>,
/// Which repository to publish against. If not supplied remote url will be used.
#[arg(long, global = true)]
repo_url: Option<String>,
/// which source directory to use, if not set `std::env::current_dir` is used instead.
#[arg(long, global = true)]
source: Option<PathBuf>,
#[arg(long, global = true)]
config_stdin: bool,
#[command(flatten)]
global: GlobalArgs,
#[command(subcommand)]
commands: Option<Commands>,
@ -44,6 +27,44 @@ pub struct Command {
stdin: Option<Arc<Mutex<dyn Fn() -> anyhow::Result<String> + 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<String>,
/// 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<String>,
/// 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<PathBuf>,
/// Inject configuration from stdin
#[arg(
long,
global = true,
group = "global",
help_heading = "Global",
long_help = "inject via stdin
cat <<EOF | cuddle-please --config-stdin
something
something
something
EOF
config-stdin will consume stdin until the channel is closed via. EOF"
)]
config_stdin: bool,
}
impl Command {
pub fn new() -> 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(