2023-08-01 17:01:00 +02:00
|
|
|
use std::{
|
|
|
|
fmt::Display,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2023-08-01 02:31:44 +02:00
|
|
|
|
|
|
|
pub mod gatheres;
|
|
|
|
mod stage0_config;
|
|
|
|
|
|
|
|
pub use gatheres::ConfigArgs;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PleaseProjectConfig {
|
|
|
|
pub owner: String,
|
|
|
|
pub repository: String,
|
|
|
|
pub source: PathBuf,
|
|
|
|
pub branch: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PleaseSettingsConfig {
|
|
|
|
pub api_url: String,
|
2023-08-01 22:54:26 +02:00
|
|
|
pub git_username: Option<String>,
|
|
|
|
pub git_email: Option<String>,
|
2023-08-01 02:31:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PleaseConfig {
|
|
|
|
pub project: PleaseProjectConfig,
|
|
|
|
pub settings: PleaseSettingsConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PleaseConfig {
|
2023-08-01 15:02:05 +02:00
|
|
|
pub fn get_owner(&self) -> &str {
|
2023-08-01 02:31:44 +02:00
|
|
|
&self.project.owner
|
|
|
|
}
|
2023-08-01 15:02:05 +02:00
|
|
|
pub fn get_repository(&self) -> &str {
|
2023-08-01 02:31:44 +02:00
|
|
|
&self.project.repository
|
|
|
|
}
|
2023-08-01 15:02:05 +02:00
|
|
|
pub fn get_source(&self) -> &PathBuf {
|
2023-08-01 02:31:44 +02:00
|
|
|
&self.project.source
|
|
|
|
}
|
2023-08-01 15:02:05 +02:00
|
|
|
pub fn get_branch(&self) -> &str {
|
2023-08-01 02:31:44 +02:00
|
|
|
&self.project.branch
|
|
|
|
}
|
2023-08-01 15:02:05 +02:00
|
|
|
pub fn get_api_url(&self) -> &str {
|
2023-08-01 02:31:44 +02:00
|
|
|
&self.settings.api_url
|
|
|
|
}
|
2023-08-01 22:54:26 +02:00
|
|
|
pub fn get_git_username(&self) -> Option<String> {
|
|
|
|
self.settings.git_username.clone()
|
|
|
|
}
|
|
|
|
pub fn get_git_email(&self) -> Option<String> {
|
|
|
|
self.settings.git_email.clone()
|
|
|
|
}
|
2023-08-01 02:31:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 17:01:00 +02:00
|
|
|
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())?;
|
2023-08-01 22:54:26 +02:00
|
|
|
if let Some(git_username) = self.get_git_username() {
|
|
|
|
writeln!(f, " git_username: {}", git_username)?;
|
|
|
|
}
|
|
|
|
if let Some(git_email) = self.get_git_email() {
|
|
|
|
writeln!(f, " git_email: {}", git_email)?;
|
|
|
|
}
|
2023-08-01 17:01:00 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 02:31:44 +02:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct PleaseConfigBuilder {
|
|
|
|
stdin: Option<stage0_config::PleaseConfigBuilder>,
|
|
|
|
execution_env: Option<stage0_config::PleaseConfigBuilder>,
|
|
|
|
cli: Option<stage0_config::PleaseConfigBuilder>,
|
|
|
|
config: Option<stage0_config::PleaseConfigBuilder>,
|
2023-08-01 16:38:30 +02:00
|
|
|
source: Option<stage0_config::PleaseConfigBuilder>,
|
2023-08-01 02:31:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PleaseConfigBuilder {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_stdin(&mut self, stdin: String) -> &mut Self {
|
|
|
|
self.stdin = Some(gatheres::get_config_from_stdin::<
|
|
|
|
stage0_config::PleaseConfigBuilder,
|
|
|
|
>(stdin.as_str()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_config_file(&mut self, current_dir: &Path) -> &mut Self {
|
|
|
|
self.config = Some(gatheres::get_config_from_config_file(current_dir));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_execution_env(&mut self, env_bag: std::env::Vars) -> &mut Self {
|
|
|
|
self.execution_env = Some(gatheres::get_from_environment(env_bag));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_cli(&mut self, cli: gatheres::ConfigArgs) -> &mut Self {
|
|
|
|
self.cli = Some(cli.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-01 16:38:30 +02:00
|
|
|
pub fn with_source(&mut self, source: &Path) -> &mut Self {
|
|
|
|
self.source = Some(gatheres::get_source(source));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-01 02:31:44 +02:00
|
|
|
pub fn build(&mut self) -> anyhow::Result<PleaseConfig> {
|
2023-08-01 16:38:30 +02:00
|
|
|
let gathered = vec![
|
|
|
|
&self.execution_env,
|
|
|
|
&self.source,
|
|
|
|
&self.config,
|
|
|
|
&self.stdin,
|
|
|
|
&self.cli,
|
|
|
|
];
|
2023-08-01 02:31:44 +02:00
|
|
|
|
|
|
|
let final_config = gathered
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.fold(stage0_config::PleaseConfigBuilder::default(), |mut a, x| {
|
|
|
|
a.merge(x).clone()
|
|
|
|
});
|
|
|
|
|
2023-08-01 15:02:05 +02:00
|
|
|
final_config.try_into()
|
2023-08-01 02:31:44 +02:00
|
|
|
}
|
|
|
|
}
|