feat: refactor frontend configuration
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
95
crates/cuddle-please-frontend/src/lib.rs
Normal file
95
crates/cuddle-please-frontend/src/lib.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PleaseConfig {
|
||||
pub project: PleaseProjectConfig,
|
||||
pub settings: PleaseSettingsConfig,
|
||||
}
|
||||
|
||||
impl PleaseConfig {
|
||||
pub fn get_owner<'a>(&'a self) -> &'a str {
|
||||
&self.project.owner
|
||||
}
|
||||
pub fn get_repository<'a>(&'a self) -> &'a str {
|
||||
&self.project.repository
|
||||
}
|
||||
pub fn get_source<'a>(&'a self) -> &'a PathBuf {
|
||||
&self.project.source
|
||||
}
|
||||
pub fn get_branch<'a>(&'a self) -> &'a str {
|
||||
&self.project.branch
|
||||
}
|
||||
pub fn get_api_url<'a>(&'a self) -> &'a str {
|
||||
&self.settings.api_url
|
||||
}
|
||||
}
|
||||
|
||||
#[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>,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
pub fn build(&mut self) -> anyhow::Result<PleaseConfig> {
|
||||
let gathered = vec![&self.execution_env, &self.config, &self.stdin, &self.cli];
|
||||
|
||||
let final_config = gathered
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.fold(stage0_config::PleaseConfigBuilder::default(), |mut a, x| {
|
||||
a.merge(x).clone()
|
||||
});
|
||||
|
||||
Ok(final_config.try_into()?)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user