Load templating variables from file
This commit is contained in:
12
manager/Cargo.lock
generated
12
manager/Cargo.lock
generated
@ -75,7 +75,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "blueprint"
|
name = "blueprint"
|
||||||
version = "0.4.0"
|
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 = [
|
dependencies = [
|
||||||
"nom",
|
"nom",
|
||||||
"structopt",
|
"structopt",
|
||||||
@ -142,6 +142,7 @@ dependencies = [
|
|||||||
"structopt",
|
"structopt",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"toml",
|
||||||
"xdg",
|
"xdg",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -738,6 +739,15 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "0.5.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-segmentation"
|
name = "unicode-segmentation"
|
||||||
version = "1.8.0"
|
version = "1.8.0"
|
||||||
|
|||||||
@ -12,6 +12,7 @@ futures = "0.3.13"
|
|||||||
async-recursion = "0.3.2"
|
async-recursion = "0.3.2"
|
||||||
xdg = "2.2.0"
|
xdg = "2.2.0"
|
||||||
thiserror = "1.0.24"
|
thiserror = "1.0.24"
|
||||||
|
toml = "0.5.8"
|
||||||
|
|
||||||
[dependencies.blueprint]
|
[dependencies.blueprint]
|
||||||
git = "https://git.nubo.sh/hulthe/blueprint.git"
|
git = "https://git.nubo.sh/hulthe/blueprint.git"
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
use crate::error::{Error, ErrorLocation, Errors};
|
use crate::error::{Error, ErrorLocation, Errors, InnerError};
|
||||||
use crate::Config;
|
use crate::Config;
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
use blueprint::{parse_template, Env, Value};
|
use blueprint::{parse_template, Env, Value};
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io::ErrorKind;
|
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("hostname".into(), Value::Str(get_hostname().await));
|
||||||
env.insert("username".into(), Value::Str(get_username()));
|
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<String, toml::Value> =
|
||||||
|
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 {
|
for flag in &cfg.flags {
|
||||||
env.insert(flag.to_string(), Value::Bool(true));
|
env.insert(flag.to_string(), Value::Bool(true));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,12 @@ pub enum InnerError {
|
|||||||
|
|
||||||
#[error("Failed to parse template file")]
|
#[error("Failed to parse template file")]
|
||||||
TemplateErr(#[from] blueprint::Error),
|
TemplateErr(#[from] blueprint::Error),
|
||||||
|
|
||||||
|
#[error("Failed to parse toml file")]
|
||||||
|
TomlErr(#[from] toml::de::Error),
|
||||||
|
|
||||||
|
#[error("Unsupported variable type")]
|
||||||
|
TypeErr,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<Error>> for Errors {
|
impl From<Vec<Error>> for Errors {
|
||||||
|
|||||||
@ -26,6 +26,9 @@ struct Opt {
|
|||||||
#[structopt(short, long)]
|
#[structopt(short, long)]
|
||||||
link_dir: Option<PathBuf>,
|
link_dir: Option<PathBuf>,
|
||||||
|
|
||||||
|
#[structopt(long = "variables")]
|
||||||
|
variables_path: Option<PathBuf>,
|
||||||
|
|
||||||
#[structopt(short, long)]
|
#[structopt(short, long)]
|
||||||
print_variables: bool,
|
print_variables: bool,
|
||||||
|
|
||||||
@ -40,6 +43,7 @@ pub struct Config {
|
|||||||
template_dir: PathBuf,
|
template_dir: PathBuf,
|
||||||
build_dir: PathBuf,
|
build_dir: PathBuf,
|
||||||
link_dir: PathBuf,
|
link_dir: PathBuf,
|
||||||
|
variables_path: PathBuf,
|
||||||
flags: Vec<String>,
|
flags: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,6 +81,9 @@ async fn run() -> Result<(), Errors> {
|
|||||||
link_dir: opt
|
link_dir: opt
|
||||||
.link_dir
|
.link_dir
|
||||||
.unwrap_or_else(|| env::var("HOME").expect("$HOME").into()),
|
.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,
|
flags: opt.flags,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user