|
| 1 | +/* |
| 2 | +Copyright 2018 Humio Ltd. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +
|
| 15 | +This file is derived of source code from the Helm Project. |
| 16 | +You can find the original source here: https://github.com/helm/helm |
| 17 | +
|
| 18 | +The project is released under the same Apache 2.0 license, the original |
| 19 | +work is copyright of the Helm Authorer. |
| 20 | +*/ |
| 21 | + |
| 22 | +package cmd |
| 23 | + |
| 24 | +import ( |
| 25 | + "bytes" |
| 26 | + "fmt" |
| 27 | + "io" |
| 28 | + "os" |
| 29 | + |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +const completionDesc = ` |
| 34 | +Generate autocompletions script for Humio for the specified shell (bash or zsh). |
| 35 | +
|
| 36 | +This command can generate shell autocompletions. e.g. |
| 37 | +
|
| 38 | + $ humio completion bash |
| 39 | +
|
| 40 | +Can be sourced as such |
| 41 | +
|
| 42 | + $ source <(humio completion bash) |
| 43 | +` |
| 44 | + |
| 45 | +var ( |
| 46 | + completionShells = map[string]func(out io.Writer, cmd *cobra.Command) error{ |
| 47 | + "bash": runCompletionBash, |
| 48 | + "zsh": runCompletionZsh, |
| 49 | + } |
| 50 | +) |
| 51 | + |
| 52 | +func newCompletionCmd() *cobra.Command { |
| 53 | + out := os.Stdout |
| 54 | + |
| 55 | + shells := []string{} |
| 56 | + for s := range completionShells { |
| 57 | + shells = append(shells, s) |
| 58 | + } |
| 59 | + |
| 60 | + cmd := &cobra.Command{ |
| 61 | + Use: "completion SHELL", |
| 62 | + Short: "Generate autocompletions script for the specified shell (bash or zsh)", |
| 63 | + Long: completionDesc, |
| 64 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 65 | + return runCompletion(out, cmd, args) |
| 66 | + }, |
| 67 | + ValidArgs: shells, |
| 68 | + } |
| 69 | + |
| 70 | + return cmd |
| 71 | +} |
| 72 | + |
| 73 | +func runCompletion(out io.Writer, cmd *cobra.Command, args []string) error { |
| 74 | + if len(args) == 0 { |
| 75 | + return fmt.Errorf("shell not specified") |
| 76 | + } |
| 77 | + if len(args) > 1 { |
| 78 | + return fmt.Errorf("too many arguments, expected only the shell type") |
| 79 | + } |
| 80 | + run, found := completionShells[args[0]] |
| 81 | + if !found { |
| 82 | + return fmt.Errorf("unsupported shell type %q", args[0]) |
| 83 | + } |
| 84 | + |
| 85 | + return run(out, cmd) |
| 86 | +} |
| 87 | + |
| 88 | +func runCompletionBash(out io.Writer, cmd *cobra.Command) error { |
| 89 | + return cmd.Root().GenBashCompletion(out) |
| 90 | +} |
| 91 | + |
| 92 | +func runCompletionZsh(out io.Writer, cmd *cobra.Command) error { |
| 93 | + zshInitialization := `#compdef humio |
| 94 | +
|
| 95 | +__humio_bash_source() { |
| 96 | + alias shopt=':' |
| 97 | + alias _expand=_bash_expand |
| 98 | + alias _complete=_bash_comp |
| 99 | + emulate -L sh |
| 100 | + setopt kshglob noshglob braceexpand |
| 101 | + source "$@" |
| 102 | +} |
| 103 | +__humio_type() { |
| 104 | + # -t is not supported by zsh |
| 105 | + if [ "$1" == "-t" ]; then |
| 106 | + shift |
| 107 | + # fake Bash 4 to disable "complete -o nospace". Instead |
| 108 | + # "compopt +-o nospace" is used in the code to toggle trailing |
| 109 | + # spaces. We don't support that, but leave trailing spaces on |
| 110 | + # all the time |
| 111 | + if [ "$1" = "__humio_compopt" ]; then |
| 112 | + echo builtin |
| 113 | + return 0 |
| 114 | + fi |
| 115 | + fi |
| 116 | + type "$@" |
| 117 | +} |
| 118 | +__humio_compgen() { |
| 119 | + local completions w |
| 120 | + completions=( $(compgen "$@") ) || return $? |
| 121 | + # filter by given word as prefix |
| 122 | + while [[ "$1" = -* && "$1" != -- ]]; do |
| 123 | + shift |
| 124 | + shift |
| 125 | + done |
| 126 | + if [[ "$1" == -- ]]; then |
| 127 | + shift |
| 128 | + fi |
| 129 | + for w in "${completions[@]}"; do |
| 130 | + if [[ "${w}" = "$1"* ]]; then |
| 131 | + echo "${w}" |
| 132 | + fi |
| 133 | + done |
| 134 | +} |
| 135 | +__humio_compopt() { |
| 136 | + true # don't do anything. Not supported by bashcompinit in zsh |
| 137 | +} |
| 138 | +__humio_declare() { |
| 139 | + if [ "$1" == "-F" ]; then |
| 140 | + whence -w "$@" |
| 141 | + else |
| 142 | + builtin declare "$@" |
| 143 | + fi |
| 144 | +} |
| 145 | +__humio_ltrim_colon_completions() |
| 146 | +{ |
| 147 | + if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then |
| 148 | + # Remove colon-word prefix from COMPREPLY items |
| 149 | + local colon_word=${1%${1##*:}} |
| 150 | + local i=${#COMPREPLY[*]} |
| 151 | + while [[ $((--i)) -ge 0 ]]; do |
| 152 | + COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"} |
| 153 | + done |
| 154 | + fi |
| 155 | +} |
| 156 | +__humio_get_comp_words_by_ref() { |
| 157 | + cur="${COMP_WORDS[COMP_CWORD]}" |
| 158 | + prev="${COMP_WORDS[${COMP_CWORD}-1]}" |
| 159 | + words=("${COMP_WORDS[@]}") |
| 160 | + cword=("${COMP_CWORD[@]}") |
| 161 | +} |
| 162 | +__humio_filedir() { |
| 163 | + local RET OLD_IFS w qw |
| 164 | + __debug "_filedir $@ cur=$cur" |
| 165 | + if [[ "$1" = \~* ]]; then |
| 166 | + # somehow does not work. Maybe, zsh does not call this at all |
| 167 | + eval echo "$1" |
| 168 | + return 0 |
| 169 | + fi |
| 170 | + OLD_IFS="$IFS" |
| 171 | + IFS=$'\n' |
| 172 | + if [ "$1" = "-d" ]; then |
| 173 | + shift |
| 174 | + RET=( $(compgen -d) ) |
| 175 | + else |
| 176 | + RET=( $(compgen -f) ) |
| 177 | + fi |
| 178 | + IFS="$OLD_IFS" |
| 179 | + IFS="," __debug "RET=${RET[@]} len=${#RET[@]}" |
| 180 | + for w in ${RET[@]}; do |
| 181 | + if [[ ! "${w}" = "${cur}"* ]]; then |
| 182 | + continue |
| 183 | + fi |
| 184 | + if eval "[[ \"\${w}\" = *.$1 || -d \"\${w}\" ]]"; then |
| 185 | + qw="$(__humio_quote "${w}")" |
| 186 | + if [ -d "${w}" ]; then |
| 187 | + COMPREPLY+=("${qw}/") |
| 188 | + else |
| 189 | + COMPREPLY+=("${qw}") |
| 190 | + fi |
| 191 | + fi |
| 192 | + done |
| 193 | +} |
| 194 | +__humio_quote() { |
| 195 | + if [[ $1 == \'* || $1 == \"* ]]; then |
| 196 | + # Leave out first character |
| 197 | + printf %q "${1:1}" |
| 198 | + else |
| 199 | + printf %q "$1" |
| 200 | + fi |
| 201 | +} |
| 202 | +autoload -U +X bashcompinit && bashcompinit |
| 203 | +# use word boundary patterns for BSD or GNU sed |
| 204 | +LWORD='[[:<:]]' |
| 205 | +RWORD='[[:>:]]' |
| 206 | +if sed --help 2>&1 | grep -q GNU; then |
| 207 | + LWORD='\<' |
| 208 | + RWORD='\>' |
| 209 | +fi |
| 210 | +__humio_convert_bash_to_zsh() { |
| 211 | + sed \ |
| 212 | + -e 's/declare -F/whence -w/' \ |
| 213 | + -e 's/_get_comp_words_by_ref "\$@"/_get_comp_words_by_ref "\$*"/' \ |
| 214 | + -e 's/local \([a-zA-Z0-9_]*\)=/local \1; \1=/' \ |
| 215 | + -e 's/flags+=("\(--.*\)=")/flags+=("\1"); two_word_flags+=("\1")/' \ |
| 216 | + -e 's/must_have_one_flag+=("\(--.*\)=")/must_have_one_flag+=("\1")/' \ |
| 217 | + -e "s/${LWORD}_filedir${RWORD}/__humio_filedir/g" \ |
| 218 | + -e "s/${LWORD}_get_comp_words_by_ref${RWORD}/__humio_get_comp_words_by_ref/g" \ |
| 219 | + -e "s/${LWORD}__ltrim_colon_completions${RWORD}/__humio_ltrim_colon_completions/g" \ |
| 220 | + -e "s/${LWORD}compgen${RWORD}/__humio_compgen/g" \ |
| 221 | + -e "s/${LWORD}compopt${RWORD}/__humio_compopt/g" \ |
| 222 | + -e "s/${LWORD}declare${RWORD}/__humio_declare/g" \ |
| 223 | + -e "s/\\\$(type${RWORD}/\$(__humio_type/g" \ |
| 224 | + <<'BASH_COMPLETION_EOF' |
| 225 | +` |
| 226 | + out.Write([]byte(zshInitialization)) |
| 227 | + |
| 228 | + buf := new(bytes.Buffer) |
| 229 | + cmd.Root().GenBashCompletion(buf) |
| 230 | + out.Write(buf.Bytes()) |
| 231 | + |
| 232 | + zshTail := ` |
| 233 | +BASH_COMPLETION_EOF |
| 234 | +} |
| 235 | +__humio_bash_source <(__humio_convert_bash_to_zsh) |
| 236 | +` |
| 237 | + out.Write([]byte(zshTail)) |
| 238 | + return nil |
| 239 | +} |
0 commit comments