feat: add command to gen shell completions

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-23 18:16:26 +03:00
parent f4ad03cee0
commit d1d09ac912
2 changed files with 32 additions and 0 deletions

View File

@ -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(())

View File

@ -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<CompletionsShell> 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,
}
}
}