diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index 3a28b1c..c641144 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -12,6 +12,7 @@ mod opts; use crate::error::StdResult; use config::Config; use opts::{AppOpt, Command, StructOpt}; +use std::io; fn main() -> StdResult<()> { let opt = AppOpt::from_args(); @@ -40,6 +41,13 @@ fn main() -> StdResult<()> { let config = Config::read(opt.config)?; commands::downgrade_applied_migrations(config)?; } + Command::Completions(opts) => { + AppOpt::clap().gen_completions_to( + env!("CARGO_BIN_NAME"), + opts.into(), + &mut io::stdout(), + ); + } } Ok(()) diff --git a/migra-cli/src/opts.rs b/migra-cli/src/opts.rs index a2fb4a6..db4b648 100644 --- a/migra-cli/src/opts.rs +++ b/migra-cli/src/opts.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use structopt::clap; pub use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -27,6 +28,8 @@ pub(crate) enum Command { #[structopt(name = "downgrade", visible_alias = "down")] Downgrade, + + Completions(CompletionsShell), } #[derive(Debug, StructOpt)] @@ -40,3 +43,24 @@ pub(crate) struct MakeCommandOpt { #[structopt(parse(from_str))] pub migration_name: String, } + +#[derive(Debug, StructOpt)] +pub(crate) enum CompletionsShell { + Bash, + Fish, + Zsh, + PowerShell, + Elvish, +} + +impl From for clap::Shell { + fn from(shell: CompletionsShell) -> Self { + match shell { + CompletionsShell::Bash => Self::Bash, + CompletionsShell::Fish => Self::Fish, + CompletionsShell::Zsh => Self::Zsh, + CompletionsShell::PowerShell => Self::PowerShell, + CompletionsShell::Elvish => Self::Elvish, + } + } +}