manager: Add os variable since hostname on macos is weird
This commit is contained in:
@ -3,16 +3,17 @@ use crate::Config;
|
||||
use async_recursion::async_recursion;
|
||||
use blueprint::{parse_template, Env, Value};
|
||||
use futures::future::join_all;
|
||||
use futures::TryFutureExt;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::str::from_utf8;
|
||||
use tokio::fs::{copy, create_dir, read_dir, read_to_string, File};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::join;
|
||||
use tokio::process::Command;
|
||||
|
||||
pub const TEMPLATE_EXTENSION: &str = "tpl";
|
||||
|
||||
@ -20,6 +21,7 @@ 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()));
|
||||
env.insert("os".into(), Value::Str(get_operating_system().await));
|
||||
|
||||
debug!("trying to read {:?}", cfg.variables_path);
|
||||
if let Ok(s) = read_to_string(&cfg.variables_path).await {
|
||||
@ -31,11 +33,7 @@ pub async fn build_tree(cfg: &Config) -> Result<(), Errors> {
|
||||
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())
|
||||
}
|
||||
_ => return Err(InnerError::Type.with_location(&cfg.variables_path).into()),
|
||||
};
|
||||
|
||||
env.insert(key, value);
|
||||
@ -170,26 +168,38 @@ fn get_username() -> String {
|
||||
env::var("USER")
|
||||
.ok()
|
||||
.or_else(|| env::var("USERNAME").ok())
|
||||
.unwrap_or_else(String::new)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
async fn get_hostname() -> String {
|
||||
async fn read_hostname_file() -> Option<String> {
|
||||
read_to_string("/etc/hostname").await.ok()
|
||||
async fn read_hostname_file() -> eyre::Result<String> {
|
||||
Ok(read_to_string("/etc/hostname").await?)
|
||||
}
|
||||
|
||||
// TODO: should be async
|
||||
fn run_hostname_cmd() -> Option<String> {
|
||||
async fn run_hostname_cmd() -> eyre::Result<String> {
|
||||
Command::new("hostname")
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|out| from_utf8(&out.stdout).ok().map(str::to_string))
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
.and_then(|out| Ok(from_utf8(&out.stdout).map(str::to_string)?))
|
||||
}
|
||||
|
||||
read_hostname_file()
|
||||
.or_else(|_| run_hostname_cmd())
|
||||
.await
|
||||
.or_else(run_hostname_cmd)
|
||||
.unwrap_or_else(String::new)
|
||||
.unwrap_or(String::new())
|
||||
.trim()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
async fn get_operating_system() -> String {
|
||||
Command::new("uname")
|
||||
.output()
|
||||
.await
|
||||
.ok()
|
||||
.as_ref()
|
||||
.and_then(|out| from_utf8(&out.stdout).ok())
|
||||
.unwrap_or("unknown")
|
||||
.trim()
|
||||
.to_lowercase()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user