UNPKG

2.68 kBapplication/x-shView Raw
1#!/usr/bin/env bash
2#
3# Source this script from your .bashrc|.profile|... to enable argument auto-completion.
4
5cd "$(dirname ${BASH_SOURCE[0]})"
6
7all_services () {
8 echo "$($(dirname ${BASH_SOURCE[0]})/js/getServiceNames.js --composefile dc.prod.yml)"
9}
10
11testable_services () {
12 echo "$($(dirname ${BASH_SOURCE[0]})/js/getServiceNames.js --composefile dc.prod.yml --testable)"
13}
14
15available_services () {
16 local ALL_SERVICES
17 IFS=' ' read -r -a ALL_SERVICES <<< "$(all_services)"
18 local CURR_SERVICES=(${COMP_WORDS[@]:2:$1})
19 local NEXT_SERVICES=""
20 for I_ALL in ${!ALL_SERVICES[@]}
21 do
22 local FOUND=0
23 for I_CURR in "${!CURR_SERVICES[@]}"
24 do
25 if [ "${ALL_SERVICES[I_ALL]}" = "${CURR_SERVICES[I_CURR]}" ]
26 then
27 FOUND=1
28 break
29 fi
30 done
31 if [ ${FOUND} = 0 ]
32 then
33 NEXT_SERVICES+=" ${ALL_SERVICES[I_ALL]}"
34 fi
35 done
36 echo "${NEXT_SERVICES}"
37}
38
39_dx_completion () {
40 COMPREPLY=()
41
42 local ACTIONS=("build" "clean" "down" "help" "inspect" "logs" "outdated" "restart" "stop" "test" "up" "watch")
43
44 local CUR=${COMP_WORDS[COMP_CWORD]} # current word being autocompleted
45
46 if (( ${COMP_CWORD} == 1 ))
47 then
48 COMPREPLY=( `compgen -W "${ACTIONS[*]}" -- ${CUR}` )
49
50 # cases: `build`
51 elif [ ${COMP_WORDS[1]} == "build" ]
52 then
53 if (( ${COMP_CWORD} == 2 ))
54 then COMPREPLY=( `compgen -W "--dev --dxdev --test $(all_services)" -- ${CUR}` )
55 elif [ ${COMP_WORDS[2]} == "--dev" ] || [ ${COMP_WORDS[2]} == "--dxdev" ]
56 then COMPREPLY=( `compgen -W "$(available_services ${COMP_CWORD})" -- ${CUR}` )
57 fi
58
59 # cases: `clean`
60 elif [ ${COMP_WORDS[1]} == "clean" ]
61 then
62 if (( ${COMP_CWORD} == 2 ))
63 then COMPREPLY=( `compgen -W "--test" -- ${CUR}` )
64 fi
65
66 # cases: `inspect` or `watch`
67 elif [ ${COMP_WORDS[1]} == "inspect" ] || [ ${COMP_WORDS[1]} == "watch" ]
68 then
69 if (( ${COMP_CWORD} == 2 ))
70 then COMPREPLY=( `compgen -W "$(all_services)" -- ${CUR}` )
71 fi
72
73 # cases: `logs` or `restart` or `up`
74 elif [ ${COMP_WORDS[1]} == "logs" ] || [ ${COMP_WORDS[1]} == "restart" ] || [ ${COMP_WORDS[1]} == "up" ]
75 then
76 if (( ${COMP_CWORD} == 2 ))
77 then COMPREPLY=( `compgen -W "$(all_services)" -- ${CUR}` )
78 else COMPREPLY=( `compgen -W "$(available_services ${COMP_CWORD})" -- ${CUR}` )
79 fi
80
81 # cases: `test`
82 elif [ ${COMP_WORDS[1]} == "test" ]
83 then
84 if (( ${COMP_CWORD} == 2 ))
85 then COMPREPLY=( `compgen -W "--watch $(testable_services)" -- ${CUR}` )
86 elif (( ${COMP_CWORD} == 3 )) && [ ${COMP_WORDS[2]} == "--watch" ]
87 then COMPREPLY=( `compgen -W "$(testable_services)" -- ${CUR}` )
88 fi
89
90 fi
91}
92
93complete -F _dx_completion dx
94
95cd - > /dev/null