Skip to content

Commit a9f78dd

Browse files
committed
HADOOP-3714. Add a new contrib, bash-tab-completion, which enables bash tab completion for the bin/hadoop script. Contributed by Chris Smith.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@676653 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4f1542b commit a9f78dd

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Trunk (unreleased changes)
4848
HADOOP-3149. Adds a way in which map/reducetasks can create multiple
4949
outputs. (Alejandro Abdelnur via ddas)
5050

51+
HADOOP-3714. Add a new contrib, bash-tab-completion, which enables
52+
bash tab completion for the bin/hadoop script. See the README file
53+
in the contrib directory for the installation. (Chris Smith via enis)
54+
5155
IMPROVEMENTS
5256

5357
HADOOP-3577. Tools to inject blocks into name node and simulated
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Bash tab completion support for the hadoop script.
2+
3+
On Debian-like distributions, the script can be placed in
4+
/etc/bash_completion.d/, and it will be sourced automatically by Bash. On
5+
other distributions, you may source the file manually (`. hadoop.sh') or
6+
source it from your bashrc (or equivalent) file.
7+
8+
The script allows tab completion of all the command names, subcommands for the
9+
'fs', 'dfsadmin', 'job', 'namenode' and 'pipe' commands, arguments of the 'jar'
10+
command and most arguments to the 'fs' subcommands (completing local and
11+
dfs paths as appropriate).
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)