-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_complete
295 lines (210 loc) · 9.44 KB
/
.bash_complete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
##=====================================================================================================================##
## plex complete
_comp_plex () {
# if [ "${#COMP_WORDS[@]}" != "2" ]; then
# return
# fi
## initialize the return array
COMPREPLY=()
## build the ssh hosts list
plex_args='start stop log pull status help env'
#echo -e "\e[32mplex_args: ${plex_args[@]}\e[0m"
## set the actual return value
#plexcomp=$(compgen -W "${plex_args[@]}" ${COMP_WORDS[COMP_CWORD]})
#echo -e "\e[32msshcomp: ${plex_args[@]}\e[0m"
COMPREPLY=($(compgen -W "$(echo ${plex_args[@]})" ${COMP_WORDS[COMP_CWORD]}))
## normal return
return 0
}
## run_plex.sh complete
complete -F _comp_plex ./run_plex.sh
##=====================================================================================================================##
## run_install.sh bash auto-complete
function _runcomp {
# if [ "${#COMP_WORDS[@]}" != "2" ]; then
# return
# fi
## initialize the return array
COMPREPLY=()
## build the ssh hosts list
runargs=("help deploy certs inst env build start stop status log pull")
runargs+=("beta dev recette prod")
runargs+=("remuser remhost remalias repodir")
#echo -e "\e[32mrunargs: ${runargs[@]}\e[0m"
## set the actual return value
COMPREPLY=($(compgen -W "$(echo ${runargs[@]})" ${COMP_WORDS[COMP_CWORD]}))
## normal return
return 0
}
## setup the ./run_install.sh script to use bash auto-complete
complete -F _runcomp ./run_install.sh
##=====================================================================================================================##
## ssh & scp complete
_comp_ssh () {
# if [ "${#COMP_WORDS[@]}" != "2" ]; then
# return
# fi
## initialize the return array
COMPREPLY=()
## build the ssh hosts list
#ssh_hosts=$(cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | grep -v ^# | uniq | grep -v "\[" ; cat ~/.ssh/config | grep -i "^Host " | awk '{print $2}'; )
#ssh_config=$(cat ~/.ssh/config | grep -i "^Host " | awk '{print $2}')
#ssh_knownhosts=$(cat ~/.ssh/known_hosts | awk '{print $1}' | sed 's/\[*\]*//g' | awk 'BEGIN {FS=","} {print $1}' | awk 'BEGIN {FS="."} {print $1}')
test -f ~/.ssh/config && ssh_config=$(cat ~/.ssh/config | grep -i "^Host " | awk '{print $2}') || ssh_config=""
test -f ~/.ssh/known_hosts && ssh_knownhosts=$(cat ~/.ssh/known_hosts | awk '{print $1}' | sed 's/\[*\]*//g' | awk 'BEGIN {FS=","} {print $1}' | awk 'BEGIN {FS="."} {print $1}') || ssh_knownhosts=""
#ssh_config='test1 test2 test3 test23 test23 test23 test1'
#ssh_config='test1 test2 test3 test23 test1 test2 test3 test23 test1 test2 test3 test23'
#hosts=($ssh_config)
hosts=($ssh_config $ssh_knownhosts)
hosts=$(echo "$hosts" | xargs -n1 | sort -u | xargs )
#echo -e "\e[32mhosts: ${hosts[@]}\e[0m"
## set the actual return value
#sshcomp=$(compgen -W "${hosts[@]}" ${COMP_WORDS[COMP_CWORD]})
#echo -e "\e[32msshcomp: ${hosts[@]}\e[0m"
COMPREPLY=($(compgen -W "$(echo ${hosts[@]})" ${COMP_WORDS[COMP_CWORD]}))
## normal return
return 0
}
## the actual complete commands
#complete -F _comp_ssh scp
complete -F _comp_ssh ssh
complete -F _comp_ssh ssh-copy-id
complete -F _comp_ssh ssh-display
complete -F _comp_ssh ssh-keygen
##=====================================================================================================================##
## git complete
function _comp_gitcommands {
## initialize the return array
COMPREPLY=()
## intialize the gitcmds array
gitcmds=()
## search for the git command
gitcmd=$(which git)
## if found run the codegen code
if [[ ! -z $gitcmd ]] ; then
## todo: fix the git help -a command awk (output has changed)
## build the git commands command
## static version (test)
#gitcmds=('status push pull commit clone add')
## old version
#gitcmds+=($(git help -a | awk '{if ($0 !~ /available subcommands/ && $0 !~ /available git commands/) {print $0}}' | xargs -n1))
## new version
gitcmds+=($(git help -a | awk '{if ($0 !~ /command/ && $0 !~ /Commands/ && $0 !~ /Interacting/) {print $1}}' | xargs -n1))
## add the local files
gitcmds+=($(git status --porcelain | awk '{print $2}' | xargs))
## inplace sort and uniquify
gitcmds=$(echo "${gitcmds[@]}" | xargs -n1 | sort -u | xargs )
## old csh command (template)
#complete git 'p/1/$gitcmds/'
## set the actual return value
COMPREPLY=($(compgen -W "$(echo ${gitcmds[@]})" -- ${COMP_WORDS[COMP_CWORD]}))
fi
## normal return
return 0
}
## the actual complete command
complete -F _comp_gitcommands git
##=====================================================================================================================##
## makefile complete
function _comp_makecommands {
## initialize the return array
COMPREPLY=()
## search for the make command
makecmd=$(which make)
#echo -e "\e[32mmakecmd: ${makecmd[@]}\e[0m"
## if found run the codegen code
if [[ ! -z $makecmd ]] ; then
## initialize some arrays
gitmakefiles=()
gitpaths=()
unimakefiles=()
makefiles=()
makeincludes=()
## build the gitpath makefile list
#gitpaths+=($(find . -maxdepth 3 -type d -exec test -e '{}/.git' ';' -print -prune | xargs))
#gitpaths+=($(find . -maxdepth 3 -type d -exec test -e '{}/.git' ';' -print | xargs))
#gitpaths+=($(find . -maxdepth 3 -type d -iname ".git" | xargs dirname))
#gitmakefiles+=($(find ${gitpaths[@]} -maxdepth 3 -type f -iname '*Makefile*' | xargs) )
## build the currmakefiles makefile list (wrt pwd)
currmakefiles+=($(find . -maxdepth 1 -type f -iname '*Makefile*' | xargs))
## todo write code to reccursively check includes and check only in the current directory & use -maxdepth 1 as i am going for the exact file
makeincludes=($(find . -type f -iname '*Makefile*' | xargs grep --color=auto -ish ^include | awk '{print $2}' | sed -e 's/\r//g' -e 's/(/{/g' -e 's/)/}/g' | xargs -n1 | cat -n | sort -uk2 | sort -nk1 | cut -f2- | xargs))
#echo -e "\e[32mgitpaths: ${gitpaths[@]}\e[0m"
#echo -e "\e[32mgitmakefiles: ${gitmakefiles[@]}\e[0m"
## build the unimake makefile list
#unimakefiles=("$UNIMAKE_DIR/Makefile.def $UNIMAKE_DIR/Makefile.veloce $UNIMAKE_DIR/Makefile.questa")
#unimakefiles=$(find $gitpath -iname "makefile.def") | xargs grep -oP '^include \K.*$' | awk 'BEGIN {FS=":"} {print $2}')
## build the general makefile list
makefiles+=(${currmakefiles[@]})
makefiles+=(${makeincludes[@]})
#makefiles+=(${gitmakefiles[@]})
#makefiles+=(${unimakefiles[@]})
## check if the files exist and run readlink on each item
for i in ${!makefiles[@]} ; do
## apply readlink if the element is non-empty else remove the element
if [[ -n makefiles[$i] ]] ; then
makefiles[$i]=$(readlink -f ${makefiles[$i]})
else
## unset the current element
unset makefiles[$i]
## rebase the array
makefiles=("${makefiles[@]}")
fi
done
maketargets=($(/bin/grep -hoE '^[a-zA-Z0-9_-]+:([^=]|$)' ${makefiles[@]} | sed 's/[^a-zA-Z0-9_-]*$//' | sed 's/ //' | xargs -n1 | sort -u | xargs))
#echo -e "\e[32mmaketargets: ${maketargets[@]}\e[0m"
## set the actual return value
# COMPREPLY=($(compgen -W "$(echo ${maketargets[@]})" ${COMP_WORDS[COMP_CWORD]}))
COMPREPLY=($(compgen -W "$(echo ${maketargets[@]})" ${COMP_WORDS[COMP_CWORD]}))
fi
## normal return
return 0
}
## the actual complete command
complete -F _comp_makecommands make
##=====================================================================================================================##
## run compgen -c once and store the results
#cmdlist=""
#cmdlist=$(compgen -c | xargs)
## cd
#complete -W "$(compgen -a | xargs)" -- ${COMP_WORDS[COMP_CWORD]} cd
# aliases
#complete -W "$(alias | sed 's/alias //g' | sed 's/=.*//g' | xargs)" -- ${COMP_WORDS[COMP_CWORD]} alias
complete -W "$(compgen -a | xargs)" -- ${COMP_WORDS[COMP_CWORD]} alias
complete -W "$(compgen -a | xargs | xargs)" -- ${COMP_WORDS[COMP_CWORD]} unalias
## man, which & where
complete -W "$(compgen -a | xargs)" -- ${COMP_WORDS[COMP_CWORD]} man
complete -W "$(compgen -a | xargs)" -- ${COMP_WORDS[COMP_CWORD]} which
complete -W "$(compgen -a | xargs)" -- ${COMP_WORDS[COMP_CWORD]} where
# environment & local variables
complete -W "$(compgen -e | xargs)" -- ${COMP_WORDS[COMP_CWORD]} export
complete -W "$(compgen -ev | xargs)" -- ${COMP_WORDS[COMP_CWORD]} unset
## chgrp & chown
#complete -W "$(compgen -ug | xargs)" -- ${COMP_WORDS[COMP_CWORD]} chown
#complete -W "$(compgen -g | xargs)" -- ${COMP_WORDS[COMP_CWORD]} chgrp
## su
#complete -W "$(compgen -u | xargs)" -- ${COMP_WORDS[COMP_CWORD]} su
##=====================================================================================================================##
# ## local files
# _comp_files () {
# # if [ "${#COMP_WORDS[@]}" != "2" ]; then
# # return
# # fi
# ## initialize the return array
# COMPREPLY=()
# ## build the local files list
# files=($(ls . | xargs -n1 | sort -u | xargs) )
# #echo -e "\e[32mfiles: ${files[@]}\e[0m"
# ## set the actual return value
# #sshcomp=$(compgen -W "${files[@]}" ${COMP_WORDS[COMP_CWORD]})
# #echo -e "\e[32mfilecomp: ${files[@]}\e[0m"
# COMPREPLY=($(compgen -W "$(echo ${files[@]})" ${COMP_WORDS[COMP_CWORD]}))
# ## normal return
# return 0
# }
##=====================================================================================================================##
## load bash-completion general functions
#source /etc/bash_completion
#complete -o filenames -o bashdefault
##=====================================================================================================================##