diff --git a/Cargo.lock b/Cargo.lock index 32388a1..b6fefd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,6 +151,12 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "memchr" version = "2.6.4" @@ -166,6 +172,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", + "yaml-rust", ] [[package]] @@ -214,9 +221,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.107" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ "itoa", "ryu", @@ -336,3 +343,12 @@ name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index cf61be2..167ddff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +yaml-rust = "0.4.5" clap = { version = "4.4.7", features = ["derive"] } csv = "1.3.0" -serde = { version = "1.0.189", features = ["std", "derive"] } -serde_json = "1.0.107" -serde_yaml = "0.9.25" +serde = { version = "1.0.190", features = ["std", "derive"] } +serde_json = "1.0.108" +serde_yaml = "0.9.27" diff --git a/src/gatherers/environment.rs b/src/gatherers/environment.rs index 7e6db55..c6c40f4 100644 --- a/src/gatherers/environment.rs +++ b/src/gatherers/environment.rs @@ -4,8 +4,7 @@ use crate::types::fact::Fact; use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::env; +use std::{collections::HashMap, env}; pub struct EnvironmentData {} impl EnvironmentData {} @@ -14,18 +13,13 @@ impl EnvironmentData {} pub struct EnvironmentValue { key: String, value: String, - time_set: u128, } impl Fact for EnvironmentData { fn gather(&self) -> String { - let mut outmap: Vec = vec![]; + let mut outmap: HashMap = HashMap::new(); for (key, value) in env::vars() { - let entry = json!({ - "key": &key, - "value": value, - }); - outmap.append(&mut vec![entry]); + outmap.insert(key, value); } serde_json::to_string(&outmap).unwrap() } diff --git a/src/gatherers/ip.rs b/src/gatherers/ip.rs index a4faf5b..b394104 100644 --- a/src/gatherers/ip.rs +++ b/src/gatherers/ip.rs @@ -3,10 +3,8 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::{types::fact::Fact, util::command::get_result_as_string}; -use serde::{Deserialize, Serialize}; use serde_json::Value; -#[derive(Serialize, Deserialize)] pub struct IPData {} impl IPData {} @@ -14,7 +12,7 @@ impl Fact for IPData { fn gather(&self) -> String { let res = get_result_as_string("ip", vec!["-j", "addr"]); let jsonres: Value = serde_json::from_str(res.as_str()).unwrap(); - let out = json!({ "ip_addr": jsonres }); + let out = json!({ "ipaddr": jsonres }); out.to_string() } } @@ -26,7 +24,7 @@ impl Fact for IPRouteData { let res = get_result_as_string("ip", vec!["-j", "route"]); let jsonres: Value = serde_json::from_str(res.as_str()).unwrap(); let out = json!({ - "ip_route": jsonres + "iproute": jsonres }); out.to_string() } diff --git a/src/main.rs b/src/main.rs index 4b4bad5..970ac15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,10 @@ use crate::gatherers::environment::EnvironmentData; use crate::gatherers::ip::{IPData, IPRouteData}; use crate::types::fact::Fact; use clap::Parser; +use serde::de::Error; use serde_json::Value; use std::collections::{HashMap, HashSet}; +use yaml_rust::{YamlEmitter, YamlLoader}; #[derive(Parser)] #[clap(author, version, about, long_about = None)] @@ -40,13 +42,21 @@ fn gather_list(gatherers: HashSet, output: &str) -> String { "ipaddr" => { outmap.insert( "ipaddr".to_string(), - serde_json::from_str(&IPData {}.gather()).unwrap(), + serde_json::from_str::(&IPData {}.gather()) + .unwrap() + .get("ipaddr") + .unwrap() + .clone(), ); } "iproute" => { outmap.insert( "iproute".to_string(), - serde_json::from_str(&IPRouteData {}.gather()).unwrap(), + serde_json::from_str::(&IPRouteData {}.gather()) + .unwrap() + .get("iproute") + .unwrap() + .clone(), ); } x => { @@ -55,7 +65,7 @@ fn gather_list(gatherers: HashSet, output: &str) -> String { }; } match output { - "json" => serde_json::to_string(&outmap).unwrap(), + "json" => serde_json::to_string_pretty(&outmap).unwrap(), "yaml" => serde_yaml::to_string(&outmap).unwrap(), x => format!("Unknown output format {x}"), } @@ -79,5 +89,18 @@ fn main() { gather_list(gatherers, output_format) } }; - println!("{}", data_output); + match output_format { + "json" => println!("{}", data_output), + "yaml" => { + let tmp = format!("---\n{}", data_output); + let yr = YamlLoader::load_from_str(tmp.as_str()).unwrap(); + let mut out = String::new(); + { + let mut emitter = YamlEmitter::new(&mut out); + emitter.dump(&yr[0]).unwrap(); + }; + println!("{}", out) + } + x => println!("This should never print,.. but it did. Maybe this explains why: {x}"), + } }