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 simple_eyre::eyre::Result;
6use clap::Parser;
7use std::path::Path;
8use tokio::fs::File;
9use tokio::io::{self, AsyncReadExt};
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 async 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).await,
31 Input::File(ref path) => {
32 let mut f = File::open(path).await?;
33 f.read_to_string(&mut buffer).await
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::JSONPretty)]
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
50#[tokio::main]
51async fn main() -> Result<()> {
52 let args = Args::parse();
53
54 let config: fuk::config::Config = toml::from_str(&args.file.read_all().await?)?;
55
56 let output = fuk::fuk(config).await;
57
58 println!("{}", args.format.render(&output));
59
60 Ok(())
61}