Addd flag to list used template variables

This commit is contained in:
2021-05-27 23:34:46 +02:00
parent 8d9937e44e
commit ce84eac3c3
4 changed files with 121 additions and 21 deletions

16
manager/Cargo.lock generated
View File

@ -74,8 +74,8 @@ dependencies = [
[[package]] [[package]]
name = "blueprint" name = "blueprint"
version = "0.3.0" version = "0.4.0"
source = "git+https://git.nubo.sh/hulthe/blueprint.git#0af5d72005bebca426eeff628c1f010d2e850f06" source = "git+https://git.nubo.sh/hulthe/blueprint.git#6d382f9a34f1339bec524e7b8f1adb8e3488e635"
dependencies = [ dependencies = [
"nom", "nom",
"structopt", "structopt",
@ -295,9 +295,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.94" version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -648,18 +648,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.24" version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" checksum = "fa6f76457f59514c7eeb4e59d891395fab0b2fd1d40723ae737d64153392e9c6"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.24" version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" checksum = "8a36768c0fbf1bb15eca10defa29526bda730a2376c2ab4393ccfa16fb1a318d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -3,21 +3,13 @@ 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 serde::Serialize;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::PathBuf; use std::path::PathBuf;
use tokio::fs::{copy, create_dir, read_dir, read_to_string, write}; use tokio::fs::{copy, create_dir, read_dir, read_to_string, write};
use tokio::join; use tokio::join;
#[derive(Serialize)] pub const TEMPLATE_EXTENSION: &str = "tpl";
struct TemplateContext<'a> {
hostname: &'a str,
lightmode: bool,
darkmode: bool,
}
const TEMPLATE_EXTENSION: &str = "tpl";
pub async fn build_tree(cfg: &Config) -> Result<(), Errors> { pub async fn build_tree(cfg: &Config) -> Result<(), Errors> {
let hostname_path: PathBuf = "/etc/hostname".into(); let hostname_path: PathBuf = "/etc/hostname".into();

View File

@ -4,11 +4,13 @@ extern crate log;
mod builder; mod builder;
mod error; mod error;
mod linker; mod linker;
mod peeker;
use builder::build_tree; use builder::build_tree;
use error::Errors; use error::Errors;
use linker::link_tree; use linker::link_tree;
use log::LevelFilter; use log::LevelFilter;
use peeker::peek_tree;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
@ -24,6 +26,9 @@ struct Opt {
#[structopt(short, long)] #[structopt(short, long)]
link_dir: Option<PathBuf>, link_dir: Option<PathBuf>,
#[structopt(short, long)]
print_variables: bool,
#[structopt(short, parse(from_occurrences))] #[structopt(short, parse(from_occurrences))]
verbosity: u8, verbosity: u8,
@ -75,11 +80,16 @@ async fn run() -> Result<(), Errors> {
flags: opt.flags, flags: opt.flags,
}; };
info!("building tree"); if opt.print_variables {
build_tree(&cfg).await?; info!("peeking tree");
peek_tree(&cfg).await?;
} else {
info!("building tree");
build_tree(&cfg).await?;
info!("linking tree"); info!("linking tree");
link_tree(&cfg).await?; link_tree(&cfg).await?;
}
Ok(()) Ok(())
} }

98
manager/src/peeker.rs Normal file
View File

@ -0,0 +1,98 @@
use crate::builder::TEMPLATE_EXTENSION;
use crate::error::{Error, ErrorLocation, Errors};
use crate::Config;
use async_recursion::async_recursion;
use blueprint::parse_template;
use futures::future::join_all;
use std::ffi::OsStr;
use std::path::PathBuf;
use tokio::fs::{read_dir, read_to_string};
use tokio::join;
pub async fn peek_tree(cfg: &Config) -> Result<(), Errors> {
for var in dir(cfg, PathBuf::new()).await? {
println!("{}", var);
}
Ok(())
}
#[async_recursion]
async fn dir(cfg: &Config, relative: PathBuf) -> Result<Vec<String>, Errors> {
let template_path = cfg.template_dir.join(&relative);
info!("traversing {:?}", template_path);
let mut walker = read_dir(&template_path)
.await
.with_location(&template_path)?;
let mut dir_tasks = vec![];
let mut file_tasks = vec![];
while let Some(entry) = walker.next_entry().await.with_location(&&template_path)? {
let meta = entry.metadata().await.with_location(&entry.path())?;
let new_relative = relative.join(entry.file_name());
if meta.is_dir() {
dir_tasks.push(dir(cfg, new_relative));
} else if meta.is_file() {
file_tasks.push(file(cfg, new_relative));
}
}
let dirs = async { join_all(dir_tasks).await.into_iter().collect::<Vec<_>>() };
let files = async { join_all(file_tasks).await.into_iter().collect::<Vec<_>>() };
let (dirs, files) = join!(dirs, files);
let mut vars = vec![];
let mut errors = vec![];
for result in files.into_iter() {
match result {
Ok(mut more_vars) => vars.append(&mut more_vars),
Err(error) => errors.push(error),
}
}
let mut errors: Errors = errors.into();
for result in dirs.into_iter() {
match result {
Ok(mut more_vars) => vars.append(&mut more_vars),
Err(error) => errors.join(error),
}
}
if errors.is_empty() {
vars.sort_unstable();
vars.dedup();
Ok(vars)
} else {
Err(errors)
}
}
async fn file(cfg: &Config, relative: PathBuf) -> Result<Vec<String>, Error> {
let template_path = cfg.template_dir.join(&relative);
if template_path.extension() != Some(OsStr::new(TEMPLATE_EXTENSION)) {
return Ok(vec![]);
}
debug!("reading {:?}", template_path);
// parse template
let file_str = read_to_string(&template_path)
.await
.with_location(&template_path)?;
let vars = parse_template(&file_str)
.with_location(&template_path)?
.list_variables()
.into_iter()
.map(|s| s.to_string())
.collect();
Ok(vars)
}