From ae9073bf0b87b051928da906344c720712c697de Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 1 Aug 2023 17:01:00 +0200 Subject: [PATCH] refactor: move config list Signed-off-by: kjuulh --- crates/cuddle-please-commands/src/command.rs | 27 +++++------- .../src/config_command.rs | 27 ++++++++++++ crates/cuddle-please-commands/src/lib.rs | 1 + .../src/release_command.rs | 14 ++---- crates/cuddle-please-frontend/src/lib.rs | 17 ++++++- .../src/local_git_client.rs | 44 +++++++++---------- crates/cuddle-please/tests/common/mod.rs | 4 +- crates/cuddle-please/tests/config.rs | 17 ++++--- 8 files changed, 93 insertions(+), 58 deletions(-) create mode 100644 crates/cuddle-please-commands/src/config_command.rs diff --git a/crates/cuddle-please-commands/src/command.rs b/crates/cuddle-please-commands/src/command.rs index 22303cd..90e2aa4 100644 --- a/crates/cuddle-please-commands/src/command.rs +++ b/crates/cuddle-please-commands/src/command.rs @@ -1,23 +1,18 @@ use std::{ - env::current_dir, io::Read, ops::Deref, path::{Path, PathBuf}, - rc::Rc, sync::{Arc, Mutex}, }; -use ::semver::Version; -use anyhow::Context; use clap::{Parser, Subcommand}; use cuddle_please_frontend::{gatheres::ConfigArgs, PleaseConfig, PleaseConfigBuilder}; use cuddle_please_misc::{ - changelog_parser, get_most_significant_version, ChangeLogBuilder, ConsoleUi, - DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs, LocalGitClient, NextVersion, - RemoteGitEngine, StdinFn, VcsClient, + get_most_significant_version, ConsoleUi, DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs, + LocalGitClient, NextVersion, RemoteGitEngine, StdinFn, VcsClient, }; -use crate::release_command::ReleaseCommand; +use crate::{config_command::ConfigCommandHandler, release_command::ReleaseCommand}; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -94,17 +89,15 @@ impl Command { match &self.commands { Some(Commands::Release {}) => { + tracing::debug!("running command: release"); + ReleaseCommand::new(config, git_client, gitea_client) .execute(self.global.dry_run)?; } - Some(Commands::Config { command }) => match command { - ConfigCommand::List { .. } => { - tracing::debug!("running command: config list"); - - self.ui.write_str_ln("cuddle-config"); - } - }, + Some(Commands::Config { command }) => { + ConfigCommandHandler::new(self.ui, config).execute(command)?; + } Some(Commands::Gitea { command }) => { let git_url = url::Url::parse(config.get_api_url())?; @@ -116,7 +109,7 @@ impl Command { url.push_str(format!(":{port}").as_str()); } - let client = GiteaClient::new(&url, self.global.token.as_ref().map(|t| t.as_str())); + let client = GiteaClient::new(&url, self.global.token.as_deref()); match command { GiteaCommand::Connect {} => { client.connect(config.get_owner(), config.get_repository())?; @@ -227,7 +220,7 @@ impl Command { cuddle_please_misc::RemoteEngine::Local => Box::new(LocalGitClient::new()), cuddle_please_misc::RemoteEngine::Gitea => Box::new(GiteaClient::new( &self.config.api_url.clone().expect("api_url to be set"), - self.global.token.as_ref().map(|t| t.as_str()), + self.global.token.as_deref(), )), } } diff --git a/crates/cuddle-please-commands/src/config_command.rs b/crates/cuddle-please-commands/src/config_command.rs new file mode 100644 index 0000000..75f8eb3 --- /dev/null +++ b/crates/cuddle-please-commands/src/config_command.rs @@ -0,0 +1,27 @@ +use cuddle_please_frontend::PleaseConfig; +use cuddle_please_misc::DynUi; + +use crate::command::ConfigCommand; + +pub struct ConfigCommandHandler { + ui: DynUi, + config: PleaseConfig, +} + +impl ConfigCommandHandler { + pub fn new(ui: DynUi, config: PleaseConfig) -> Self { + Self { ui, config } + } + + pub fn execute(&self, command: &ConfigCommand) -> anyhow::Result<()> { + match command { + ConfigCommand::List {} => { + tracing::debug!("running command: config list"); + + self.ui.write_str_ln("cuddle-config"); + self.ui.write_str(&format!("{}", self.config)); + } + } + Ok(()) + } +} diff --git a/crates/cuddle-please-commands/src/lib.rs b/crates/cuddle-please-commands/src/lib.rs index c7455ce..eb2ad1b 100644 --- a/crates/cuddle-please-commands/src/lib.rs +++ b/crates/cuddle-please-commands/src/lib.rs @@ -1,4 +1,5 @@ mod command; +mod config_command; mod release_command; pub use command::Command as PleaseCommand; diff --git a/crates/cuddle-please-commands/src/release_command.rs b/crates/cuddle-please-commands/src/release_command.rs index 8f7725f..2ad1cdd 100644 --- a/crates/cuddle-please-commands/src/release_command.rs +++ b/crates/cuddle-please-commands/src/release_command.rs @@ -1,19 +1,11 @@ use cuddle_please_frontend::PleaseConfig; -use std::{ - io::Read, - ops::Deref, - path::{Path, PathBuf}, - sync::{Arc, Mutex}, -}; - use ::semver::Version; use anyhow::Context; -use clap::{Parser, Subcommand}; -use cuddle_please_frontend::{gatheres::ConfigArgs, PleaseConfigBuilder}; + use cuddle_please_misc::{ - changelog_parser, get_most_significant_version, ChangeLogBuilder, ConsoleUi, - DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs, NextVersion, StdinFn, VcsClient, + changelog_parser, get_most_significant_version, ChangeLogBuilder, DynRemoteGitClient, + NextVersion, VcsClient, }; pub struct ReleaseCommand { diff --git a/crates/cuddle-please-frontend/src/lib.rs b/crates/cuddle-please-frontend/src/lib.rs index 15a4854..d66b5c6 100644 --- a/crates/cuddle-please-frontend/src/lib.rs +++ b/crates/cuddle-please-frontend/src/lib.rs @@ -1,4 +1,7 @@ -use std::path::{Path, PathBuf}; +use std::{ + fmt::Display, + path::{Path, PathBuf}, +}; pub mod gatheres; mod stage0_config; @@ -42,6 +45,18 @@ impl PleaseConfig { } } +impl Display for PleaseConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "PleaseConfig")?; + writeln!(f, " owner: {}", self.get_owner())?; + writeln!(f, " repository: {}", self.get_repository())?; + writeln!(f, " branch: {}", self.get_branch())?; + writeln!(f, " api_url: {}", self.get_api_url())?; + + Ok(()) + } +} + #[derive(Clone, Debug, Default)] pub struct PleaseConfigBuilder { stdin: Option, diff --git a/crates/cuddle-please-misc/src/local_git_client.rs b/crates/cuddle-please-misc/src/local_git_client.rs index 4509f30..c1431c1 100644 --- a/crates/cuddle-please-misc/src/local_git_client.rs +++ b/crates/cuddle-please-misc/src/local_git_client.rs @@ -9,57 +9,57 @@ impl LocalGitClient { } impl RemoteGitEngine for LocalGitClient { - fn connect(&self, owner: &str, repo: &str) -> anyhow::Result<()> { + fn connect(&self, _owner: &str, _repo: &str) -> anyhow::Result<()> { todo!() } - fn get_tags(&self, owner: &str, repo: &str) -> anyhow::Result> { + fn get_tags(&self, _owner: &str, _repo: &str) -> anyhow::Result> { todo!() } fn get_commits_since( &self, - owner: &str, - repo: &str, - since_sha: Option<&str>, - branch: &str, + _owner: &str, + _repo: &str, + _since_sha: Option<&str>, + _branch: &str, ) -> anyhow::Result> { todo!() } - fn get_pull_request(&self, owner: &str, repo: &str) -> anyhow::Result> { + fn get_pull_request(&self, _owner: &str, _repo: &str) -> anyhow::Result> { todo!() } fn create_pull_request( &self, - owner: &str, - repo: &str, - version: &str, - body: &str, - base: &str, + _owner: &str, + _repo: &str, + _version: &str, + _body: &str, + _base: &str, ) -> anyhow::Result { todo!() } fn update_pull_request( &self, - owner: &str, - repo: &str, - version: &str, - body: &str, - index: usize, + _owner: &str, + _repo: &str, + _version: &str, + _body: &str, + _index: usize, ) -> anyhow::Result { todo!() } fn create_release( &self, - owner: &str, - repo: &str, - version: &str, - body: &str, - prerelease: bool, + _owner: &str, + _repo: &str, + _version: &str, + _body: &str, + _prerelease: bool, ) -> anyhow::Result { todo!() } diff --git a/crates/cuddle-please/tests/common/mod.rs b/crates/cuddle-please/tests/common/mod.rs index 68db3ac..ce6cd89 100644 --- a/crates/cuddle-please/tests/common/mod.rs +++ b/crates/cuddle-please/tests/common/mod.rs @@ -108,8 +108,8 @@ impl From<&BufferUi> for DynUi { pub fn assert_output(ui: &BufferUi, expected_stdout: &str, expected_stderr: &str) { let (stdout, stderr) = ui.get_output(); - assert_eq!(expected_stdout, &stdout); - assert_eq!(expected_stderr, &stderr); + pretty_assertions::assert_eq!(expected_stdout, &stdout); + pretty_assertions::assert_eq!(expected_stderr, &stderr); } pub fn get_test_data_path(item: &str) -> PathBuf { diff --git a/crates/cuddle-please/tests/config.rs b/crates/cuddle-please/tests/config.rs index 288cb8a..0534c0a 100644 --- a/crates/cuddle-please/tests/config.rs +++ b/crates/cuddle-please/tests/config.rs @@ -16,6 +16,14 @@ fn get_base_args<'a>() -> Vec<&'a str> { ] } +const EXPECTED_OUTPUT: &'static str = r#"cuddle-config +PleaseConfig + owner: kjuulh + repository: cuddle-please + branch: main + api_url: https://some-example.gitea-instance +"#; + #[test] #[traced_test] fn test_config_from_current_dir() { @@ -27,7 +35,7 @@ fn test_config_from_current_dir() { .execute(Some(¤t_dir)) .unwrap(); - assert_output(ui, "cuddle-config\n", ""); + assert_output(ui, EXPECTED_OUTPUT, ""); } #[test] @@ -43,7 +51,7 @@ fn test_config_from_source_dir() { .execute(None) .unwrap(); - assert_output(ui, "cuddle-config\n", ""); + assert_output(ui, EXPECTED_OUTPUT, ""); } #[test] @@ -57,14 +65,13 @@ project: repository: cuddle-please branch: main settings: - api_url: https://some-example.gitea-instance -"#; + api_url: https://some-example.gitea-instance"#; args.push("--config-stdin"); PleaseCommand::new_from_args_with_stdin(Some(ui), args, || Ok(config.into())) .execute(None) .unwrap(); - assert_output(ui, "cuddle-config\n", ""); + assert_output(ui, EXPECTED_OUTPUT, ""); } #[test]