Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
Add shell completions for CLI (#2285)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Pashev <pashev.igor@gmail.com>
  • Loading branch information
ip1981 authored Oct 2, 2020
1 parent bdd52c4 commit dd76e96
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
27 changes: 26 additions & 1 deletion iml-manager-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use iml_manager_cli::{
};

use std::process::exit;
use structopt::clap::Shell;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -54,6 +55,16 @@ pub enum App {
#[structopt(name = "debugql", setting = structopt::clap::AppSettings::Hidden)]
/// Direct GraphQL Access (for testing and debug)
DebugQl(api::GraphQlCommand),

#[structopt(name = "shell-completion", setting = structopt::clap::AppSettings::Hidden)]
/// Generate shell completion script
Shell {
shell: Shell,
#[structopt(short = "e", long = "executable", default_value = "iml")]
exe: String,
#[structopt(short = "o", long = "output")]
output: Option<String>,
},
}

#[tokio::main]
Expand All @@ -64,7 +75,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

tracing::debug!("Matching args {:?}", matches);

dotenv::from_path("/var/lib/chroma/iml-settings.conf").expect("Could not load cli env");
match matches {
App::Shell { .. } => (),
_ => {
dotenv::from_path("/var/lib/chroma/iml-settings.conf").expect("Could not load cli env")
}
}

let r = match matches {
App::DebugApi(command) => api_cli(command).await,
Expand All @@ -74,6 +90,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
App::Snapshot { command } => snapshot_cli(command).await,
App::Stratagem { command } => stratagem_cli(command).await,
App::UpdateRepoFile(config) => update_repo_file_cli(config).await,
App::Shell { shell, exe, output } => {
if let Some(out) = output {
let mut o = std::fs::File::create(out)?;
App::clap().gen_completions_to(exe, shell, &mut o);
} else {
App::clap().gen_completions_to(exe, shell, &mut std::io::stdout());
};
Ok(())
}
};

if let Err(e) = r {
Expand Down
26 changes: 26 additions & 0 deletions rust-iml.spec
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ mkdir -p %{buildroot}%{_presetdir}
cp 00-rust-iml-agent.preset %{buildroot}%{_presetdir}
mkdir -p %{buildroot}%{_sysconfdir}/iml/
cp settings.conf %{buildroot}%{_sysconfdir}/iml/iml-agent.conf
mkdir -p %{buildroot}%{_sysconfdir}/bash_completion.d
%{buildroot}%{_bindir}/iml shell-completion bash -e iml -o %{buildroot}%{_sysconfdir}/bash_completion.d/iml
mkdir -p %{buildroot}%{_datadir}/zsh/site-functions
%{buildroot}%{_bindir}/iml shell-completion zsh -e iml -o %{buildroot}%{_datadir}/zsh/site-functions/_iml

%package cli
Summary: IML manager CLI
Expand All @@ -86,6 +90,28 @@ Group: System Environment/Libraries
%files cli
%{_bindir}/iml

%package cli-bash-completion
Summary: IML manager CLI (bash completion script)
License: MIT
Group: System Environment/Libraries

%description cli-bash-completion
%{summary}

%files cli-bash-completion
%{_sysconfdir}/bash_completion.d/iml

%package cli-zsh-completion
Summary: IML manager CLI (zsh completion script)
License: MIT
Group: System Environment/Libraries

%description cli-zsh-completion
%{summary}

%files cli-zsh-completion
%{_datadir}/zsh/site-functions/_iml

%package config-cli
Summary: IML manager Config CLI
License: MIT
Expand Down

0 comments on commit dd76e96

Please sign in to comment.