-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect
255 lines (213 loc) · 8.07 KB
/
detect
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
#!/bin/bash
#-----------------------------------------------------------------------------
# Bash completions for Synopsys (Black Duck) Detect commands.
# See https://detect.synopsys.com/doc
#-----------------------------------------------------------------------------
_complete_option_from_help()
{
# Completes a word with command-line options found in the
# documentation for Detect. Since it takes a while for Detect
# to start up, the options are first extracted from the
# documentation and cached in a text file for faster access.
# TODO: Honor variables used by the detect.sh bootstrapper, so
# that we use the same java, java options, jar file, etc.
# TODO: Need better exception handling here. Check that `java`
# and `awk` are available; check `bash` version is compatible.
local word=$1
local detect_jars=($(ls -t $HOME/synopsys-detect/download/synopsys-detect*.jar /tmp/synopsys-detect*.jar 2> /dev/null))
local latest_detect_jar=${detect_jars[0]}
local latest_detect_options=${latest_detect_jar}.options
if [ ! ${latest_detect_jar} ]; then
printf "\nCompletions are not available until Detect has been bootstrapped.\n"
return 0
fi
if [ $latest_detect_jar -nt $latest_detect_options ]; then
printf "\nOne moment while we cache completions for %s..." `basename $latest_detect_jar`
java -jar $latest_detect_jar -hv | sed -n 's/^\(--[[:alnum:].]*\).*$/\1/p' > $latest_detect_options
printf "done.\n"
fi
COMPREPLY=($(compgen -S = -W "$(cat $latest_detect_options)" -- $word))
return 0
}
#-----------------------------------------------------------------------------
_complete_option_param_list()
{
# Completes a word as a comma-delimited list of parameters. If the
# word ends in a comma, then additional parameters are suggested
# for the list. Parameters that have already been specified are
# excluded from the suggestions. A space will not be
# automatiacally appended when a word is completed.
local completions=$1
local word=$2
if [[ "$word" == *,* ]]; then
local curparam params
curparam=${word##*,}
params=${word%,*}
# params cannot be repeated, so
# delete params we have already
# entered from the completions
for del in ${params//,/ }; do
completions=${completions/"$del"/}
done
if [[ $params =~ '^(ALL|NONE)\b' ]]; then
# Some param lists can have a value of ALL or NONE.
# In either case, all other completions become unnecessary.
completions=""
else
# And if the param list does *not* contain ALL or NONE,
# then ALL and NONE become unnecessary completions.
completions=${completions/ALL/}
completions=${completions/NONE/}
fi
COMPREPLY=($(compgen -W "$completions" -P "${params}," -- $curparam))
else
COMPREPLY=($(compgen -W "$completions" -- $word))
fi
}
#-----------------------------------------------------------------------------
_complete_option_param()
{
# Completes a word as a single parameter. Once the parameter is
# complete, a space is automatically appended to move the cursor
# to the next word.
local completions=$1
local word=$2
compopt +o nospace
COMPREPLY=($(compgen -W "$completions" -- $word))
}
#-----------------------------------------------------------------------------
_detect_completion()
{
# Completion function for the `detect` command. All arguments have
# the form "--option=value". If the value is supposed to be a list
# of comma-separated words, then those words will also be
# completed. If the value is supposed to be true|false, or a path
# to a file or directory, then those values will be completed too.
_detect_log_levels="\
TRACE\
DEBUG\
INFO\
WARN\
ERROR\
FATAL\
OFF"
_detect_project_tools="\
DETECTOR\
DOCKER\
BAZEL"
_detect_tools="\
BAZEL\
DETECTOR\
DOCKER\
SIGNATURE_SCAN\
BINARY_SCAN\
POLARIS\
NONE\
ALL"
_detect_severities="\
ALL\
BLOCKER\
CRITICAL\
MAJOR\
MINOR\
TRIVIAL\
UNSPECIFIED"
_detect_clone_categories="\
COMPONENT_DATA\
VULN_DATA"
_detect_distributions="\
EXTERNAL\
SAAS\
INTERNAL\
OPENSOURCE"
_detect_phases="\
ARCHIVED\
DEPRECATED\
DEVELOPMENT\
PLANNING\
PRERELEASE\
RELEASED"
_detect_snippet_matching="\
SNIPPET_MATCHING\
SNIPPET_MATCHING_ONLY\
FULL_SNIPPET_MATCHING\
FULL_SNIPPET_MATCHING_ONLY\
NONE"
# The command will be parsed *without* treating "=" as a word
# boundary. This means cur will always have a trailing "=" when it
# is completed, so all arguments evetually end up in the form "--
# option=param"
local cur prev words cword
_init_completion -n = || return
# If the current word contains an "=" then split it and set the
# previous word to the option name and the current word into the
# option parameter. That allows us to complete the option
# parameter separately.
if [[ "$cur" == *"="* ]] ; then
temp=(${cur//=/ })
prev=${temp[0]}
cur=${temp[1]}
fi
case $prev in
*path | *tar)
_filedir
;;
--blackduck.trust.cert | --blackduck.offline.mode | --detect.disable.without.blackduck | --detect.test.connection)
_complete_option_param "true false" $cur
;;
*.mode | *.continue | *.exclusion.defaults)
_complete_option_param "true false" $cur
;;
--detect.cleanup | --detect.detector.buildless | --detect.force.success | --detect.ignore.connection.failures | --detect.wait.for.results)
_complete_option_param "true false" $cur
;;
--detect.suppress.configuration.output | --detect.suppress.results.output)
_complete_option_param "true false" $cur
;;
--detect.npm.include.dev.dependencies | --detect.nuget.ignore.failure | --detect.packagist.include.dev.dependencies)
_complete_option_param "$_detect_log_levels" $cur
;;
--logging.level.com.blackducksoftware.integration)
_complete_option_param "$_detect_log_levels" $cur
;;
--logging.level.com.synopsys.integration)
_complete_option_param "$_detect_log_levels" $cur
;;
--detect.tools)
_complete_option_param_list "$_detect_tools" $cur
;;
--detect.tools.excluded)
_complete_option_param_list "$_detect_tools" $cur
;;
--detect.project.tool)
_complete_option_param_list "$_detect_project_tools" $cur
;;
--detect.policy.check.fail.on.severities)
_complete_option_param_list "$_detect_severities" $cur
;;
--detect.project.clone.categories)
_complete_option_param_list "$_detect_clone_categories" $cur
;;
--detect.project.tier)
compopt +o nospace
COMPREPLY=($(compgen -W "1 2 3 4 5" -- $cur))
;;
--detect.project.version.distribution)
_complete_option_param "$_detect_distributions" $cur
;;
--detect.project.version.phase)
_complete_option_param "$_detect_phases" $cur
;;
--detect.blackduck.signature.scanner.snippet.matching)
_complete_option_param "$_detect_snippet_matching" $cur
;;
*)
_complete_option_from_help $cur
esac
return 0
}
#-----------------------------------------------------------------------------
# Now bind the completion function to a command named `detect` By
# default, we do not automatically append spaces to a finished
# completion so that we can use "=" as the separator instead.
complete -o nospace -F _detect_completion detect