Set up initial structure for the nix builder.
This commit is contained in:
4
src/config/mod.rs
Normal file
4
src/config/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
mod root;
|
||||
mod target;
|
||||
pub(crate) use root::Config;
|
||||
pub(crate) use target::TargetConfig;
|
||||
75
src/config/root.rs
Normal file
75
src/config/root.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::borrow::Cow;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
use crate::error::CustomError;
|
||||
|
||||
use super::TargetConfig;
|
||||
|
||||
/// This is the config struct for nix_builder.toml
|
||||
#[derive(Debug, Deserialize, Serialize, Default)]
|
||||
pub(crate) struct Config {
|
||||
#[serde(skip)]
|
||||
config_path: Option<PathBuf>,
|
||||
work_directory: Option<PathBuf>,
|
||||
targets: Vec<TargetConfig>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub(crate) fn new<P: AsRef<Path>>(root_dir: P) -> Result<Config, CustomError> {
|
||||
fn inner(root_dir: &Path) -> Result<Config, CustomError> {
|
||||
let file_path = root_dir.join("nix_builder.toml");
|
||||
Ok(Config {
|
||||
config_path: Some(file_path),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
inner(root_dir.as_ref())
|
||||
}
|
||||
|
||||
pub(crate) async fn load_from_file<P: Into<PathBuf>>(path: P) -> Result<Config, CustomError> {
|
||||
async fn inner(path: PathBuf) -> Result<Config, CustomError> {
|
||||
let contents = tokio::fs::read_to_string(&path).await?;
|
||||
let mut parsed_contents: Config = toml::from_str(contents.as_str())?;
|
||||
parsed_contents.config_path = Some(path);
|
||||
Ok(parsed_contents)
|
||||
}
|
||||
inner(path.into()).await
|
||||
}
|
||||
|
||||
pub(crate) async fn write_to_disk(&self) -> Result<(), CustomError> {
|
||||
let mut config_file = File::create(
|
||||
&self
|
||||
.config_path
|
||||
.as_ref()
|
||||
.expect("config_path must be set to write config to disk."),
|
||||
)
|
||||
.await?;
|
||||
config_file
|
||||
.write_all(toml::to_string(&self)?.as_bytes())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_target_config(
|
||||
&self,
|
||||
target_name: &str,
|
||||
) -> Result<Option<&TargetConfig>, CustomError> {
|
||||
let target_config = self.targets.iter().find(|t| t.name == target_name);
|
||||
Ok(target_config)
|
||||
}
|
||||
|
||||
pub(crate) fn get_work_directory(&self) -> Result<Cow<'_, Path>, CustomError> {
|
||||
let maybe_work_directory = self.work_directory.as_deref().map(Cow::Borrowed);
|
||||
if let Some(work_dir) = maybe_work_directory {
|
||||
return Ok(work_dir);
|
||||
}
|
||||
let current_dir: PathBuf = std::env::current_dir()?;
|
||||
let work_dir = current_dir.join("work");
|
||||
Ok(Cow::Owned(work_dir))
|
||||
}
|
||||
}
|
||||
45
src/config/target.rs
Normal file
45
src/config/target.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::borrow::Cow;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::error::CustomError;
|
||||
|
||||
use super::Config;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Default)]
|
||||
pub(crate) struct TargetConfig {
|
||||
pub(super) name: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub(super) repo: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) branch: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) path: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) attr: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) update: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub(super) update_branch: Option<String>,
|
||||
}
|
||||
|
||||
impl TargetConfig {
|
||||
pub(crate) fn get_target_directory<'target, 'root>(
|
||||
&'target self,
|
||||
config_root: &'root Config,
|
||||
) -> Result<Cow<'target, Path>, CustomError> {
|
||||
let work_directory = config_root.get_work_directory()?;
|
||||
Ok(Cow::Owned(work_directory.join(&self.name)))
|
||||
}
|
||||
|
||||
pub(crate) fn get_flake_directory<'target, 'root>(
|
||||
&'target self,
|
||||
config_root: &'root Config,
|
||||
) -> Result<Cow<'target, Path>, CustomError> {
|
||||
let target_directory = self.get_target_directory(config_root)?;
|
||||
Ok(Cow::Owned(target_directory.join("flake")))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user