cuddle-please/crates/cuddle-please-frontend/src/lib.rs
kjuulh e51454088e
refactor: move gitea out of the way
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-08-01 16:38:30 +02:00

109 lines
2.6 KiB
Rust

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(&self) -> &str {
&self.project.owner
}
pub fn get_repository(&self) -> &str {
&self.project.repository
}
pub fn get_source(&self) -> &PathBuf {
&self.project.source
}
pub fn get_branch(&self) -> &str {
&self.project.branch
}
pub fn get_api_url(&self) -> &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>,
source: 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 with_source(&mut self, source: &Path) -> &mut Self {
self.source = Some(gatheres::get_source(source));
self
}
pub fn build(&mut self) -> anyhow::Result<PleaseConfig> {
let gathered = vec![
&self.execution_env,
&self.source,
&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()
});
final_config.try_into()
}
}