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

Configure Feed

Select the types of activity you want to include in your feed.

ft!: always use pretty printing for JSON and TOML

+31 -16
+4 -2
cli/src/main.rs
··· 40 40 41 41 #[derive(Debug, Parser)] 42 42 struct Args { 43 - #[arg(long, default_value_t = fuk::output::Format::JSONPretty)] 43 + #[arg(long, default_value_t = fuk::output::Format::JSON)] 44 44 format: fuk::output::Format, 45 45 /// File containing list of keys to be fetched 46 46 #[arg(value_hint = clap::ValueHint::FilePath)] ··· 54 54 55 55 let output = fuk::fuk(config); 56 56 57 - println!("{}", args.format.render(&output)); 57 + let out = std::io::stdout(); 58 + let mut out = out.lock(); 59 + args.format.render(&output, &mut out)?; 58 60 59 61 Ok(()) 60 62 }
+22 -12
cli/src/output/mod.rs
··· 3 3 // SPDX-License-Identifier: EUPL-1.2 4 4 5 5 use std::collections::HashMap; 6 + use std::io::{self, prelude::*}; 6 7 7 8 #[derive(PartialEq, Eq, Debug, Copy, Clone)] 8 9 pub enum Format { 9 10 JSON, 10 - JSONPretty, 11 11 TOML, 12 - TOMLPretty, 12 + CSV, 13 13 } 14 14 15 15 impl Format { 16 - pub fn render(self, output: &Output) -> String { 16 + pub fn render<W: Write>(self, output: &Output, w: &mut W) -> io::Result<()> { 17 17 match self { 18 - Format::JSON => serde_json::to_string(&output.keys).unwrap(), 19 - Format::JSONPretty => serde_json::to_string_pretty(&output.keys).unwrap(), 20 - Format::TOML => toml::to_string(&output.keys).unwrap(), 21 - Format::TOMLPretty => toml::to_string_pretty(&output.keys).unwrap(), 18 + Format::JSON => { 19 + serde_json::to_writer_pretty(&mut *w, &output.keys).map_err(io::Error::other)?; 20 + writeln!(w, "") 21 + } 22 + Format::TOML => write!(w, "{}", toml::to_string_pretty(&output.keys).unwrap()), 23 + Format::CSV => as_csv(w, &output), 22 24 } 23 25 } 24 26 } ··· 27 29 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 28 30 match *self { 29 31 Format::JSON => f.write_str("json"), 30 - Format::JSONPretty => f.write_str("json-pretty"), 31 32 Format::TOML => f.write_str("toml"), 32 - Format::TOMLPretty => f.write_str("toml-pretty"), 33 + Format::CSV => f.write_str("csv"), 33 34 } 34 35 } 35 36 } ··· 38 39 fn from(s: &'a str) -> Format { 39 40 match s { 40 41 "json" => Format::JSON, 41 - "pretty" => Format::JSONPretty, 42 - "json-pretty" => Format::JSONPretty, 43 42 "toml" => Format::TOML, 44 - "toml-pretty" => Format::TOMLPretty, 43 + "csv" => Format::CSV, 45 44 _ => unreachable!(), 46 45 } 47 46 } ··· 51 50 pub struct Output { 52 51 pub keys: HashMap<String, Vec<ssh_key::PublicKey>>, 53 52 } 53 + 54 + // TODO: proper escaping 55 + fn as_csv<W: Write>(w: &mut W, output: &Output) -> io::Result<()> { 56 + for (name, keys) in &output.keys { 57 + for key in keys { 58 + writeln!(w, "{name},{}", key.to_string())?; 59 + } 60 + } 61 + 62 + Ok(()) 63 + }
+5 -2
docs/fuk.1.scd
··· 7 7 # SYNOPSIS 8 8 9 9 *fuk* -h++ 10 - *fuk* [--format (json|json-pretty|toml|toml-pretty)] _config.toml_ 10 + *fuk* --help++ 11 + *fuk* [--format (json|toml|csv)] _config.toml_ 11 12 12 13 # DESCRIPTION 13 14 ··· 68 69 69 70 # OUTPUT 70 71 71 - _fuk_ prints result to stdout in JSON format. Keys will be deduplicated by 72 + _fuk_ prints result to stdout in selected format. Keys will be deduplicated by 72 73 algorithm and key, but the comment *will* be ignored when comparing for 73 74 equality. If comments for duplicated keys are different, then there is no 74 75 guarantee about comment content. 76 + 77 + Possible formats - JSON, TOML, CSV. 75 78 76 79 Example output: 77 80