From d5645e84792dab3dbde8bdfa21f00ac3c6e26b7c Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Tue, 10 Aug 2021 12:27:13 +0200 Subject: [PATCH] Add username to template env --- manager/src/builder.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/manager/src/builder.rs b/manager/src/builder.rs index 3a63d56..b5cbda4 100644 --- a/manager/src/builder.rs +++ b/manager/src/builder.rs @@ -3,6 +3,7 @@ use crate::Config; use async_recursion::async_recursion; use blueprint::{parse_template, Env, Value}; use futures::future::join_all; +use std::env; use std::ffi::OsStr; use std::io::ErrorKind; use std::path::PathBuf; @@ -16,6 +17,7 @@ pub const TEMPLATE_EXTENSION: &str = "tpl"; pub async fn build_tree(cfg: &Config) -> Result<(), Errors> { let mut env = Env::new(); env.insert("hostname".into(), Value::Str(get_hostname().await)); + env.insert("username".into(), Value::Str(get_username())); for flag in &cfg.flags { env.insert(flag.to_string(), Value::Bool(true)); @@ -116,6 +118,13 @@ async fn file(cfg: &Config, env: &Env, relative: PathBuf) -> Result<(), Error> { Ok(()) } +fn get_username() -> String { + env::var("USER") + .ok() + .or_else(|| env::var("USERNAME").ok()) + .unwrap_or_else(|| String::new()) +} + async fn get_hostname() -> String { async fn read_hostname_file() -> Option { read_to_string("/etc/hostname").await.ok()