feat: with start of environment engine

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-07-30 02:09:40 +02:00
parent 5876ed847c
commit 1b9f8b26e6
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
5 changed files with 75 additions and 3 deletions

View File

@ -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<String>,
) -> anyhow::Result<PleaseConfig> {
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<String>,
pub repository: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PleaseSettingsConfig {
pub api_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PleaseConfig {
pub project: Option<PleaseProjectConfig>,
pub settings: Option<PleaseSettingsConfig>,
}
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,
}
}
}

View File

@ -0,0 +1 @@

View File

@ -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,
}

View File

@ -1,4 +1,5 @@
pub mod command;
pub mod environment;
pub mod git_client;
pub mod gitea_client;
pub mod ui;

View File

@ -1,4 +1,5 @@
pub mod command;
pub mod environment;
pub mod git_client;
pub mod gitea_client;
pub mod ui;