-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_golem
151 lines (118 loc) · 5.19 KB
/
gc_golem
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
# gc_golem
# tag: main 2021-10-09T10:48:41-07:00
# golem completion engine
# author: krunch3r76 (https://github.com/krunch3r76)
# license: poetic/GPL 3
#
# install to $HOME/.local/share/bash-completion/completions/golem
if [[ -v SUDO_USER ]]; then
return # assume completion engines not used in sudo environment
fi
YAGNA_COMPLETION_ENGINE=$(yagna complete bash 2>&1) # store the completion engine script produced by yagna
if [[ $? != 0 ]]; then
echo "[[gc__enhanced_completion]] ERROR: could not load yagna's completion engine"
return
fi
source /dev/stdin <<<$YAGNA_COMPLETION_ENGINE # add yagna official completion engine
if [[ $? != 0 ]]; then
echo "[[gc__enhanced_completion]] ERROR: could not load parse's completion engine"
return
fi
# remove completion hooks to ensure overrides defined in this source
complete -r -o bashdefault -o default -F _yagna yagna 2>/dev/null # clear before reload of yagna official script
# check for internal support of golemsp completions
GOLEMSP_COMPLETION_ENGINE_FULL=$(golemsp complete bash 2>&1)
if [[ $? == 0 ]]; then # command to get the completions ran successfully
GOLEMSP_COMPLETION_ENGINE=$(echo "$GOLEMSP_COMPLETION_ENGINE_FULL")
source /dev/stdin <<<$GOLEMSP_COMPLETION_ENGINE
# remove completion hooks to ensure overrides defined in this source
complete -r -o bashdefault -o default -F _golemsp golemsp 2>/dev/null # clear before reload of golemsp official script
[[ $(declare -f _golemsp) ]] && GOLEMSP_SUPPORTED=1 # the expected completion function has been defined
fi
if [[ ! $GOLEMSP_SUPPORTED ]]; then
echo "[[gc__enhanced_completion]] WARN: could not parse golemsp's completion engine, manually added"
gc__subcommands=(golemsp yagna run stop settings set show) # golemsp subcommands manually added
fi
# INPUT: gc__cwords {global}, COMP_LINE {imported env}
# OUTPUT: produce output of the command line proceeded with -h
# POST: N/A
_gc__context_help() {
# DOCSTRING: gets corresponding output of command on the line from postfixing -h for help
echo -e "\n-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
if [[ ! $1 == @(-h|help) ]]; then
$COMP_LINE -h
else
$COMP_LINE
fi
echo -n "${PS1@P}$COMP_LINE"
}
# INPUT: last entered subcommand, second to last entered subcommand
# OUTPUT: none
# POST: COMPREPLY {global} set with possible completions
_gc__completion_golemsp() {
# DOCSTRING: tab complete current word for command name golemsp
local ULTIMATE=$1
local PENULTIMATE=$2
local options
if [[ "$ULTIMATE" == "golemsp" ]]; then
options=(run stop settings status help)
elif [[ "$ULTIMATE" == "run" ]]; then
options=(--subnet --account --payment-network --log-dir --debug)
elif [[ "$ULTIMATE" == "settings" ]]; then
options=(set show)
elif [[ "$ULTIMATE" == "set" && $PENULTIMATE == "settings" ]]; then
options=(--node-name --cores --memory --disk --starting-fee --env-per-hour --cpu-per-hour --acount --payment-network)
fi
COMPREPLY=($(compgen -W "${options[*]}" \"${COMP_WORDS[$COMP_CWORD]}\")) # generate and use possible expansions in options from the word under the cursor
}
# INPUT: gc__cwords {global}
# OUTPUT: space delimited tuple of size 1 or 2 containing ULTIMATE and if found PENULTIMATE
# POST: none
_gc__completion__rfind_subcommands() {
# DOCSTRING: reverse find first and optionally second subcommand in the gc__cwords on the command line before cword
gc__subcommands=${gc__subcommands:-(golemsp yagna)} # kludge for _gc__completion__rfind_subcommands upon invocation
local RINDEX
# reverse move index to next subcommand, then store in ULTIMATE
for (( RINDEX=$(( COMP_CWORD - 1 )); RINDEX > -1; RINDEX-- )); do
if [[ "${gc__subcommands[*]}" =~ (^| )${gc__cwords[$RINDEX]}($| ) ]]; then
local ULTIMATE=${gc__cwords[$RINDEX]}
break # on first subcommand seen
fi
done
# reverse move index to next subcommand if preset, then store in PENULTIMATE
for (( RINDEX=$(( RINDEX - 1 )); RINDEX > -1; RINDEX-- )); do
if [[ "${gc__subcommands[*]}" =~ (^| )${gc__cwords[$RINDEX]}($| ) ]]; then
local PENULTIMATE=${gc__cwords[$RINDEX]}
break # break on first subcommand seen
fi
done
echo $ULTIMATE ${PENULTIMATE:+$PENULTIMATE}
}
# IN: complete's builtin exported variables
# OUT: none
# PRE: none
# POST: display updated with completion suggestions or contextual help for command as entered
# comments: $1 -> root command, $2 -> word being completed, $3-> word before word being completed
_gc__completion() {
# DOCSTRING: handle golemsp, yagna specific logic
# notes: ULTIMATE refers to the last occurring subcommand, PENULTIMATE is the preceding subcommand/command if present
gc__cwords=("${COMP_WORDS[@]}") # global copy of the list of words in the current command line
if [[ ! $2 ]]; then # cursor over empty space so provide context help
_gc__context_help $3
else # cursor in word, so provide possible completions
local ULTIMATE PENULTIMATE
local ULTIMATES=$(_gc__completion__rfind_subcommands)
ULTIMATE=${ULTIMATES[0]}
if [[ ${#ULTIMATES[*]} > 1 ]]; then PENULTIMATE=${ULTIMATES[1]}; fi
if [[ "$1" == "golemsp" ]]; then
if [[ $GOLEMSP_SUPPORTED ]]; then
_golemsp
else
_gc__completion_golemsp $ULTIMATE ${PENULTIMATE:+$PENULTIMATE}
fi
elif [[ "$1" == "yagna" ]]; then
_yagna
fi
fi
} &&
complete -F _gc__completion golemsp yagna