Skip to content

Commit

Permalink
user decides completion output
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunSHamilton authored and Byron committed Oct 10, 2023
1 parent 6c913bf commit 215c3ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
36 changes: 7 additions & 29 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use anyhow::{Context, Result};
use clap::{CommandFactory, Parser};
use clap_complete::{generate, shells::Bash};
use clap_complete::{generate, generate_to};
use gitoxide_core as core;
use gitoxide_core::{pack::verify, repository::PathsOrPatterns};
use gix::bstr::{io::BufReadExt, BString};
Expand Down Expand Up @@ -1220,36 +1220,14 @@ pub fn main() -> Result<()> {
},
),
},
Subcommands::GenerateCompletions => {
Subcommands::GenerateCompletions { shell, out_dir } => {
let mut app = Args::command();

let bin_name = "gix";

app.set_bin_name(bin_name);

// TODO: For additonal shells, find preferred completion workflow
let outdir = std::path::Path::new("/etc/").join("bash_completion.d/");

match &mut std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&outdir.join(bin_name))
{
Ok(write_buffer) => {
generate(Bash, &mut app, bin_name, write_buffer);
// generate(Fish, &mut app, bin_name, write_buffer);
// generate(Zsh, &mut app, bin_name, write_buffer);
// generate(PowerShell, &mut app, bin_name, write_buffer);
// generate(Elvish, &mut app, bin_name, write_buffer);

println!("completion file generated in: {outdir:?}");
}
Err(e) => {
eprintln!("failed to open path '{outdir:?}': {}", e);
std::process::exit(1);
}
};
if let Some(out_dir) = out_dir {
generate_to(shell, &mut app, env!("CARGO_PKG_NAME"), &out_dir)?;
} else {
generate(shell, &mut app, env!("CARGO_PKG_NAME"), &mut std::io::stdout());
}

Ok(())
}
Expand Down
13 changes: 10 additions & 3 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use clap_complete::Shell;
use gitoxide_core as core;
use gix::bstr::BString;

Expand Down Expand Up @@ -134,9 +135,15 @@ pub enum Subcommands {
/// Subcommands that need no git repository to run.
#[clap(subcommand)]
Free(free::Subcommands),
/// Generate shell completions in `/etc/bash_completion.d/gix.bash`.
/// NOTE: Requires `sudo` to write to `/etc/`.
GenerateCompletions,
/// Generate shell completions
GenerateCompletions {
/// Shell for generating completions.
#[clap(long, short)]
shell: Shell,
/// Output directory. If not provided, will write to stdout.
#[clap(long, short)]
out_dir: Option<String>,
},
}

#[cfg(feature = "gitoxide-core-tools-archive")]
Expand Down

0 comments on commit 215c3ac

Please sign in to comment.