use super::{ActionContext, ActionResult, CliAction};
use failure::ResultExt;
use std::fs::File;
use std::io::Write;
pub struct Command {}
impl<'a> CliAction for Command {
fn dispatch(context: ActionContext) -> ActionResult {
let shell = context
.matches
.value_of("SHELL")
.expect("shell name enforced by clap");
let shell = match shell {
"bash" => clap::Shell::Bash,
"elvish" => clap::Shell::Elvish,
"fish" => clap::Shell::Fish,
"powershell" => clap::Shell::PowerShell,
"zsh" => clap::Shell::Zsh,
_ => unreachable!(),
};
match context.matches.value_of("file") {
Some(file) => {
write_completions(shell, File::create(file).context("Creating completions file")?)
}
None => {
let stdout = std::io::stdout();
write_completions(shell, stdout.lock())
}
}
}
}
fn write_completions<W>(shell: clap::Shell, mut out: W) -> ActionResult
where
W: Write,
{
let app_yaml = load_yaml!("../app.yaml");
let mut app = clap::App::from(app_yaml);
app.gen_completions_to(crate_name!(), clap::Shell::Fish, &mut out);
out.flush()?;
if let clap::Shell::Fish = shell {
out.write_all(include_bytes!("completions/extra.fish"))
.context("Writing extra fish shell completion code")?;
};
Ok(())
}