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:
parent
e7e0720b54
commit
282fe70e69
20
Cargo.lock
generated
20
Cargo.lock
generated
@ -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",
|
||||
]
|
||||
|
@ -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"
|
||||
|
@ -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<Value> = vec![];
|
||||
let mut outmap: HashMap<String, String> = 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()
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
31
src/main.rs
31
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<String>, output: &str) -> String {
|
||||
"ipaddr" => {
|
||||
outmap.insert(
|
||||
"ipaddr".to_string(),
|
||||
serde_json::from_str(&IPData {}.gather()).unwrap(),
|
||||
serde_json::from_str::<Value>(&IPData {}.gather())
|
||||
.unwrap()
|
||||
.get("ipaddr")
|
||||
.unwrap()
|
||||
.clone(),
|
||||
);
|
||||
}
|
||||
"iproute" => {
|
||||
outmap.insert(
|
||||
"iproute".to_string(),
|
||||
serde_json::from_str(&IPRouteData {}.gather()).unwrap(),
|
||||
serde_json::from_str::<Value>(&IPRouteData {}.gather())
|
||||
.unwrap()
|
||||
.get("iproute")
|
||||
.unwrap()
|
||||
.clone(),
|
||||
);
|
||||
}
|
||||
x => {
|
||||
@ -55,7 +65,7 @@ fn gather_list(gatherers: HashSet<String>, 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}"),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user