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

Refactor completion scripts and instructions for shell auto-completion #418

Merged
merged 1 commit into from
Sep 27, 2024
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
32 changes: 31 additions & 1 deletion docs/_advanced/tips/shell-completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,46 @@ To enable auto-completion for Zsh, run the following command to generate the com

```bash
noir --generate-completion zsh
# #compdef noir
# _arguments \
# ....
```

Then, move the generated script to your Zsh completions directory, typically `~/.zsh/completion/`. If this directory does not exist, you may need to create it. Ensure the script is named `_noir` to follow Zsh's naming convention for completion scripts.

```bash
noir --generate-completion zsh > ~/.zsh/completion/_noir
```

## Bash completion

For Bash, generate the completion script by running:

```bash
noir --generate-completion bash
# _noir_completions() {
# local cur prev opts
# ....
```

After generating the script, move it to the appropriate directory for Bash completions. This location can vary depending on your operating system and Bash configuration, but a common path is `/etc/bash_completion.d/` for system-wide availability, or `~/.local/share/bash-completion/completions/` for a single user. Ensure the script is executable and sourced in your Bash profile.

```bash
noir --generate-completion bash > ~/.local/share/bash-completion/completions/noir
```

After generating the script, move it to the appropriate directory for Bash completions. This location can vary depending on your operating system and Bash configuration, but a common path is `/etc/bash_completion.d/` for system-wide availability, or `~/.local/share/bash-completion/completions/` for a single user. Ensure the script is executable and sourced in your Bash profile.
## Fish completion

For Fish, generate the completion script by running:

```bash
noir --generate-completion fish
# function __fish_noir_needs_command
# ....
```

After generating the script, move it to the Fish completions directory, typically `~/.config/fish/completions/.` If this directory does not exist, you may need to create it. Ensure the script is named noir.fish to follow Fish's naming convention for completion scripts.

```bash
noir --generate-completion fish > ~/.config/fish/completions/noir.fish
```
4 changes: 2 additions & 2 deletions docs/_includes/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FLAGS:
--set-pvalue-json VALUE Specifies the value of the identified parameter for JSON data
--set-pvalue-path VALUE Specifies the value of the identified parameter for path parameters
--status-codes Display HTTP status codes for discovered endpoints
--exclude-codes Exclude specific HTTP status code
--exclude-codes 404,500 Exclude specific HTTP response codes (comma-separated)
--include-path Include file path in the plain result
--no-color Disable color output
--no-log Displaying only the results
Expand Down Expand Up @@ -48,7 +48,7 @@ FLAGS:
CONFIG:
--config-file ./config.yaml Specify the path to a configuration file in YAML format
--concurrency 100 Set concurrency
--generate-completion zsh Generate Zsh/Bash completion script
--generate-completion zsh Generate Zsh/Bash/Fish completion script

