http-envinfo 0.1.0

This package provides JSON API of health for microservice. It's using rocket framework.
use get_if_addrs::get_if_addrs;
use log::{error, info};
use mac_address::mac_address_by_name;
use std::env;
use std::process::Command;
use std::time::SystemTime;
// use serde_json::{json, to_string};
use serde_json::to_string;
use whoami::{arch, devicename, distro, platform, realname, username};

// Use local librairies
use crate::local_environment::local_environment_struct::{NetInterface, ServerInfo, SysEnv};

fn get_hostname() -> Result<String, String> {
    let output = Command::new("hostname")
        .output()
        .map_err(|e| format!("Error executing 'hostname' command: {}", e))?;

    if !output.status.success() {
        return Err(format!(
            "The 'hostname' command failed with status: {}",
            output.status
        ));
    }

    let _hostname = String::from_utf8_lossy(&output.stdout);

    Ok(_hostname.trim().to_string())
}

fn get_network_info() -> Result<Vec<NetInterface>, String> {
    let mut net_info_vector: Vec<NetInterface> = Vec::new();

    let interfaces =
        get_if_addrs().map_err(|e| format!("Error getting network interfaces: {}", e))?;

    for interface in interfaces {
        // println!("{:#?}", interface);
        if !interface.addr.is_loopback() {
            let interface_name: String = interface.name;
            let ip: String = interface.addr.ip().to_string();
            let mut macaddress: String = "".to_string();

            match mac_address_by_name(&interface_name) {
                Ok(Some(mac)) => {
                    macaddress = mac.to_string();
                }
                Ok(None) => macaddress = "No MAC address found.".to_string(),
                Err(e) => error!("{:?}", e),
            }
            net_info_vector.push(NetInterface {
                name: String::from(&interface_name),
                ip: String::from(&ip),
                ip_v4: interface.addr.ip().is_ipv4(),
                ip_v6: interface.addr.ip().is_ipv6(),
                mac: String::from(&macaddress),
            });
        }
    }

    Ok(net_info_vector)
}

pub fn get_local_env() -> String {
    let start_time = SystemTime::now();

    let mut _hostname: String = "".to_string();
    let mut _environmentvars: Vec<SysEnv> = Vec::new();
    let mut _netinfo: Vec<NetInterface> = Vec::new();

    match get_hostname() {
        Ok(hostname) => _hostname = hostname,
        Err(_e) => _hostname = "No hostname found !".to_string(),
    }

    for (key, value) in env::vars() {
        _environmentvars.push(SysEnv {
            name: String::from(&key),
            value: String::from(&value),
        });
    }

    match get_network_info() {
        Ok(netinfo) => {
            // println!("{}", to_string(&netinfo).unwrap());
            _netinfo = netinfo;
        }
        Err(e) => println!("{}", e),
    }

    let serverinfo = ServerInfo {
        hostname: _hostname,
        device: devicename(),
        username: username(),
        realname: realname(),
        platform: platform().to_string(),
        cpu_arch: arch().to_string(),
        distro: distro(),
        net_interface: _netinfo,
        environment: _environmentvars,
    };

    // let end_time = SystemTime::now();
    info!(
        "Le traitement a pris {} ms",
        SystemTime::now()
            .duration_since(start_time)
            .unwrap()
            .as_millis()
    );

    to_string(&serverinfo).unwrap()
    // println!("JSON Object using json!: {}", to_string(&serverinfo).unwrap());
}