Fetch User Keys - simple tool for fetching SSH keys from various sources

ft!: always use pretty printing for JSON and TOML

hauleth.dev d35e5c70 b6bf166e

verified
Changed files
+31 -16
cli
src
output
docs
+4 -2
cli/src/main.rs
···
#[derive(Debug, Parser)]
struct Args {
-
#[arg(long, default_value_t = fuk::output::Format::JSONPretty)]
+
#[arg(long, default_value_t = fuk::output::Format::JSON)]
format: fuk::output::Format,
/// File containing list of keys to be fetched
#[arg(value_hint = clap::ValueHint::FilePath)]
···
let output = fuk::fuk(config);
-
println!("{}", args.format.render(&output));
+
let out = std::io::stdout();
+
let mut out = out.lock();
+
args.format.render(&output, &mut out)?;
Ok(())
}
+22 -12
cli/src/output/mod.rs
···
// SPDX-License-Identifier: EUPL-1.2
use std::collections::HashMap;
+
use std::io::{self, prelude::*};
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum Format {
JSON,
-
JSONPretty,
TOML,
-
TOMLPretty,
+
CSV,
}
impl Format {
-
pub fn render(self, output: &Output) -> String {
+
pub fn render<W: Write>(self, output: &Output, w: &mut W) -> io::Result<()> {
match self {
-
Format::JSON => serde_json::to_string(&output.keys).unwrap(),
-
Format::JSONPretty => serde_json::to_string_pretty(&output.keys).unwrap(),
-
Format::TOML => toml::to_string(&output.keys).unwrap(),
-
Format::TOMLPretty => toml::to_string_pretty(&output.keys).unwrap(),
+
Format::JSON => {
+
serde_json::to_writer_pretty(&mut *w, &output.keys).map_err(io::Error::other)?;
+
writeln!(w, "")
+
}
+
Format::TOML => write!(w, "{}", toml::to_string_pretty(&output.keys).unwrap()),
+
Format::CSV => as_csv(w, &output),
}
}
}
···
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Format::JSON => f.write_str("json"),
-
Format::JSONPretty => f.write_str("json-pretty"),
Format::TOML => f.write_str("toml"),
-
Format::TOMLPretty => f.write_str("toml-pretty"),
+
Format::CSV => f.write_str("csv"),
}
}
}
···
fn from(s: &'a str) -> Format {
match s {
"json" => Format::JSON,
-
"pretty" => Format::JSONPretty,
-
"json-pretty" => Format::JSONPretty,
"toml" => Format::TOML,
-
"toml-pretty" => Format::TOMLPretty,
+
"csv" => Format::CSV,
_ => unreachable!(),
}
}
···
pub struct Output {
pub keys: HashMap<String, Vec<ssh_key::PublicKey>>,
}
+
+
// TODO: proper escaping
+
fn as_csv<W: Write>(w: &mut W, output: &Output) -> io::Result<()> {
+
for (name, keys) in &output.keys {
+
for key in keys {
+
writeln!(w, "{name},{}", key.to_string())?;
+
}
+
}
+
+
Ok(())
+
}
+5 -2
docs/fuk.1.scd
···
# SYNOPSIS
*fuk* -h++
-
*fuk* [--format (json|json-pretty|toml|toml-pretty)] _config.toml_
+
*fuk* --help++
+
*fuk* [--format (json|toml|csv)] _config.toml_
# DESCRIPTION
···
# OUTPUT
-
_fuk_ prints result to stdout in JSON format. Keys will be deduplicated by
+
_fuk_ prints result to stdout in selected format. Keys will be deduplicated by
algorithm and key, but the comment *will* be ignored when comparing for
equality. If comments for duplicated keys are different, then there is no
guarantee about comment content.
+
+
Possible formats - JSON, TOML, CSV.
Example output: