|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# Provides tab completion for the main hadoop script. |
| 19 | +# |
| 20 | +# On debian-based systems, place in /etc/bash_completion.d/ and either restart |
| 21 | +# Bash or source the script manually (. /etc/bash_completion.d/hadoop.sh). |
| 22 | + |
| 23 | +_hadoop() { |
| 24 | + local script cur prev temp |
| 25 | + |
| 26 | + COMPREPLY=() |
| 27 | + cur=${COMP_WORDS[COMP_CWORD]} |
| 28 | + prev=${COMP_WORDS[COMP_CWORD-1]} |
| 29 | + script=${COMP_WORDS[0]} |
| 30 | + |
| 31 | + # Bash lets you tab complete things even if the script doesn't |
| 32 | + # exist (or isn't executable). Check to make sure it is, as we |
| 33 | + # need to execute it to get options/info |
| 34 | + if [ -f "$script" -a -x "$script" ]; then |
| 35 | + case $COMP_CWORD in |
| 36 | + 1) |
| 37 | + # Completing the first argument (the command). |
| 38 | + |
| 39 | + temp=`$script | grep -n "^\s*or"`; |
| 40 | + temp=`$script | head -n $((${temp%%:*} - 1)) | awk '/^ / {print $1}' | sort | uniq`; |
| 41 | + COMPREPLY=(`compgen -W "${temp}" -- ${cur}`); |
| 42 | + return 0;; |
| 43 | + |
| 44 | + 2) |
| 45 | + # Completing the second arg (first arg to the command) |
| 46 | + |
| 47 | + # The output of commands isn't hugely consistent, so certain |
| 48 | + # names are hardcoded and parsed differently. Some aren't |
| 49 | + # handled at all (mostly ones without args). |
| 50 | + case ${COMP_WORDS[1]} in |
| 51 | + dfs | dfsadmin | fs | job | pipes) |
| 52 | + # One option per line, enclosed in square brackets |
| 53 | + |
| 54 | + temp=`$script ${COMP_WORDS[1]} 2>&1 | awk '/^[ \t]*\[/ {gsub("[[\\]]", ""); print $1}'`; |
| 55 | + COMPREPLY=(`compgen -W "${temp}" -- ${cur}`); |
| 56 | + return 0;; |
| 57 | + |
| 58 | + jar) |
| 59 | + # Any (jar) file |
| 60 | + |
| 61 | + COMPREPLY=(`compgen -A file -- ${cur}`); |
| 62 | + return 0;; |
| 63 | + |
| 64 | + namenode) |
| 65 | + # All options specified in one line, |
| 66 | + # enclosed in [] and separated with | |
| 67 | + temp=`$script ${COMP_WORDS[1]} -help 2>&1 | grep Usage: | cut -d '[' -f 2- | awk '{gsub("] \\| \\[|]", " "); print $0}'`; |
| 68 | + COMPREPLY=(`compgen -W "${temp}" -- ${cur}`); |
| 69 | + return 0;; |
| 70 | + |
| 71 | + *) |
| 72 | + # Other commands - no idea |
| 73 | + |
| 74 | + return 1;; |
| 75 | + esac;; |
| 76 | + |
| 77 | + *) |
| 78 | + # Additional args |
| 79 | + |
| 80 | + case ${COMP_WORDS[1]} in |
| 81 | + dfs | fs) |
| 82 | + # DFS/FS subcommand completion |
| 83 | + # Pull the list of options, grep for the one the user is trying to use, |
| 84 | + # and then select the description of the relevant argument |
| 85 | + temp=$((${COMP_CWORD} - 1)); |
| 86 | + temp=`$script ${COMP_WORDS[1]} 2>&1 | grep -- "${COMP_WORDS[2]} " | awk '{gsub("[[ \\]]", ""); print $0}' | cut -d '<' -f ${temp}`; |
| 87 | + |
| 88 | + if [ ${#temp} -lt 1 ]; then |
| 89 | + # No match |
| 90 | + return 1; |
| 91 | + fi; |
| 92 | + |
| 93 | + temp=${temp:0:$((${#temp} - 1))}; |
| 94 | + |
| 95 | + # Now do completion based on the argument |
| 96 | + case $temp in |
| 97 | + path | src | dst) |
| 98 | + # DFS path completion |
| 99 | + temp=`$script ${COMP_WORDS[1]} -ls "${cur}*" 2>&1 | grep -vE '^Found ' | cut -f 1 | awk '{gsub("^.* ", ""); print $0;}'` |
| 100 | + COMPREPLY=(`compgen -W "${temp}" -- ${cur}`); |
| 101 | + return 0;; |
| 102 | + |
| 103 | + localsrc | localdst) |
| 104 | + # Local path completion |
| 105 | + COMPREPLY=(`compgen -A file -- ${cur}`); |
| 106 | + return 0;; |
| 107 | + |
| 108 | + *) |
| 109 | + # Other arguments - no idea |
| 110 | + return 1;; |
| 111 | + esac;; |
| 112 | + |
| 113 | + *) |
| 114 | + # Other subcommands - no idea |
| 115 | + return 1;; |
| 116 | + esac; |
| 117 | + esac; |
| 118 | + fi; |
| 119 | +} |
| 120 | + |
| 121 | +complete -F _hadoop hadoop |
0 commit comments