cuddle-please/crates/cuddle-please-commands/src/command.rs

212 lines
6.4 KiB
Rust
Raw Normal View History

use std::{
io::Read,
ops::Deref,
path::{Path, PathBuf},
sync::{Arc, Mutex},
};
use clap::{Parser, Subcommand};
use cuddle_please_frontend::{gatheres::ConfigArgs, PleaseConfig, PleaseConfigBuilder};
use cuddle_please_misc::{
ConsoleUi, DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs, LocalGitClient, StdinFn,
VcsClient,
};
use tracing::Level;
use tracing_subscriber::{EnvFilter};
use crate::{
config_command::{ConfigCommand, ConfigCommandHandler},
doctor_command::DoctorCommandHandler,
gitea_command::{GiteaCommand, GiteaCommandHandler},
release_command::ReleaseCommandHandler,
};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Command {
#[command(flatten)]
global: GlobalArgs,
#[command(flatten)]
config: ConfigArgs,
#[command(subcommand)]
commands: Option<Commands>,
#[clap(skip)]
ui: DynUi,
#[clap(skip)]
stdin: StdinFn,
}
impl Default for Command {
fn default() -> Self {
Self::new()
}
}
impl Command {
pub fn new() -> Self {
let args = std::env::args();
Self::new_from_args_with_stdin(Some(ConsoleUi::default()), args, || {
let mut input = String::new();
std::io::stdin().read_to_string(&mut input)?;
Ok(input)
})
}
pub fn new_from_args<I, T, UIF>(ui: Option<UIF>, i: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
UIF: Into<DynUi>,
{
let mut s = Self::parse_from(i);
if let Some(ui) = ui {
s.ui = ui.into();
}
s
}
pub fn new_from_args_with_stdin<I, T, F, UIF>(ui: Option<UIF>, i: I, input: F) -> Self
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
F: Fn() -> anyhow::Result<String> + Send + Sync + 'static,
UIF: Into<DynUi>,
{
let mut s = Self::parse_from(i);
if let Some(ui) = ui {
s.ui = ui.into();
}
s.stdin = Some(Arc::new(Mutex::new(input)));
s
}
pub fn execute(self, current_dir: Option<&Path>) -> anyhow::Result<()> {
let config = self.build_config(current_dir)?;
let git_client = self.get_git(&config)?;
chore(release): 0.0.1 (#4) # Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ## [0.2.0] - 2023-08-01 ### Added - add docker setup - refactor frontend configuration - with all the way through - with create pull request and release - with gitea - with prepend as well - add cliff - remove tokio - with doctor - with git client - with fixes - with conventional parse - with tags command - add semver - can get commit chain - with start of environment engine - with gitea client - fmt - add gitea client stub - add tests for git setup - split headings into local and global - rename to cuddle_please - add config parsing - with basic get dir - add mkdocs - add base ### Other - release command - add cuddle.release to this repository - add granular docker setup - fix checks - chck refactor commands - move doctor command - fmt - rename release command - move gitea command into its own file - move config list - move gitea out of the way - move config building out of main execution loop - move commands and misc out of main binary package - fmt - check hide commands - move cuddle-please to cuddle-please release - remove no-vcs option (moved to a later stage if github is someday adopted - fix clippy warnings - clippy fix - fix - cleanup Reviewed-on: https://git.front.kjuulh.io/kjuulh/cuddle-please/pulls/4 Co-authored-by: kjuulh <contact@kjuulh.io> Co-committed-by: kjuulh <contact@kjuulh.io>
2023-08-01 17:53:50 +02:00
let gitea_client = self.get_gitea_client(&config);
let filter = match self.global.log_level {
cuddle_please_misc::LogLevel::None => None,
cuddle_please_misc::LogLevel::Trace => Some(Level::TRACE),
cuddle_please_misc::LogLevel::Debug => Some(Level::DEBUG),
cuddle_please_misc::LogLevel::Info => Some(Level::INFO),
cuddle_please_misc::LogLevel::Error => Some(Level::ERROR),
};
if let Some(filter) = filter {
let env_filter = EnvFilter::builder().with_regex(false).parse(format!(
"{},hyper=error,reqwest=error,git_cliff_core=error",
filter
))?;
tracing_subscriber::fmt().with_env_filter(env_filter).init();
}
match &self.commands {
Some(Commands::Release {}) => {
ReleaseCommandHandler::new(self.ui, config, git_client, gitea_client)
.execute(self.global.dry_run)?;
}
Some(Commands::Config { command }) => {
ConfigCommandHandler::new(self.ui, config).execute(command)?;
}
Some(Commands::Gitea { command }) => {
GiteaCommandHandler::new(self.ui, config, gitea_client)
.execute(command, self.global.token.expect("token to be set").deref())?;
}
Some(Commands::Doctor {}) => {
DoctorCommandHandler::new(self.ui).execute()?;
}
None => {}
}
Ok(())
}
fn build_config(&self, current_dir: Option<&Path>) -> Result<PleaseConfig, anyhow::Error> {
let mut builder = &mut PleaseConfigBuilder::new();
if self.global.config_stdin {
if let Some(stdin_fn) = self.stdin.clone() {
let output = (stdin_fn.lock().unwrap().deref())();
builder = builder.with_stdin(output?);
}
}
let current_dir = get_current_path(current_dir, self.config.source.clone())?;
let config = builder
.with_config_file(&current_dir)
.with_source(&current_dir)
.with_execution_env(std::env::vars())
.with_cli(self.config.clone())
.build()?;
Ok(config)
}
fn get_git(&self, config: &PleaseConfig) -> anyhow::Result<VcsClient> {
if self.global.no_vcs {
Ok(VcsClient::new_noop())
} else {
VcsClient::new_git(
config.get_source(),
config.settings.git_username.clone(),
config.settings.git_email.clone(),
)
}
}
chore(release): 0.0.1 (#4) # Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ## [0.2.0] - 2023-08-01 ### Added - add docker setup - refactor frontend configuration - with all the way through - with create pull request and release - with gitea - with prepend as well - add cliff - remove tokio - with doctor - with git client - with fixes - with conventional parse - with tags command - add semver - can get commit chain - with start of environment engine - with gitea client - fmt - add gitea client stub - add tests for git setup - split headings into local and global - rename to cuddle_please - add config parsing - with basic get dir - add mkdocs - add base ### Other - release command - add cuddle.release to this repository - add granular docker setup - fix checks - chck refactor commands - move doctor command - fmt - rename release command - move gitea command into its own file - move config list - move gitea out of the way - move config building out of main execution loop - move commands and misc out of main binary package - fmt - check hide commands - move cuddle-please to cuddle-please release - remove no-vcs option (moved to a later stage if github is someday adopted - fix clippy warnings - clippy fix - fix - cleanup Reviewed-on: https://git.front.kjuulh.io/kjuulh/cuddle-please/pulls/4 Co-authored-by: kjuulh <contact@kjuulh.io> Co-committed-by: kjuulh <contact@kjuulh.io>
2023-08-01 17:53:50 +02:00
fn get_gitea_client(&self, config: &PleaseConfig) -> DynRemoteGitClient {
match self.global.engine {
cuddle_please_misc::RemoteEngine::Local => Box::new(LocalGitClient::new()),
cuddle_please_misc::RemoteEngine::Gitea => Box::new(GiteaClient::new(
chore(release): 0.0.1 (#4) # Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ## [0.2.0] - 2023-08-01 ### Added - add docker setup - refactor frontend configuration - with all the way through - with create pull request and release - with gitea - with prepend as well - add cliff - remove tokio - with doctor - with git client - with fixes - with conventional parse - with tags command - add semver - can get commit chain - with start of environment engine - with gitea client - fmt - add gitea client stub - add tests for git setup - split headings into local and global - rename to cuddle_please - add config parsing - with basic get dir - add mkdocs - add base ### Other - release command - add cuddle.release to this repository - add granular docker setup - fix checks - chck refactor commands - move doctor command - fmt - rename release command - move gitea command into its own file - move config list - move gitea out of the way - move config building out of main execution loop - move commands and misc out of main binary package - fmt - check hide commands - move cuddle-please to cuddle-please release - remove no-vcs option (moved to a later stage if github is someday adopted - fix clippy warnings - clippy fix - fix - cleanup Reviewed-on: https://git.front.kjuulh.io/kjuulh/cuddle-please/pulls/4 Co-authored-by: kjuulh <contact@kjuulh.io> Co-committed-by: kjuulh <contact@kjuulh.io>
2023-08-01 17:53:50 +02:00
config.get_api_url(),
self.global.token.as_deref(),
)),
}
}
}
#[derive(Debug, Clone, Subcommand)]
pub enum Commands {
/// Config is mostly used for debugging the final config output
Release {},
#[command(hide = true)]
Config {
#[command(subcommand)]
command: ConfigCommand,
},
#[command(hide = true)]
Gitea {
#[command(subcommand)]
command: GiteaCommand,
},
/// Helps you identify missing things from your execution environment for cuddle-please to function as intended
Doctor {},
}
fn get_current_path(
optional_current_dir: Option<&Path>,
optional_source_path: Option<PathBuf>,
) -> anyhow::Result<PathBuf> {
let path = optional_source_path
.or_else(|| optional_current_dir.map(|p| p.to_path_buf())) // fall back on current env from environment
.filter(|v| v.to_string_lossy() != "") // make sure we don't get empty values
//.and_then(|p| p.canonicalize().ok()) // Make sure we get the absolute path
//.context("could not find current dir, pass --source as a replacement")?;
.unwrap_or(PathBuf::from("."));
if !path.exists() {
anyhow::bail!("path doesn't exist {}", path.display());
}
Ok(path)
}