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;
10
11#[derive(Debug, Clone)]
12enum Input {
13 File(Box<Path>),
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)]
42struct Args {
43 #[arg(long, default_value_t = fuk::output::Format::JSON)]
44 format: fuk::output::Format,
45 /// File containing list of keys to be fetched
46 #[arg(value_hint = clap::ValueHint::FilePath)]
47 file: Input,
48}
49
50fn main() -> Result<()> {
51 let args = Args::parse();
52
53 let config: fuk::config::Config = toml::from_str(&args.file.read_all()?)?;
54
55 let output = fuk::fuk(config);
56
57 let out = std::io::stdout();
58 let mut out = out.lock();
59 args.format.render(&output, &mut out)?;
60
61 Ok(())
62}