Skip to content
Myr

Merge pull request #265 from sttts/sttts-shellcheck

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Run shellcheck on bash completion

authored by Eric Paris · committed by Eric Paris · parents 60267ae, defeccc · Browse files · 3 files+51 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+51 added−15 removed

  • other:+51 added−15 removed

3 files+51 added−15 removed

modified:.travis.yml+5 added-1 removed
modified:bash_completions_test.go+30 added
2 unmodified lines
3import (4 "bytes"5 "fmt"6 "os"7 "os/exec"8 "strings"9 "testing"10)11
11 unmodified lines
23 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)24 }25}2627func runShellCheck(s string) error {28 excluded := []string{29 "SC2034", // PREFIX appears unused. Verify it or export it.30 }31 cmd := exec.Command("shellcheck", "-s", "bash", "-", "-e", strings.Join(excluded, ","))32 cmd.Stderr = os.Stderr33 cmd.Stdout = os.Stdout3435 stdin, err := cmd.StdinPipe()36 if err != nil {37 return err38 }39 go func() {40 defer stdin.Close()41 stdin.Write([]byte(s))42 }()4344 return cmd.Run()45}4647// World worst custom function, just keep telling you to enter hello!48const (49 bashCompletionFunc = `__custom_func() {50COMPREPLY=( "hello" )
76 unmodified lines
127 // check for subdirs_in_dir flags128 check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)129130 checkOmit(t, str, cmdDeprecated.Name())131132 // if available, run shellcheck against the script133 if err := exec.Command("which", "shellcheck").Run(); err != nil {134 return135 }136 err := runShellCheck(str)137 if err != nil {138 t.Fatalf("shellcheck failed: %v", err)139 }140}
modified:bash_completions.go+16 added-14 removed
59 unmodified lines
60}6162__handle_reply()63{64 __debug "${FUNCNAME}"64 __debug "${FUNCNAME[0]}"65 case $cur in66 -*)67 if [[ $(type -t compopt) = "builtin" ]]; then68 compopt -o nospace
5 unmodified lines
74 allflags=("${flags[*]} ${two_word_flags[*]}")75 fi76 COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") )77 if [[ $(type -t compopt) = "builtin" ]]; then78 [[ $COMPREPLY == *= ]] || compopt +o nospace78 [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace79 fi8081 # complete after --flag=abc82 if [[ $cur == *=* ]]; then
4 unmodified lines
87 local index flag88 flag="${cur%%=*}"89 __index_of_word "${flag}" "${flags_with_completion[@]}"90 if [[ ${index} -ge 0 ]]; then91 COMPREPLY=() PREFIX="" cur="${cur#*=}"91 COMPREPLY=()92 PREFIX=""93 cur="${cur#*=}"94 ${flags_completion[${index}]}95 if [ -n "${ZSH_VERSION}" ]; then96 # zfs completion needs --flag= prefix95 eval COMPREPLY=( "\${COMPREPLY[@]/#/${flag}=}" )97 eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"98 fi99 fi100 fi101 return 0;
48 unmodified lines
150}151152__handle_flag()153{152 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"154 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"155156 # if a command required a flag, and we found it, unset must_have_one_flag()157 local flagname=${words[c]}158 local flagvalue
2 unmodified lines
161 flagvalue=${flagname#*=} # take in as flagvalue after the =162 flagname=${flagname%%=*} # strip everything after the =163 flagname="${flagname}=" # but put the = back164 fi163 __debug "${FUNCNAME}: looking for ${flagname}"165 __debug "${FUNCNAME[0]}: looking for ${flagname}"166 if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then167 must_have_one_flag=()168 fi169170 # keep flag value with flagname as flaghash169 if [ ${flagvalue} ] ; then171 if [ -n "${flagvalue}" ] ; then172 flaghash[${flagname}]=${flagvalue}171 elif [ ${words[ $((c+1)) ]} ] ; then173 elif [ -n "${words[ $((c+1)) ]}" ] ; then174 flaghash[${flagname}]=${words[ $((c+1)) ]}175 else176 flaghash[${flagname}]="true" # pad "true" for bool flag177 fi
12 unmodified lines
190}191192__handle_noun()193{192 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"194 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"195196 if __contains_word "${words[c]}" "${must_have_one_noun[@]}"; then197 must_have_one_noun=()198 elif __contains_word "${words[c]}" "${noun_aliases[@]}"; then
5 unmodified lines
204}205206__handle_command()207{206 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"208 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"209210 local next_command211 if [[ -n ${last_command} ]]; then212 next_command="_${last_command}_${words[c]//:/__}"213 else214 if [[ $c -eq 0 ]]; then213 next_command="_$(basename ${words[c]//:/__})"215 next_command="_$(basename "${words[c]//:/__}")"216 else217 next_command="_${words[c]//:/__}"218 fi219 fi220 c=$((c+1))219 __debug "${FUNCNAME}: looking for ${next_command}"221 __debug "${FUNCNAME[0]}: looking for ${next_command}"222 declare -F $next_command >/dev/null && $next_command223}224225__handle_word()
1 unmodified line
227 if [[ $c -ge $cword ]]; then228 __handle_reply229 return230 fi229 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"231 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"232 if [[ "${words[c]}" == -* ]]; then233 __handle_flag234 elif __contains_word "${words[c]}" "${commands[@]}"; then235 __handle_command234 elif [[ $c -eq 0 ]] && __contains_word "$(basename ${words[c]})" "${commands[@]}"; then236 elif [[ $c -eq 0 ]] && __contains_word "$(basename "${words[c]}")" "${commands[@]}"; then237 __handle_command238 else239 __handle_noun240 fi
362 unmodified lines