Fetch User Keys - simple tool for fetching SSH keys from various sources
1// SPDX-FileCopyrightText: 2024 Łukasz Niemier <#@hauleth.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5use clap::Parser;
6use simple_eyre::eyre::Result;
7use std::fs::File;
8use std::io::{self, prelude::*};
9use std::path::{Path, PathBuf};
10
11#[derive(Debug, Clone)]
12enum Input {
13 File(PathBuf),
14 Stdio,
15}
16
17impl<'a> From<&'a str> for Input {
18 fn from(input: &'a str) -> Self {
19 match input {
20 "-" => Input::Stdio,
21 other => Input::File(Path::new(other).into()),
22 }
23 }
24}
25
26impl Input {
27 fn read_all(&self) -> io::Result<String> {
28 let mut buffer = String::new();
29 let result = match *self {
30 Input::Stdio => io::stdin().read_to_string(&mut buffer),
31 Input::File(ref path) => {
32 let mut f = File::open(path)?;
33 f.read_to_string(&mut buffer)
34 }
35 };
36
37 result.map(|_| buffer)
38 }
39}
40
41#[derive(Debug, Parser)]
42#[command(version, about)]
43struct Args {
44 /// Output format type
45 #[arg(long, default_value_t = fuk::output::Format::JSON)]
46 format: fuk::output::Format,
47 /// File containing list of keys to be fetched
48 #[arg(value_hint = clap::ValueHint::FilePath)]
49 file: Input,
50}
51
52fn main() -> Result<()> {
53 let args = Args::parse();
54
55 let config: fuk::config::Config = toml::from_str(&args.file.read_all()?)?;
56
57 let output = fuk::fuk(config);
58
59 let out = std::io::stdout();
60 let mut out = out.lock();
61 args.format.render(&output, &mut out)?;
62
63 Ok(())
64}