From 7abfb11a0b64cd462efccd2a089b59b2f7111ea1 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sun, 11 Apr 2021 14:10:35 +0530 Subject: [PATCH] Use `shellcheck` on Bash completion file --- Library/Homebrew/completions/bash.erb | 48 ++++++++++++++++----------- Library/Homebrew/style.rb | 2 +- completions/bash/brew | 48 ++++++++++++++++----------- 3 files changed, 59 insertions(+), 39 deletions(-) diff --git a/Library/Homebrew/completions/bash.erb b/Library/Homebrew/completions/bash.erb index f93aefb5f5..7490c34388 100644 --- a/Library/Homebrew/completions/bash.erb +++ b/Library/Homebrew/completions/bash.erb @@ -51,50 +51,56 @@ __brewcomp() { done IFS="$sep" - COMPREPLY+=($(compgen -W "$list" -- "$cur")) + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$list" -- "$cur") } # Don't use __brewcomp() in any of the __brew_complete_foo functions, as # it is too slow and is not worth it just for duplicate elimination. __brew_complete_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" - local formulae="$(brew formulae)" - COMPREPLY+=($(compgen -W "$formulae" -- "$cur")) + local formulae + formulae="$(brew formulae)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$formulae" -- "$cur") } __brew_complete_casks() { local cur="${COMP_WORDS[COMP_CWORD]}" - local casks="$(brew casks)" - COMPREPLY+=($(compgen -W "$casks" -- "$cur")) + local casks + casks="$(brew casks)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$casks" -- "$cur") } __brew_complete_installed_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" - local installed_formulae="$(command ls "$(brew --cellar)" 2>/dev/null)" - COMPREPLY+=($(compgen -W "$installed_formulae" -- "$cur")) + local installed_formulae + installed_formulae="$(command ls "$(brew --cellar)" 2>/dev/null)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$installed_formulae" -- "$cur") } __brew_complete_installed_casks() { local cur="${COMP_WORDS[COMP_CWORD]}" - local installed_casks="$(command ls "$(brew --caskroom)" 2>/dev/null)" - COMPREPLY+=($(compgen -W "$installed_casks" -- "$cur")) + local installed_casks + installed_casks="$(command ls "$(brew --caskroom)" 2>/dev/null)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$installed_casks" -- "$cur") } __brew_complete_outdated_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" - local outdated_formulae="$(brew outdated --formula --quiet)" - COMPREPLY+=($(compgen -W "$outdated_formulae" -- "$cur")) + local outdated_formulae + outdated_formulae="$(brew outdated --formula --quiet)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$outdated_formulae" -- "$cur") } __brew_complete_outdated_casks() { local cur="${COMP_WORDS[COMP_CWORD]}" - local outdated_casks="$(brew outdated --cask --quiet)" - COMPREPLY+=($(compgen -W "$outdated_casks" -- "$cur")) + local outdated_casks + outdated_casks="$(brew outdated --cask --quiet)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$outdated_casks" -- "$cur") } __brew_complete_tapped() { - local taplib="$(brew --repository)/Library/Taps" - local dir taps + local dir taps taplib + taplib="$(brew --repository)/Library/Taps" for dir in "$taplib"/*/* do @@ -108,13 +114,17 @@ __brew_complete_tapped() { __brew_complete_commands() { local cur="${COMP_WORDS[COMP_CWORD]}" + local cmds HOMEBREW_CACHE=$(brew --cache) HOMEBREW_REPOSITORY=$(brew --repo) # Do not auto-complete "*instal" or "*uninstal" aliases for "*install" commands. - [[ -f "$HOMEBREW_CACHE/all_commands_list.txt" ]] && - local cmds="$(cat "$HOMEBREW_CACHE/all_commands_list.txt" | \grep -v instal$)" || - local cmds="$(cat "$HOMEBREW_REPOSITORY/completions/internal_commands_list.txt" | \grep -v instal$)" - COMPREPLY+=($(compgen -W "$cmds" -- "$cur")) + if [[ -f "$HOMEBREW_CACHE/all_commands_list.txt" ]] + then + cmds="$(< "$HOMEBREW_CACHE/all_commands_list.txt" \grep -v instal$)" + else + cmds="$(< "$HOMEBREW_REPOSITORY/completions/internal_commands_list.txt" \grep -v instal$)" + fi + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$cmds" -- "$cur") } # compopt is only available in newer versions of bash diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index 8346871cc0..029ee745e4 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -176,7 +176,7 @@ module Homebrew if files.empty? files = [ HOMEBREW_BREW_FILE, - # TODO: HOMEBREW_REPOSITORY/"completions/bash/brew", + HOMEBREW_REPOSITORY/"completions/bash/brew", *HOMEBREW_LIBRARY.glob("Homebrew/*.sh"), *HOMEBREW_LIBRARY.glob("Homebrew/shims/**/*").map(&:realpath).uniq .reject { |path| path.directory? || path.basename.to_s == "cc" }, diff --git a/completions/bash/brew b/completions/bash/brew index f782ed8a83..5b573517ba 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -38,50 +38,56 @@ __brewcomp() { done IFS="$sep" - COMPREPLY+=($(compgen -W "$list" -- "$cur")) + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$list" -- "$cur") } # Don't use __brewcomp() in any of the __brew_complete_foo functions, as # it is too slow and is not worth it just for duplicate elimination. __brew_complete_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" - local formulae="$(brew formulae)" - COMPREPLY+=($(compgen -W "$formulae" -- "$cur")) + local formulae + formulae="$(brew formulae)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$formulae" -- "$cur") } __brew_complete_casks() { local cur="${COMP_WORDS[COMP_CWORD]}" - local casks="$(brew casks)" - COMPREPLY+=($(compgen -W "$casks" -- "$cur")) + local casks + casks="$(brew casks)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$casks" -- "$cur") } __brew_complete_installed_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" - local installed_formulae="$(command ls "$(brew --cellar)" 2>/dev/null)" - COMPREPLY+=($(compgen -W "$installed_formulae" -- "$cur")) + local installed_formulae + installed_formulae="$(command ls "$(brew --cellar)" 2>/dev/null)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$installed_formulae" -- "$cur") } __brew_complete_installed_casks() { local cur="${COMP_WORDS[COMP_CWORD]}" - local installed_casks="$(command ls "$(brew --caskroom)" 2>/dev/null)" - COMPREPLY+=($(compgen -W "$installed_casks" -- "$cur")) + local installed_casks + installed_casks="$(command ls "$(brew --caskroom)" 2>/dev/null)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$installed_casks" -- "$cur") } __brew_complete_outdated_formulae() { local cur="${COMP_WORDS[COMP_CWORD]}" - local outdated_formulae="$(brew outdated --formula --quiet)" - COMPREPLY+=($(compgen -W "$outdated_formulae" -- "$cur")) + local outdated_formulae + outdated_formulae="$(brew outdated --formula --quiet)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$outdated_formulae" -- "$cur") } __brew_complete_outdated_casks() { local cur="${COMP_WORDS[COMP_CWORD]}" - local outdated_casks="$(brew outdated --cask --quiet)" - COMPREPLY+=($(compgen -W "$outdated_casks" -- "$cur")) + local outdated_casks + outdated_casks="$(brew outdated --cask --quiet)" + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$outdated_casks" -- "$cur") } __brew_complete_tapped() { - local taplib="$(brew --repository)/Library/Taps" - local dir taps + local dir taps taplib + taplib="$(brew --repository)/Library/Taps" for dir in "$taplib"/*/* do @@ -95,13 +101,17 @@ __brew_complete_tapped() { __brew_complete_commands() { local cur="${COMP_WORDS[COMP_CWORD]}" + local cmds HOMEBREW_CACHE=$(brew --cache) HOMEBREW_REPOSITORY=$(brew --repo) # Do not auto-complete "*instal" or "*uninstal" aliases for "*install" commands. - [[ -f "$HOMEBREW_CACHE/all_commands_list.txt" ]] && - local cmds="$(cat "$HOMEBREW_CACHE/all_commands_list.txt" | \grep -v instal$)" || - local cmds="$(cat "$HOMEBREW_REPOSITORY/completions/internal_commands_list.txt" | \grep -v instal$)" - COMPREPLY+=($(compgen -W "$cmds" -- "$cur")) + if [[ -f "$HOMEBREW_CACHE/all_commands_list.txt" ]] + then + cmds="$(< "$HOMEBREW_CACHE/all_commands_list.txt" \grep -v instal$)" + else + cmds="$(< "$HOMEBREW_REPOSITORY/completions/internal_commands_list.txt" \grep -v instal$)" + fi + while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$cmds" -- "$cur") } # compopt is only available in newer versions of bash