refactor: move config list

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-01 17:01:00 +02:00
parent e51454088e
commit ae9073bf0b
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
8 changed files with 93 additions and 58 deletions

View File

@ -1,23 +1,18 @@
use std::{ use std::{
env::current_dir,
io::Read, io::Read,
ops::Deref, ops::Deref,
path::{Path, PathBuf}, path::{Path, PathBuf},
rc::Rc,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use ::semver::Version;
use anyhow::Context;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use cuddle_please_frontend::{gatheres::ConfigArgs, PleaseConfig, PleaseConfigBuilder}; use cuddle_please_frontend::{gatheres::ConfigArgs, PleaseConfig, PleaseConfigBuilder};
use cuddle_please_misc::{ use cuddle_please_misc::{
changelog_parser, get_most_significant_version, ChangeLogBuilder, ConsoleUi, get_most_significant_version, ConsoleUi, DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs,
DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs, LocalGitClient, NextVersion, LocalGitClient, NextVersion, RemoteGitEngine, StdinFn, VcsClient,
RemoteGitEngine, StdinFn, VcsClient,
}; };
use crate::release_command::ReleaseCommand; use crate::{config_command::ConfigCommandHandler, release_command::ReleaseCommand};
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@ -94,17 +89,15 @@ impl Command {
match &self.commands { match &self.commands {
Some(Commands::Release {}) => { Some(Commands::Release {}) => {
tracing::debug!("running command: release");
ReleaseCommand::new(config, git_client, gitea_client) ReleaseCommand::new(config, git_client, gitea_client)
.execute(self.global.dry_run)?; .execute(self.global.dry_run)?;
} }
Some(Commands::Config { command }) => match command { Some(Commands::Config { command }) => {
ConfigCommand::List { .. } => { ConfigCommandHandler::new(self.ui, config).execute(command)?;
tracing::debug!("running command: config list"); }
self.ui.write_str_ln("cuddle-config");
}
},
Some(Commands::Gitea { command }) => { Some(Commands::Gitea { command }) => {
let git_url = url::Url::parse(config.get_api_url())?; let git_url = url::Url::parse(config.get_api_url())?;
@ -116,7 +109,7 @@ impl Command {
url.push_str(format!(":{port}").as_str()); 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 { match command {
GiteaCommand::Connect {} => { GiteaCommand::Connect {} => {
client.connect(config.get_owner(), config.get_repository())?; 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::Local => Box::new(LocalGitClient::new()),
cuddle_please_misc::RemoteEngine::Gitea => Box::new(GiteaClient::new( cuddle_please_misc::RemoteEngine::Gitea => Box::new(GiteaClient::new(
&self.config.api_url.clone().expect("api_url to be set"), &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(),
)), )),
} }
} }

View File

@ -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(())
}
}

View File

@ -1,4 +1,5 @@
mod command; mod command;
mod config_command;
mod release_command; mod release_command;
pub use command::Command as PleaseCommand; pub use command::Command as PleaseCommand;

View File

@ -1,19 +1,11 @@
use cuddle_please_frontend::PleaseConfig; use cuddle_please_frontend::PleaseConfig;
use std::{
io::Read,
ops::Deref,
path::{Path, PathBuf},
sync::{Arc, Mutex},
};
use ::semver::Version; use ::semver::Version;
use anyhow::Context; use anyhow::Context;
use clap::{Parser, Subcommand};
use cuddle_please_frontend::{gatheres::ConfigArgs, PleaseConfigBuilder};
use cuddle_please_misc::{ use cuddle_please_misc::{
changelog_parser, get_most_significant_version, ChangeLogBuilder, ConsoleUi, changelog_parser, get_most_significant_version, ChangeLogBuilder, DynRemoteGitClient,
DynRemoteGitClient, DynUi, GiteaClient, GlobalArgs, NextVersion, StdinFn, VcsClient, NextVersion, VcsClient,
}; };
pub struct ReleaseCommand { pub struct ReleaseCommand {

View File

@ -1,4 +1,7 @@
use std::path::{Path, PathBuf}; use std::{
fmt::Display,
path::{Path, PathBuf},
};
pub mod gatheres; pub mod gatheres;
mod stage0_config; 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)] #[derive(Clone, Debug, Default)]
pub struct PleaseConfigBuilder { pub struct PleaseConfigBuilder {
stdin: Option<stage0_config::PleaseConfigBuilder>, stdin: Option<stage0_config::PleaseConfigBuilder>,

View File

@ -9,57 +9,57 @@ impl LocalGitClient {
} }
impl RemoteGitEngine for LocalGitClient { impl RemoteGitEngine for LocalGitClient {
fn connect(&self, owner: &str, repo: &str) -> anyhow::Result<()> { fn connect(&self, _owner: &str, _repo: &str) -> anyhow::Result<()> {
todo!() todo!()
} }
fn get_tags(&self, owner: &str, repo: &str) -> anyhow::Result<Vec<crate::gitea_client::Tag>> { fn get_tags(&self, _owner: &str, _repo: &str) -> anyhow::Result<Vec<crate::gitea_client::Tag>> {
todo!() todo!()
} }
fn get_commits_since( fn get_commits_since(
&self, &self,
owner: &str, _owner: &str,
repo: &str, _repo: &str,
since_sha: Option<&str>, _since_sha: Option<&str>,
branch: &str, _branch: &str,
) -> anyhow::Result<Vec<crate::gitea_client::Commit>> { ) -> anyhow::Result<Vec<crate::gitea_client::Commit>> {
todo!() todo!()
} }
fn get_pull_request(&self, owner: &str, repo: &str) -> anyhow::Result<Option<usize>> { fn get_pull_request(&self, _owner: &str, _repo: &str) -> anyhow::Result<Option<usize>> {
todo!() todo!()
} }
fn create_pull_request( fn create_pull_request(
&self, &self,
owner: &str, _owner: &str,
repo: &str, _repo: &str,
version: &str, _version: &str,
body: &str, _body: &str,
base: &str, _base: &str,
) -> anyhow::Result<usize> { ) -> anyhow::Result<usize> {
todo!() todo!()
} }
fn update_pull_request( fn update_pull_request(
&self, &self,
owner: &str, _owner: &str,
repo: &str, _repo: &str,
version: &str, _version: &str,
body: &str, _body: &str,
index: usize, _index: usize,
) -> anyhow::Result<usize> { ) -> anyhow::Result<usize> {
todo!() todo!()
} }
fn create_release( fn create_release(
&self, &self,
owner: &str, _owner: &str,
repo: &str, _repo: &str,
version: &str, _version: &str,
body: &str, _body: &str,
prerelease: bool, _prerelease: bool,
) -> anyhow::Result<crate::gitea_client::Release> { ) -> anyhow::Result<crate::gitea_client::Release> {
todo!() todo!()
} }

View File

@ -108,8 +108,8 @@ impl From<&BufferUi> for DynUi {
pub fn assert_output(ui: &BufferUi, expected_stdout: &str, expected_stderr: &str) { pub fn assert_output(ui: &BufferUi, expected_stdout: &str, expected_stderr: &str) {
let (stdout, stderr) = ui.get_output(); let (stdout, stderr) = ui.get_output();
assert_eq!(expected_stdout, &stdout); pretty_assertions::assert_eq!(expected_stdout, &stdout);
assert_eq!(expected_stderr, &stderr); pretty_assertions::assert_eq!(expected_stderr, &stderr);
} }
pub fn get_test_data_path(item: &str) -> PathBuf { pub fn get_test_data_path(item: &str) -> PathBuf {

View File

@ -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] #[test]
#[traced_test] #[traced_test]
fn test_config_from_current_dir() { fn test_config_from_current_dir() {
@ -27,7 +35,7 @@ fn test_config_from_current_dir() {
.execute(Some(&current_dir)) .execute(Some(&current_dir))
.unwrap(); .unwrap();
assert_output(ui, "cuddle-config\n", ""); assert_output(ui, EXPECTED_OUTPUT, "");
} }
#[test] #[test]
@ -43,7 +51,7 @@ fn test_config_from_source_dir() {
.execute(None) .execute(None)
.unwrap(); .unwrap();
assert_output(ui, "cuddle-config\n", ""); assert_output(ui, EXPECTED_OUTPUT, "");
} }
#[test] #[test]
@ -57,14 +65,13 @@ project:
repository: cuddle-please repository: cuddle-please
branch: main branch: main
settings: settings:
api_url: https://some-example.gitea-instance api_url: https://some-example.gitea-instance"#;
"#;
args.push("--config-stdin"); args.push("--config-stdin");
PleaseCommand::new_from_args_with_stdin(Some(ui), args, || Ok(config.into())) PleaseCommand::new_from_args_with_stdin(Some(ui), args, || Ok(config.into()))
.execute(None) .execute(None)
.unwrap(); .unwrap();
assert_output(ui, "cuddle-config\n", ""); assert_output(ui, EXPECTED_OUTPUT, "");
} }
#[test] #[test]