diff --git a/crates/cuddle-please/src/command.rs b/crates/cuddle-please/src/command.rs index eb6444c..fdb7e8b 100644 --- a/crates/cuddle-please/src/command.rs +++ b/crates/cuddle-please/src/command.rs @@ -10,6 +10,7 @@ use clap::{Args, Parser, Subcommand}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::{ + environment::get_from_environment, git_client::VcsClient, gitea_client::GiteaClient, ui::{ConsoleUi, DynUi}, @@ -132,7 +133,9 @@ impl Command { current_dir: &Path, stdin: Option, ) -> anyhow::Result { - let config = get_config(current_dir, stdin)?; + let mut config = get_config(current_dir, stdin)?; + + self.get_from_environment(&mut config)?; Ok(config) } @@ -212,6 +215,13 @@ impl Command { VcsClient::new_git(current_dir) } } + + fn get_from_environment(&self, config: &mut PleaseConfig) -> anyhow::Result<()> { + let input_config = get_from_environment(); + config.merge_mut(input_config); + + Ok(()) + } } #[derive(Debug, Clone, Subcommand)] @@ -257,17 +267,38 @@ fn get_current_path( } #[derive(Debug, Clone, Serialize, Deserialize)] -struct PleaseConfig {} +pub struct PleaseProjectConfig { + pub owner: Option, + pub repository: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PleaseSettingsConfig { + pub api_url: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PleaseConfig { + pub project: Option, + pub settings: Option, +} impl PleaseConfig { fn merge(self, _config: PleaseConfig) -> Self { self } + + fn merge_mut(&mut self, _config: PleaseConfig) -> &mut Self { + self + } } impl Default for PleaseConfig { fn default() -> Self { - Self {} + Self { + project: None, + settings: None, + } } } diff --git a/crates/cuddle-please/src/environment/drone.rs b/crates/cuddle-please/src/environment/drone.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/cuddle-please/src/environment/drone.rs @@ -0,0 +1 @@ + diff --git a/crates/cuddle-please/src/environment/mod.rs b/crates/cuddle-please/src/environment/mod.rs new file mode 100644 index 0000000..8842cfb --- /dev/null +++ b/crates/cuddle-please/src/environment/mod.rs @@ -0,0 +1,38 @@ +use crate::command::{PleaseConfig, PleaseProjectConfig, PleaseSettingsConfig}; + +pub mod drone; + +pub fn get_from_environment() -> PleaseConfig { + let env = detect_environment(); + + match env { + ExecutionEnvironment::Local => PleaseConfig { + project: None, + settings: None, + }, + ExecutionEnvironment::Drone => PleaseConfig { + project: Some(PleaseProjectConfig { + owner: Some( + std::env::var("DRONE_REPO_OWNER").expect("DRONE_REPO_OWNER to be present"), + ), + repository: Some( + std::env::var("DRONE_REPO_NAME").expect("DRONE_REPO_NAME to be present"), + ), + }), + settings: None, + }, + } +} + +pub fn detect_environment() -> ExecutionEnvironment { + if let Some(_) = std::env::var("DRONE").ok() { + return ExecutionEnvironment::Drone; + } + + return ExecutionEnvironment::Local; +} + +pub enum ExecutionEnvironment { + Local, + Drone, +} diff --git a/crates/cuddle-please/src/lib.rs b/crates/cuddle-please/src/lib.rs index bcdc96f..b67b1f0 100644 --- a/crates/cuddle-please/src/lib.rs +++ b/crates/cuddle-please/src/lib.rs @@ -1,4 +1,5 @@ pub mod command; +pub mod environment; pub mod git_client; pub mod gitea_client; pub mod ui; diff --git a/crates/cuddle-please/src/main.rs b/crates/cuddle-please/src/main.rs index 980534e..2ad3884 100644 --- a/crates/cuddle-please/src/main.rs +++ b/crates/cuddle-please/src/main.rs @@ -1,4 +1,5 @@ pub mod command; +pub mod environment; pub mod git_client; pub mod gitea_client; pub mod ui;