From 045c87fbcd1cb8efe8bd3a41f55790b326ed6ee4 Mon Sep 17 00:00:00 2001 From: c-14 Date: Sun, 25 Sep 2022 12:15:33 +0200 Subject: 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 --- src/command/client.rs | 50 +--------------------------------------------- src/command/client/init.rs | 36 --------------------------------- src/command/init.rs | 36 +++++++++++++++++++++++++++++++++ src/command/mod.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 86 deletions(-) delete mode 100644 src/command/client/init.rs create mode 100644 src/command/init.rs (limited to 'src/command') diff --git a/src/command/client.rs b/src/command/client.rs index ae49b857..94148c09 100644 --- a/src/command/client.rs +++ b/src/command/client.rs @@ -1,18 +1,15 @@ use std::path::PathBuf; -use clap::{CommandFactory, Subcommand}; -use clap_complete::{generate, generate_to, Shell}; +use clap::Subcommand; use eyre::{Result, WrapErr}; use atuin_client::{database::Sqlite, settings::Settings}; -use atuin_common::utils::uuid_v4; #[cfg(feature = "sync")] mod sync; mod history; mod import; -mod init; mod search; mod stats; @@ -30,27 +27,9 @@ pub enum Cmd { /// Calculate statistics for your history Stats(stats::Cmd), - /// Output shell setup - #[clap(subcommand)] - Init(init::Cmd), - - /// Generate a UUID - Uuid, - /// Interactive history search Search(search::Cmd), - /// 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, - }, - #[cfg(feature = "sync")] #[clap(flatten)] Sync(sync::Cmd), @@ -70,34 +49,7 @@ impl Cmd { Self::History(history) => history.run(&settings, &mut db).await, Self::Import(import) => import.run(&mut db).await, Self::Stats(stats) => stats.run(&mut db, &settings).await, - Self::Init(init) => { - init.run(); - Ok(()) - } Self::Search(search) => search.run(&mut db, &settings).await, - Self::Uuid => { - println!("{}", 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(()) - } #[cfg(feature = "sync")] Self::Sync(sync) => sync.run(settings, &mut db).await, } diff --git a/src/command/client/init.rs b/src/command/client/init.rs deleted file mode 100644 index a2c6378c..00000000 --- a/src/command/client/init.rs +++ /dev/null @@ -1,36 +0,0 @@ -use clap::Parser; - -#[derive(Parser)] -pub enum Cmd { - /// Zsh setup - Zsh, - /// Bash setup - Bash, - /// Fish setup - Fish, -} - -fn init_zsh() { - let full = include_str!("../../shell/atuin.zsh"); - println!("{}", full); -} - -fn init_bash() { - let full = include_str!("../../shell/atuin.bash"); - println!("{}", full); -} - -fn init_fish() { - let full = include_str!("../../shell/atuin.fish"); - println!("{}", full); -} - -impl Cmd { - pub fn run(&self) { - match self { - Self::Zsh => init_zsh(), - Self::Bash => init_bash(), - Self::Fish => init_fish(), - } - } -} diff --git a/src/command/init.rs b/src/command/init.rs new file mode 100644 index 00000000..37453f93 --- /dev/null +++ b/src/command/init.rs @@ -0,0 +1,36 @@ +use clap::Parser; + +#[derive(Parser)] +pub enum Cmd { + /// Zsh setup + Zsh, + /// Bash setup + Bash, + /// Fish setup + Fish, +} + +fn init_zsh() { + let full = include_str!("../shell/atuin.zsh"); + println!("{}", full); +} + +fn init_bash() { + let full = include_str!("../shell/atuin.bash"); + println!("{}", full); +} + +fn init_fish() { + let full = include_str!("../shell/atuin.fish"); + println!("{}", full); +} + +impl Cmd { + pub fn run(&self) { + match self { + Self::Zsh => init_zsh(), + Self::Bash => init_bash(), + Self::Fish => init_fish(), + } + } +} diff --git a/src/command/mod.rs b/src/command/mod.rs index ca7fc724..393936b0 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -1,4 +1,5 @@ -use clap::Subcommand; +use clap::{CommandFactory, Subcommand}; +use clap_complete::{generate, generate_to, Shell}; use eyre::Result; #[cfg(feature = "client")] @@ -7,6 +8,8 @@ mod client; #[cfg(feature = "server")] mod server; +mod init; + #[derive(Subcommand)] #[clap(infer_subcommands = true)] pub enum AtuinCmd { @@ -18,6 +21,24 @@ pub enum AtuinCmd { #[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, + }, } impl AtuinCmd { @@ -27,6 +48,33 @@ impl AtuinCmd { 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(()) + } } } } -- cgit v1.3.1