Skip to content

feat: Add shell completion via clap_complete #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ xdg = "2.5.2"
xml-rs = "0.8.20"
xz2 = "0.1.7"
zstd = { version = "0.12.4", features = [ "zstdmt" ] }
clap_complete = "4.5.54"

[dependencies.hyper]
features = ["client", "http1", "http2", "runtime", "stream"]
Expand Down
17 changes: 16 additions & 1 deletion src/bin/nix-channel-index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::os::unix::ffi::OsStringExt;
use std::path::PathBuf;
use std::process;

use clap::Parser;
use clap::{CommandFactory, Parser};
use clap_complete::{generate, Shell};
use error_chain::ChainedError;
use futures::{future, StreamExt};
use nix_index::files::FileNode;
Expand Down Expand Up @@ -179,12 +180,26 @@ struct Args {
/// Show a stack trace in the case of a Nix evaluation error
#[clap(long)]
show_trace: bool,

/// Generate shell completions to stdout.
#[clap(long)]
completions: Option<Shell>,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

if let Some(shell) = args.completions {
generate(
shell,
&mut Args::command(),
"nix-channel-index",
&mut io::stdout(),
);
return;
}

if let Err(e) = update_index(&args).await {
eprintln!("error: {}", e);

Expand Down
12 changes: 11 additions & 1 deletion src/bin/nix-index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::io::{self, Write};
use std::path::PathBuf;
use std::process;

use clap::Parser;
use clap::{CommandFactory, Parser};
use clap_complete::{generate, Shell};
use error_chain::ChainedError;
use futures::future::Either;
use futures::{future, StreamExt};
Expand Down Expand Up @@ -153,12 +154,21 @@ struct Args {
/// Note: does not check if the cached data is up to date! Use only for development.
#[clap(long)]
path_cache: bool,

/// Generate shell completions to stdout.
#[clap(long)]
completions: Option<Shell>,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

if let Some(shell) = args.completions {
generate(shell, &mut Args::command(), "nix-index", &mut io::stdout());
return;
}

if let Err(e) = update_index(&args).await {
eprintln!("error: {}", e);

Expand Down
20 changes: 18 additions & 2 deletions src/bin/nix-locate.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
//! Tool for searching for files in nixpkgs packages
use std::collections::HashSet;
use std::ffi::OsStr;
use std::io;
use std::path::PathBuf;
use std::process;
use std::result;
use std::str;
use std::str::FromStr;

use clap::CommandFactory;
use clap::{value_parser, Parser};
use clap_complete::generate;
use clap_complete::Shell;
use error_chain::error_chain;
use nix_index::database;
use nix_index::files::{self, FileTreeEntry, FileType};
Expand Down Expand Up @@ -153,7 +157,10 @@ fn locate(args: &Args) -> Result<()> {
///
/// Handles parsing the values of more complex arguments.
fn process_args(matches: Opts) -> result::Result<Args, clap::Error> {
let pattern_arg = matches.pattern;
let pattern_arg = matches.pattern.ok_or(Opts::command().error(
clap::error::ErrorKind::MissingRequiredArgument,
"The argument <PATTERN> is missing",
))?;
let package_arg = matches.package;

let start_anchor = if matches.at_root { "^" } else { "" };
Expand Down Expand Up @@ -237,7 +244,7 @@ fn cache_dir() -> &'static OsStr {
struct Opts {
/// Pattern for which to search
// #[clap(name = "PATTERN")]
pattern: String,
pattern: Option<String>,

/// Directory where the index is stored
#[clap(short, long = "db", default_value_os = cache_dir(), env = "NIX_INDEX_DATABASE")]
Expand Down Expand Up @@ -293,6 +300,10 @@ struct Opts {
/// store path are omitted. This is useful for scripts that use the output of nix-locate.
#[clap(long)]
minimal: bool,

/// Generate shell completions to stdout.
#[clap(long)]
completions: Option<Shell>,
}

#[derive(clap::ValueEnum, Clone, Copy, Debug)]
Expand All @@ -318,6 +329,11 @@ impl FromStr for Color {
fn main() {
let args = Opts::parse();

if let Some(shell) = args.completions {
generate(shell, &mut Opts::command(), "nix-locate", &mut io::stdout());
return;
}

let args = process_args(args).unwrap_or_else(|e| e.exit());

if let Err(e) = locate(&args) {
Expand Down