From a806fb4de2479715f779a69c470f4dff2fb856a9 Mon Sep 17 00:00:00 2001 From: Adrian Wannenmacher Date: Sun, 24 Sep 2023 12:23:20 +0200 Subject: [PATCH] fix cli completion generation --- Cargo.lock | 10 ++++++++++ Cargo.toml | 5 +++++ build.rs | 27 +++++++++++++++++++++++++++ src/cli.rs | 20 ++++++++++++++++++++ src/main.rs | 26 +++----------------------- 5 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 build.rs create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index b47c1bf..a7eaa45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,6 +130,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4110a1e6af615a9e6d0a36f805d5c99099f8bab9b8042f5bc1fa220a4a89e36f" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.2" @@ -465,6 +474,7 @@ version = "0.1.1" dependencies = [ "anyhow", "clap", + "clap_complete", "file-owner", "human-panic", "serde", diff --git a/Cargo.toml b/Cargo.toml index 89c8f15..abc6a74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,8 @@ file-owner = "0.1.2" human-panic = "1.2.0" serde = { version = "1.0.188", features = ["derive"] } serde_json = "1.0.107" + +[build-dependencies] +anyhow = "1.0.75" +clap = { version = "4.4.4", features = ["derive", "cargo", "wrap_help"] } +clap_complete = "4.4.1" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..189c3ac --- /dev/null +++ b/build.rs @@ -0,0 +1,27 @@ +use std::{env, fs::create_dir}; + +use anyhow::{Context, Result}; +use clap::CommandFactory; +use clap_complete::{ + generate_to, + shells::{Bash, Fish, Zsh}, +}; + +include!("src/cli.rs"); + +fn main() -> Result<()> { + let out = if env::var("CI").map(|ci| ci == "true").unwrap_or_default() { + create_dir("ci-out").context("failed to create CI output directory")?; + "./ci-out".to_string() + } else { + env::var("OUT_DIR").context("OUT_DIR not set")? + }; + + println!("cargo:rerun-if-changed=src/cli.rs"); + let mut cli = ::command(); + let _ = generate_to(Fish, &mut cli, env!("CARGO_PKG_NAME"), out.clone()); + let _ = generate_to(Bash, &mut cli, env!("CARGO_PKG_NAME"), out.clone()); + let _ = generate_to(Zsh, &mut cli, env!("CARGO_PKG_NAME"), out); + + Ok(()) +} diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..19bd0dd --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,20 @@ +use std::path::PathBuf; + +use clap::Parser; + +/// Command line arguments. +#[derive(Debug, Parser)] +#[clap(author, about, version)] +pub(crate) struct Args { + /// Whether to output data as json. + #[clap(long)] + pub json: bool, + + /// Whether to output raw uid and gid numbers. + #[clap(long)] + pub raw: bool, + + /// The roots to use for discovery. + #[clap(default_value = ".")] + pub roots: Vec, +} diff --git a/src/main.rs b/src/main.rs index 10caf16..24da36a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,11 @@ -use std::{ - fs::read_dir, - os::linux::fs::MetadataExt, - path::{Path, PathBuf}, -}; +use std::{fs::read_dir, os::linux::fs::MetadataExt, path::Path}; use anyhow::{ensure, Context, Result}; use clap::Parser; -use crate::{id::Ids, name::Names, output::Output}; +use crate::{cli::Args, id::Ids, name::Names, output::Output}; +mod cli; mod id; mod name; mod output; @@ -35,23 +32,6 @@ fn main() -> Result<()> { Ok(()) } -/// Command line arguments. -#[derive(Debug, Parser)] -#[clap(author, about, version)] -struct Args { - /// Whether to output data as json. - #[clap(long)] - pub json: bool, - - /// Whether to output raw uid and gid numbers. - #[clap(long)] - pub raw: bool, - - /// The roots to use for discovery. - #[clap(default_value = ".")] - pub roots: Vec, -} - /// Perform gid & uid gathering for a file, or a directory and its children. fn fs_entry(entry: &Path, summary: &mut Ids) -> Result<()> { let display = entry.display();