Improve output and handling

Adds an extra passthrough through yaml-rust due to the inability of serde_yaml
to produce yamllint-able output strings.
this will slow it down a bit, until serde_yaml is fixed.
This commit is contained in:
Jochen Maes 2023-11-01 08:43:39 +01:00
parent e7e0720b54
commit 282fe70e69
5 changed files with 54 additions and 22 deletions

20
Cargo.lock generated
View File

@ -151,6 +151,12 @@ version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.6.4"
@ -166,6 +172,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"yaml-rust",
] ]
[[package]] [[package]]
@ -214,9 +221,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.107" version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -336,3 +343,12 @@ name = "windows_x86_64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" 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",
]

View File

@ -9,8 +9,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
yaml-rust = "0.4.5"
clap = { version = "4.4.7", features = ["derive"] } clap = { version = "4.4.7", features = ["derive"] }
csv = "1.3.0" csv = "1.3.0"
serde = { version = "1.0.189", features = ["std", "derive"] } serde = { version = "1.0.190", features = ["std", "derive"] }
serde_json = "1.0.107" serde_json = "1.0.108"
serde_yaml = "0.9.25" serde_yaml = "0.9.27"

View File

@ -4,8 +4,7 @@
use crate::types::fact::Fact; use crate::types::fact::Fact;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use std::{collections::HashMap, env};
use std::env;
pub struct EnvironmentData {} pub struct EnvironmentData {}
impl EnvironmentData {} impl EnvironmentData {}
@ -14,18 +13,13 @@ impl EnvironmentData {}
pub struct EnvironmentValue { pub struct EnvironmentValue {
key: String, key: String,
value: String, value: String,
time_set: u128,
} }
impl Fact for EnvironmentData { impl Fact for EnvironmentData {
fn gather(&self) -> String { fn gather(&self) -> String {
let mut outmap: Vec<Value> = vec![]; let mut outmap: HashMap<String, String> = HashMap::new();
for (key, value) in env::vars() { for (key, value) in env::vars() {
let entry = json!({ outmap.insert(key, value);
"key": &key,
"value": value,
});
outmap.append(&mut vec![entry]);
} }
serde_json::to_string(&outmap).unwrap() serde_json::to_string(&outmap).unwrap()
} }

View File

@ -3,10 +3,8 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::{types::fact::Fact, util::command::get_result_as_string}; use crate::{types::fact::Fact, util::command::get_result_as_string};
use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
#[derive(Serialize, Deserialize)]
pub struct IPData {} pub struct IPData {}
impl IPData {} impl IPData {}
@ -14,7 +12,7 @@ impl Fact for IPData {
fn gather(&self) -> String { fn gather(&self) -> String {
let res = get_result_as_string("ip", vec!["-j", "addr"]); let res = get_result_as_string("ip", vec!["-j", "addr"]);
let jsonres: Value = serde_json::from_str(res.as_str()).unwrap(); 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() out.to_string()
} }
} }
@ -26,7 +24,7 @@ impl Fact for IPRouteData {
let res = get_result_as_string("ip", vec!["-j", "route"]); let res = get_result_as_string("ip", vec!["-j", "route"]);
let jsonres: Value = serde_json::from_str(res.as_str()).unwrap(); let jsonres: Value = serde_json::from_str(res.as_str()).unwrap();
let out = json!({ let out = json!({
"ip_route": jsonres "iproute": jsonres
}); });
out.to_string() out.to_string()
} }

View File

@ -13,8 +13,10 @@ use crate::gatherers::environment::EnvironmentData;
use crate::gatherers::ip::{IPData, IPRouteData}; use crate::gatherers::ip::{IPData, IPRouteData};
use crate::types::fact::Fact; use crate::types::fact::Fact;
use clap::Parser; use clap::Parser;
use serde::de::Error;
use serde_json::Value; use serde_json::Value;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use yaml_rust::{YamlEmitter, YamlLoader};
#[derive(Parser)] #[derive(Parser)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
@ -40,13 +42,21 @@ fn gather_list(gatherers: HashSet<String>, output: &str) -> String {
"ipaddr" => { "ipaddr" => {
outmap.insert( outmap.insert(
"ipaddr".to_string(), "ipaddr".to_string(),
serde_json::from_str(&IPData {}.gather()).unwrap(), serde_json::from_str::<Value>(&IPData {}.gather())
.unwrap()
.get("ipaddr")
.unwrap()
.clone(),
); );
} }
"iproute" => { "iproute" => {
outmap.insert( outmap.insert(
"iproute".to_string(), "iproute".to_string(),
serde_json::from_str(&IPRouteData {}.gather()).unwrap(), serde_json::from_str::<Value>(&IPRouteData {}.gather())
.unwrap()
.get("iproute")
.unwrap()
.clone(),
); );
} }
x => { x => {
@ -55,7 +65,7 @@ fn gather_list(gatherers: HashSet<String>, output: &str) -> String {
}; };
} }
match output { match output {
"json" => serde_json::to_string(&outmap).unwrap(), "json" => serde_json::to_string_pretty(&outmap).unwrap(),
"yaml" => serde_yaml::to_string(&outmap).unwrap(), "yaml" => serde_yaml::to_string(&outmap).unwrap(),
x => format!("Unknown output format {x}"), x => format!("Unknown output format {x}"),
} }
@ -79,5 +89,18 @@ fn main() {
gather_list(gatherers, output_format) 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}"),
}
} }