diff --git a/manager/Cargo.lock b/manager/Cargo.lock index 1114995..8601cd7 100644 --- a/manager/Cargo.lock +++ b/manager/Cargo.lock @@ -75,7 +75,7 @@ dependencies = [ [[package]] name = "blueprint" version = "0.4.0" -source = "git+https://git.nubo.sh/hulthe/blueprint.git#6d382f9a34f1339bec524e7b8f1adb8e3488e635" +source = "git+https://git.nubo.sh/hulthe/blueprint.git#4808c454d6a46350cce20b48bd955ab7c9af6448" dependencies = [ "nom", "structopt", @@ -142,6 +142,7 @@ dependencies = [ "structopt", "thiserror", "tokio", + "toml", "xdg", ] @@ -738,6 +739,15 @@ dependencies = [ "syn", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "unicode-segmentation" version = "1.8.0" diff --git a/manager/Cargo.toml b/manager/Cargo.toml index 1ca7033..04d0b4a 100644 --- a/manager/Cargo.toml +++ b/manager/Cargo.toml @@ -12,6 +12,7 @@ futures = "0.3.13" async-recursion = "0.3.2" xdg = "2.2.0" thiserror = "1.0.24" +toml = "0.5.8" [dependencies.blueprint] git = "https://git.nubo.sh/hulthe/blueprint.git" diff --git a/manager/src/builder.rs b/manager/src/builder.rs index 820ccbb..c73f194 100644 --- a/manager/src/builder.rs +++ b/manager/src/builder.rs @@ -1,8 +1,9 @@ -use crate::error::{Error, ErrorLocation, Errors}; +use crate::error::{Error, ErrorLocation, Errors, InnerError}; use crate::Config; use async_recursion::async_recursion; use blueprint::{parse_template, Env, Value}; use futures::future::join_all; +use std::collections::HashMap; use std::env; use std::ffi::OsStr; use std::io::ErrorKind; @@ -19,6 +20,29 @@ pub async fn build_tree(cfg: &Config) -> Result<(), Errors> { env.insert("hostname".into(), Value::Str(get_hostname().await)); env.insert("username".into(), Value::Str(get_username())); + debug!("trying to read {:?}", cfg.variables_path); + if let Ok(s) = read_to_string(&cfg.variables_path).await { + debug!("parsing {:?}", cfg.variables_path); + let variables: HashMap = + toml::de::from_str(&s).with_location(&cfg.variables_path)?; + + for (key, toml_value) in variables { + let value = match toml_value { + toml::Value::String(s) => Value::Str(s), + toml::Value::Boolean(b) => Value::Bool(b), + _ => { + return Err(InnerError::TypeErr + .with_location(&cfg.variables_path) + .into()) + } + }; + + env.insert(key, value); + } + } else { + debug!("failed to read {:?}", cfg.variables_path); + } + for flag in &cfg.flags { env.insert(flag.to_string(), Value::Bool(true)); } diff --git a/manager/src/error.rs b/manager/src/error.rs index 1a1e2bc..b8d1ac0 100644 --- a/manager/src/error.rs +++ b/manager/src/error.rs @@ -19,6 +19,12 @@ pub enum InnerError { #[error("Failed to parse template file")] TemplateErr(#[from] blueprint::Error), + + #[error("Failed to parse toml file")] + TomlErr(#[from] toml::de::Error), + + #[error("Unsupported variable type")] + TypeErr, } impl From> for Errors { diff --git a/manager/src/main.rs b/manager/src/main.rs index 70c7cc1..1b039d1 100644 --- a/manager/src/main.rs +++ b/manager/src/main.rs @@ -26,6 +26,9 @@ struct Opt { #[structopt(short, long)] link_dir: Option, + #[structopt(long = "variables")] + variables_path: Option, + #[structopt(short, long)] print_variables: bool, @@ -40,6 +43,7 @@ pub struct Config { template_dir: PathBuf, build_dir: PathBuf, link_dir: PathBuf, + variables_path: PathBuf, flags: Vec, } @@ -77,6 +81,9 @@ async fn run() -> Result<(), Errors> { link_dir: opt .link_dir .unwrap_or_else(|| env::var("HOME").expect("$HOME").into()), + variables_path: opt + .variables_path + .unwrap_or_else(|| xdg_dirs.get_config_file("variables.toml")), flags: opt.flags, };