DEBUG:
-d, --debug Show debug messages
Expand Down
48 changes: 48 additions & 0 deletions src/completions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,52 @@ _noir_completions() {

complete -F _noir_completions noir
SCRIPT
end

def generate_fish_completion_script
<<-SCRIPT
function __fish_noir_needs_command
set -l cmd (commandline -opc)
if test (count $cmd) -eq 1
return 0
end
return 1
end

complete -c noir -n '__fish_noir_needs_command' -a '-b' -d 'Set base path'
complete -c noir -n '__fish_noir_needs_command' -a '-u' -d 'Set base URL for endpoints'
complete -c noir -n '__fish_noir_needs_command' -a '-f' -d 'Set output format'
complete -c noir -n '__fish_noir_needs_command' -a '-o' -d 'Write result to file'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue' -d 'Specifies the value of the identified parameter'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue-header' -d 'Specifies the value of the identified parameter for headers'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue-cookie' -d 'Specifies the value of the identified parameter for cookies'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue-query' -d 'Specifies the value of the identified parameter for query parameters'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue-form' -d 'Specifies the value of the identified parameter for form data'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue-json' -d 'Specifies the value of the identified parameter for JSON data'
complete -c noir -n '__fish_noir_needs_command' -a '--set-pvalue-path' -d 'Specifies the value of the identified parameter for path parameters'
complete -c noir -n '__fish_noir_needs_command' -a '--status-codes' -d 'Display HTTP status codes for discovered endpoints'
complete -c noir -n '__fish_noir_needs_command' -a '--exclude-codes' -d 'Exclude specific HTTP response codes (comma-separated)'
complete -c noir -n '__fish_noir_needs_command' -a '--include-path' -d 'Include file path in the plain result'
complete -c noir -n '__fish_noir_needs_command' -a '--no-color' -d 'Disable color output'
complete -c noir -n '__fish_noir_needs_command' -a '--no-log' -d 'Displaying only the results'
complete -c noir -n '__fish_noir_needs_command' -a '-T' -d 'Activates all taggers for full analysis coverage'
complete -c noir -n '__fish_noir_needs_command' -a '--use-taggers' -d 'Activates specific taggers'
complete -c noir -n '__fish_noir_needs_command' -a '--list-taggers' -d 'Lists all available taggers'
complete -c noir -n '__fish_noir_needs_command' -a '--send-req' -d 'Send results to a web request'
complete -c noir -n '__fish_noir_needs_command' -a '--send-proxy' -d 'Send results to a web request via an HTTP proxy'
complete -c noir -n '__fish_noir_needs_command' -a '--send-es' -d 'Send results to Elasticsearch'
complete -c noir -n '__fish_noir_needs_command' -a '--with-headers' -d 'Add custom headers to be included in the delivery'
complete -c noir -n '__fish_noir_needs_command' -a '--use-matchers' -d 'Send URLs that match specific conditions to the Deliver'
complete -c noir -n '__fish_noir_needs_command' -a '--use-filters' -d 'Exclude URLs that match specified conditions and send the rest to Deliver'
complete -c noir -n '__fish_noir_needs_command' -a '--diff-path' -d 'Specify the path to the old version of the source code for comparison'
complete -c noir -n '__fish_noir_needs_command' -a '-t' -d 'Specify the technologies to use'
complete -c noir -n '__fish_noir_needs_command' -a '--exclude-techs' -d 'Specify the technologies to be excluded'
complete -c noir -n '__fish_noir_needs_command' -a '--list-techs' -d 'Show all technologies'
complete -c noir -n '__fish_noir_needs_command' -a '--config-file' -d 'Specify the path to a configuration file in YAML format'
complete -c noir -n '__fish_noir_needs_command' -a '--concurrency' -d 'Set concurrency'
complete -c noir -n '__fish_noir_needs_command' -a '-d' -d 'Show debug messages'
complete -c noir -n '__fish_noir_needs_command' -a '-v' -d 'Show version'
complete -c noir -n '__fish_noir_needs_command' -a '--build-info' -d 'Show version and Build info'
complete -c noir -n '__fish_noir_needs_command' -a '-h' -d 'Show help'
SCRIPT
end
6 changes: 5 additions & 1 deletion src/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,22 @@ def run_options_parser
parser.separator "\n CONFIG:".colorize(:blue)
parser.on "--config-file ./config.yaml", "Specify the path to a configuration file in YAML format" { |var| noir_options["config_file"] = YAML::Any.new(var) }
parser.on "--concurrency 100", "Set concurrency" { |var| noir_options["concurrency"] = YAML::Any.new(var) }
parser.on "--generate-completion zsh", "Generate Zsh/Bash completion script" do |var|
parser.on "--generate-completion zsh", "Generate Zsh/Bash/Fish completion script" do |var|
case var
when "zsh"
puts generate_zsh_completion_script
STDERR.puts "\n> Instructions: Copy the content above and save it in the zsh-completion directory as _noir".colorize(:yellow)
when "bash"
puts generate_bash_completion_script
STDERR.puts "\n> Instructions: Copy the content above and save it in the .bashrc file as noir.".colorize(:yellow)
when "fish"
puts generate_fish_completion_script
STDERR.puts "\n> Instructions: Copy the content above and save it in the fish-completion directory as noir.fish".colorize(:yellow)
else
puts "ERROR: Invalid completion type.".colorize(:yellow)
puts "e.g., noir --generate-completion zsh"
puts "e.g., noir --generate-completion bash"
puts "e.g., noir --generate-completion fish"
end

exit
Expand Down
Loading