Skip to content

Commit

Permalink
Add shell-completions file generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
Coruscant11 committed Aug 30, 2023
1 parent 53e7470 commit b69069f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 24 deletions.
10 changes: 10 additions & 0 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 crates/noseyparker-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ vergen = { version = "8.1", features = ["build", "cargo", "git", "gitcl", "rustc
anyhow = { version = "1.0" }
bstr = { version = "1.0" }
clap = { version = "4.3", features = ["cargo", "derive", "env", "unicode", "wrap_help"] }
clap_complete = "4.4"
console = "0.15"
crossbeam-channel = "0.5"
indenter = "0.3"
Expand Down
37 changes: 37 additions & 0 deletions crates/noseyparker-cli/src/bin/noseyparker/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ pub enum Command {
#[command(display_order = 30)]
/// Manage rules
Rules(RulesArgs),

#[command(display_order = 30)]
/// Generate shell completions
ShellCompletions(ShellCompletionsArgs),
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -612,6 +616,39 @@ pub struct ReportArgs {
pub output_args: OutputArgs,
}


// -----------------------------------------------------------------------------
// `shell_completions` command
// -----------------------------------------------------------------------------
#[derive(ValueEnum, Debug, Clone)]
#[clap(rename_all = "lower")]
pub enum ShellFormat {
Bash,
Zsh,
Fish,
PowerShell,
Elvish
}

impl std::fmt::Display for ShellFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
ShellFormat::Bash => "bash",
ShellFormat::Zsh => "zsh",
ShellFormat::Fish => "fish",
ShellFormat::PowerShell => "powershell",
ShellFormat::Elvish => "elvish",
};
write!(f, "{s}")
}
}

#[derive(Args, Debug)]
pub struct ShellCompletionsArgs {
#[arg(long, short, value_name = "SHELL")]
pub shell: ShellFormat,
}

// -----------------------------------------------------------------------------
// output options
// -----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::args::{GlobalArgs, ShellFormat, ShellCompletionsArgs, CommandLineArgs};
use anyhow::Result;
use clap::{CommandFactory, Command};
use clap_complete::{generate, shells::Bash, shells::Zsh, shells::Fish, shells::PowerShell, shells::Elvish};

pub fn run(_global_args: &GlobalArgs, args: &ShellCompletionsArgs) -> Result<()> {
let mut cmd = CommandLineArgs::command();
generate_completions_for_shell(&args.shell, &mut cmd)
}

fn generate_completions_for_shell(shell: &ShellFormat, cmd: &mut Command) -> Result<()> {
let bin_name = "noseyparker";
let std_out = &mut std::io::stdout();

match shell {
ShellFormat::Bash => generate(Bash, cmd, bin_name, std_out),
ShellFormat::Zsh => generate(Zsh, cmd, bin_name, std_out),
ShellFormat::Fish => generate(Fish, cmd, bin_name, std_out),
ShellFormat::PowerShell => generate(PowerShell, cmd, bin_name, std_out),
ShellFormat::Elvish => generate(Elvish, cmd, bin_name, std_out),
}

Ok(())
}
8 changes: 5 additions & 3 deletions crates/noseyparker-cli/src/bin/noseyparker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod cmd_github;
mod cmd_report;
mod cmd_rules;
mod cmd_scan;
mod cmd_shell_completions;
mod cmd_summarize;

use args::GlobalArgs;
Expand All @@ -27,9 +28,9 @@ fn configure_tracing(global_args: &GlobalArgs) -> Result<()> {
.init()?;

let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(filter)
.with_ansi(global_args.use_color())
.finish();
.with_max_level(filter)
.with_ansi(global_args.use_color())
.finish();
tracing::subscriber::set_global_default(subscriber)?;

Ok(())
Expand Down Expand Up @@ -80,6 +81,7 @@ fn try_main() -> Result<()> {
args::Command::Scan(args) => cmd_scan::run(global_args, args),
args::Command::Summarize(args) => cmd_summarize::run(global_args, args),
args::Command::Report(args) => cmd_report::run(global_args, args),
args::Command::ShellCompletions(args) => cmd_shell_completions::run(global_args, args),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ and Git history.
Usage: noseyparker [OPTIONS] <COMMAND>

Commands:
scan Scan content for secrets
summarize Summarize scan findings
report Report detailed scan findings
github Interact with GitHub
datastore Manage datastores
rules Manage rules
help Print this message or the help of the given subcommand(s)
scan Scan content for secrets
summarize Summarize scan findings
report Report detailed scan findings
github Interact with GitHub
datastore Manage datastores
rules Manage rules
shell-completions Generate shell completions
help Print this message or the help of the given subcommand(s)

Options:
-h, --help
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ and Git history.
Usage: noseyparker [OPTIONS] <COMMAND>

Commands:
scan Scan content for secrets
summarize Summarize scan findings
report Report detailed scan findings
github Interact with GitHub
datastore Manage datastores
rules Manage rules
help Print this message or the help of the given subcommand(s)
scan Scan content for secrets
summarize Summarize scan findings
report Report detailed scan findings
github Interact with GitHub
datastore Manage datastores
rules Manage rules
shell-completions Generate shell completions
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help (see more with '--help')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ and Git history.
Usage: noseyparker [OPTIONS] <COMMAND>

Commands:
scan Scan content for secrets
summarize Summarize scan findings
report Report detailed scan findings
github Interact with GitHub
datastore Manage datastores
rules Manage rules
help Print this message or the help of the given subcommand(s)
scan Scan content for secrets
summarize Summarize scan findings
report Report detailed scan findings
github Interact with GitHub
datastore Manage datastores
rules Manage rules
shell-completions Generate shell completions
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help (see more with '--help')
Expand Down

0 comments on commit b69069f

Please sign in to comment.