use mac_sys_info::get_mac_sys_info;
use std::process::exit;
use mac_sys_info::structs::MacSysInfo;
enum Action {
ShowHelp,
ShowSimple,
ShowComplex,
ShowRaw,
}
fn main() {
env_logger::init();
let info = get_mac_sys_info();
if let Err(e) = info {
eprintln!("There was an error. Can't get system information.");
eprintln!("Error: {}", e);
eprintln!("Are you running a Mac? Because this utility also compiles and runs on other UNIX platforms, like Linux distributions.");
eprintln!("Please feel free to add a bug report here:");
eprintln!(" https://github.com/phip1611/mac-sys-info");
exit(-1);
}
let info = info.unwrap();
let action = parse_args();
match action {
Action::ShowHelp => { print_help() }
Action::ShowSimple => { print_simple(&info) }
Action::ShowComplex => { print_complex(&info) }
Action::ShowRaw => { print_raw(&info) }
}
}
fn parse_args() -> Action {
let args: Vec<String> = std::env::args()
.map(|s| s.to_lowercase())
.collect();
if args.len() == 1 {
return Action::ShowSimple;
};
if args[1] == "-h" || args[1] == "--help" || args[1] == "help" || args[1] == "h" {
return Action::ShowHelp;
}
if args[1] == "-c" {
return Action::ShowComplex;
}
if args[1] == "-r" {
return Action::ShowRaw;
}
Action::ShowSimple
}
fn print_simple(info: &MacSysInfo) {
println!("CPU:");
println!(" name : {}", info.cpu_info().brand_string());
println!(" arch : {}", info.cpu_info().architecture());
println!(" cores : {}", info.cpu_info().num_cores());
println!(" frequency: {}GHz", info.cpu_info().frequency_ghz());
println!(" L1D-Cache: {}KB", info.cpu_info().cache_info().l1d_cache_size_kb());
println!(" L1I-Cache: {}KB", info.cpu_info().cache_info().l1i_cache_size_kb());
println!(" L2-Cache : {}KB", info.cpu_info().cache_info().l2_cache_size_kb());
println!(" L3-Cache : {}KB", info.cpu_info().cache_info().l3_cache_size_kb());
println!();
println!("Memory:");
println!(" total: {}MB", info.mem_info().total_memory_mb());
println!();
println!("OS:");
println!(" version: {}", info.os_info().os_version());
println!(" kernel : {}", info.os_info().kern_version());
}
fn print_help() {
println!("Simple output : '$ mac-sys-info'");
println!("Complex output : '$ mac-sys-info -c'");
println!("Raw (all options)): '$ mac-sys-info -r'");
println!("Help : '$ mac-sys-info -h'");
}
fn print_complex(info: &MacSysInfo) {
println!("{}", info.cpu_info());
println!("{}", info.cpu_info().cache_info());
println!("{}", info.mem_info());
println!("{}", info.os_info());
println!("{}", info.cpu_features());
}
fn print_raw(info: &MacSysInfo) {
println!("This list is equivalent to `$ sysctl -a`, but it's ordered alphabetically.");
println!("{:#?}", info.all_keys());
}