Add username to template env
This commit is contained in:
@ -3,6 +3,7 @@ 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::env;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@ -16,6 +17,7 @@ pub const TEMPLATE_EXTENSION: &str = "tpl";
|
|||||||
pub async fn build_tree(cfg: &Config) -> Result<(), Errors> {
|
pub async fn build_tree(cfg: &Config) -> Result<(), Errors> {
|
||||||
let mut env = Env::new();
|
let mut env = Env::new();
|
||||||
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()));
|
||||||
|
|
||||||
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));
|
||||||
@ -116,6 +118,13 @@ async fn file(cfg: &Config, env: &Env, relative: PathBuf) -> Result<(), Error> {
|
|||||||
Ok(())
|
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 get_hostname() -> String {
|
||||||
async fn read_hostname_file() -> Option<String> {
|
async fn read_hostname_file() -> Option<String> {
|
||||||
read_to_string("/etc/hostname").await.ok()
|
read_to_string("/etc/hostname").await.ok()
|
||||||
|
|||||||
Reference in New Issue
Block a user