atuin/src/command/mod.rs
c-14 045c87fbcd
Allow stateless commands to be run without config/database (#544)
* Allow stateless commands to be run without config/database

Fixes an issue where gen-completions fails trying to create a config
directory in restrained build environments/distribution.

* move non-db commands up to core subcommands

* re-add lost lines

* re-add lost lines

Co-authored-by: Conrad Ludgate <conrad.ludgate@truelayer.com>
2022-09-25 11:15:33 +01:00

81 lines
1.9 KiB
Rust

use clap::{CommandFactory, Subcommand};
use clap_complete::{generate, generate_to, Shell};
use eyre::Result;
#[cfg(feature = "client")]
mod client;
#[cfg(feature = "server")]
mod server;
mod init;
#[derive(Subcommand)]
#[clap(infer_subcommands = true)]
pub enum AtuinCmd {
#[cfg(feature = "client")]
#[clap(flatten)]
Client(client::Cmd),
/// Start an atuin server
#[cfg(feature = "server")]
#[clap(subcommand)]
Server(server::Cmd),
/// Output shell setup
#[clap(subcommand)]
Init(init::Cmd),
/// Generate a UUID
Uuid,
/// Generate shell completions
GenCompletions {
/// Set the shell for generating completions
#[clap(long, short)]
shell: Shell,
/// Set the output directory
#[clap(long, short)]
out_dir: Option<String>,
},
}
impl AtuinCmd {
pub fn run(self) -> Result<()> {
match self {
#[cfg(feature = "client")]
Self::Client(client) => client.run(),
#[cfg(feature = "server")]
Self::Server(server) => server.run(),
Self::Init(init) => {
init.run();
Ok(())
}
Self::Uuid => {
println!("{}", atuin_common::utils::uuid_v4());
Ok(())
}
Self::GenCompletions { shell, out_dir } => {
let mut cli = crate::Atuin::command();
match out_dir {
Some(out_dir) => {
generate_to(shell, &mut cli, env!("CARGO_PKG_NAME"), &out_dir)?;
}
None => {
generate(
shell,
&mut cli,
env!("CARGO_PKG_NAME"),
&mut std::io::stdout(),
);
}
}
Ok(())
}
}
}
}