Skip to content
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

Add support for shell-completions file generation #76

Merged
merged 2 commits into from
Aug 31, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- A default value (`datastore.np`) is now set for commands that take a datastore option ([#74](https://github.com/praetorian-inc/noseyparker/issues/74)).
This makes simpler `noseyparker` command-line invocations possible.

- A new `shell-completions` command has been added, which generates shell-specific completion scripts for zsh, bash, fish, powershell, and elvish ([#76](https://github.com/praetorian-inc/noseyparker/pull/76)).
These generated completion scripts make discovery of Nosey Parker's command-line API simpler.
Thank you @Coruscant11!


## [v0.14.0](https://github.com/praetorian-inc/noseyparker/releases/v0.14.0) (2023-08-17)

Expand Down
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(())
}
2 changes: 2 additions & 0 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 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