Add username to template env

This commit is contained in:
2021-08-10 12:27:13 +02:00
parent ff376ef27c
commit d5645e8479

View File

@ -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<String> {
read_to_string("/etc/hostname").await.ok()