A fork of attic a self-hostable Nix Binary Cache server
1//! Global CLI Setup.
2
3use std::env;
4
5use anyhow::{anyhow, Result};
6use clap::{CommandFactory, Parser, Subcommand};
7use clap_complete::Shell;
8use enum_as_inner::EnumAsInner;
9
10use crate::command::cache::{self, Cache};
11use crate::command::get_closure::{self, GetClosure};
12use crate::command::login::{self, Login};
13use crate::command::push::{self, Push};
14use crate::command::r#use::{self, Use};
15use crate::command::watch_store::{self, WatchStore};
16
17/// Attic binary cache client.
18#[derive(Debug, Parser)]
19#[clap(version)]
20#[clap(propagate_version = true)]
21pub struct Opts {
22 #[clap(subcommand)]
23 pub command: Command,
24}
25
26#[derive(Debug, Subcommand, EnumAsInner)]
27pub enum Command {
28 Login(Login),
29 Use(Use),
30 Push(Push),
31 Cache(Cache),
32 WatchStore(WatchStore),
33
34 #[clap(hide = true)]
35 GetClosure(GetClosure),
36}
37
38/// Generate shell autocompletion files.
39#[derive(Debug, Parser)]
40pub struct GenCompletions {
41 /// The shell to generate autocompletion files for.
42 shell: Shell,
43}
44
45pub async fn run() -> Result<()> {
46 // https://github.com/clap-rs/clap/issues/1335
47 if let Some("gen-completions") = env::args().nth(1).as_deref() {
48 return gen_completions(env::args().nth(2)).await;
49 }
50
51 let opts = Opts::parse();
52
53 match opts.command {
54 Command::Login(_) => login::run(opts).await,
55 Command::Use(_) => r#use::run(opts).await,
56 Command::Push(_) => push::run(opts).await,
57 Command::Cache(_) => cache::run(opts).await,
58 Command::WatchStore(_) => watch_store::run(opts).await,
59 Command::GetClosure(_) => get_closure::run(opts).await,
60 }
61}
62
63async fn gen_completions(shell: Option<String>) -> Result<()> {
64 let shell: Shell = shell
65 .ok_or_else(|| anyhow!("Must specify a shell."))?
66 .parse()
67 .unwrap();
68
69 clap_complete::generate(shell, &mut Opts::command(), "attic", &mut std::io::stdout());
70
71 Ok(())
72}