Skip to content
This repository was archived by the owner on Sep 28, 2021. It is now read-only.

Commit 0cf4d88

Browse files
gabbaxxwennmo
authored andcommitted
Bash completion (#166)
* Add bash completion for commands and flags * Remove incorrectly checked-in yml * Limit more flags with file extension requirements * Cleanup * More cleanup * Improved suggestions and flag handling Completion now supports both the --param value and the --param=value formats when forwarding flags to corectl lookup calls Moved engine,app,ttl,headers,config flags to top most command level to work better with the autocomplete feature * Minor changes to documentation * Minor changes to documentation
1 parent fecb139 commit 0cf4d88

56 files changed

Lines changed: 621 additions & 298 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎LICENSE‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
THE SOFTWARE.
22+
23+
cmd/utils.go is based on https://github.com/kubernetes/kubernetes/blob/e2c1f435516085ef17f222fb7f89cd3ba13aa944/pkg/kubectl/cmd/completion/completion.go, licensed under the Apache License, Version 2.0.

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ Also check out the blog post about utilizing `corectl` and `catwalk` to build yo
7373

7474
Usage documentation can be found [here](./docs/corectl.md).
7575

76+
`corectl` provides auto completion of commands and flags for `bash` and `zsh`. To load completion in your shell add the following to your `~/.bashrc` or `~/.zshrc` file depending on shell.
77+
78+
`. <(corectl completion bash)` or `. <(corectl completion zsh)`
79+
80+
Auto completion requires `jq` and `bash-completion` to be installed.
81+
7682
## Testing
7783

7884
The unit tests are run with the go test command:

‎cmd/get.go‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ var getCmd = &cobra.Command{
1515
Use: "get",
1616
Short: "Lists one or several resources",
1717
Long: "Lists one or several resources",
18-
1918
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
2019
rootCmd.PersistentPreRun(rootCmd, args)
21-
viper.BindPFlag("engine", ccmd.PersistentFlags().Lookup("engine"))
22-
viper.BindPFlag("ttl", ccmd.PersistentFlags().Lookup("ttl"))
2320
},
2421
}
2522

@@ -55,7 +52,6 @@ corectl get associations`,
5552

5653
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
5754
getCmd.PersistentPreRun(getCmd, args)
58-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
5955
},
6056

6157
Run: func(ccmd *cobra.Command, args []string) {
@@ -74,7 +70,6 @@ corectl get connections --json`,
7470

7571
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
7672
getCmd.PersistentPreRun(getCmd, args)
77-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
7873
viper.BindPFlag("json", ccmd.PersistentFlags().Lookup("json"))
7974
},
8075

@@ -96,7 +91,6 @@ var getConnectionCmd = &cobra.Command{
9691

9792
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
9893
getCmd.PersistentPreRun(getCmd, args)
99-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
10094
},
10195

10296
Run: func(ccmd *cobra.Command, args []string) {
@@ -122,7 +116,6 @@ var getDimensionsCmd = &cobra.Command{
122116

123117
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
124118
getCmd.PersistentPreRun(getCmd, args)
125-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
126119
viper.BindPFlag("json", ccmd.PersistentFlags().Lookup("json"))
127120
},
128121

@@ -139,7 +132,6 @@ var getDimensionCmd = &cobra.Command{
139132

140133
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
141134
getCmd.PersistentPreRun(getCmd, args)
142-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
143135
},
144136

145137
//if no specific subcommand is used show the Dimensions properties
@@ -186,7 +178,6 @@ var getFieldCmd = &cobra.Command{
186178

187179
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
188180
getCmd.PersistentPreRun(getCmd, args)
189-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
190181
},
191182

192183
Run: func(ccmd *cobra.Command, args []string) {
@@ -208,7 +199,6 @@ var getFieldsCmd = &cobra.Command{
208199

209200
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
210201
getCmd.PersistentPreRun(getCmd, args)
211-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
212202
},
213203

214204
Run: func(ccmd *cobra.Command, args []string) {
@@ -226,7 +216,6 @@ var getKeysCmd = &cobra.Command{
226216

227217
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
228218
getCmd.PersistentPreRun(getCmd, args)
229-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
230219
},
231220

232221
Run: func(ccmd *cobra.Command, args []string) {
@@ -245,7 +234,6 @@ corectl get measures --json`,
245234

246235
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
247236
getCmd.PersistentPreRun(getCmd, args)
248-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
249237
viper.BindPFlag("json", ccmd.PersistentFlags().Lookup("json"))
250238
},
251239

@@ -262,7 +250,6 @@ var getMeasureCmd = &cobra.Command{
262250

263251
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
264252
getCmd.PersistentPreRun(getCmd, args)
265-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
266253
},
267254

268255
//if no specific subcommand is used show the Measure properties
@@ -312,7 +299,6 @@ corectl get meta --app my-app.qvf`,
312299

313300
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
314301
getCmd.PersistentPreRun(getCmd, args)
315-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
316302
},
317303

318304
Run: func(ccmd *cobra.Command, args []string) {
@@ -331,7 +317,6 @@ corectl get objects --json --app=myapp.qvf`,
331317

332318
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
333319
getCmd.PersistentPreRun(getCmd, args)
334-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
335320
viper.BindPFlag("json", ccmd.PersistentFlags().Lookup("json"))
336321
},
337322

@@ -348,7 +333,6 @@ var getObjectCmd = &cobra.Command{
348333

349334
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
350335
getCmd.PersistentPreRun(getCmd, args)
351-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
352336
},
353337

354338
//if no specific subcommand is used show the objects properties
@@ -423,7 +407,6 @@ corectl get script --app=my-app.qvf`,
423407

424408
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
425409
getCmd.PersistentPreRun(getCmd, args)
426-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
427410
},
428411

429412
Run: func(ccmd *cobra.Command, args []string) {
@@ -438,14 +421,13 @@ corectl get script --app=my-app.qvf`,
438421

439422
var getStatusCmd = &cobra.Command{
440423
Use: "status",
441-
Short: "Prints status info about the connection to engine and current app",
442-
Long: "Prints status info about the connection to engine and current app, and also the status of the data model",
424+
Short: "Prints status info about the connection to the engine and current app",
425+
Long: "Prints status info about the connection to the engine and current app, and also the status of the data model",
443426
Example: `corectl get status
444427
corectl get status --app=my-app.qvf`,
445428

446429
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
447430
getCmd.PersistentPreRun(getCmd, args)
448-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
449431
},
450432

451433
Run: func(ccmd *cobra.Command, args []string) {
@@ -463,7 +445,6 @@ corectl get tables --app=my-app.qvf`,
463445

464446
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
465447
getCmd.PersistentPreRun(getCmd, args)
466-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
467448
},
468449

469450
Run: func(ccmd *cobra.Command, args []string) {

‎cmd/remove.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ var removeCmd = &cobra.Command{
2020

2121
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
2222
rootCmd.PersistentPreRun(rootCmd, args)
23-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
24-
viper.BindPFlag("engine", ccmd.PersistentFlags().Lookup("engine"))
25-
viper.BindPFlag("ttl", ccmd.PersistentFlags().Lookup("ttl"))
26-
viper.BindPFlag("headers", ccmd.PersistentFlags().Lookup("headers"))
2723
viper.BindPFlag("suppress", ccmd.PersistentFlags().Lookup("suppress"))
2824
},
2925
}

‎cmd/root.go‎

Lines changed: 122 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ var rootCtx = context.Background()
2121

2222
// rootCmd represents the base command when called without any subcommands
2323
var rootCmd = &cobra.Command{
24-
Hidden: true,
25-
Use: "corectl",
26-
Short: "",
27-
Long: `Corectl contains various commands to interact with the Qlik Associative Engine. See respective command for more information`,
28-
DisableAutoGenTag: true,
24+
Hidden: true,
25+
Use: "corectl",
26+
Short: "",
27+
Long: `corectl contains various commands to interact with the Qlik Associative Engine. See respective command for more information`,
28+
DisableAutoGenTag: true,
29+
BashCompletionFunction: bashCompletionFunc,
2930

3031
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
3132
// if help, version or generate-docs command, no prerun is needed.
@@ -77,54 +78,56 @@ func Execute(mainVersion string) {
7778
}
7879

7980
func init() {
81+
rootCmd.PersistentFlags().StringVarP(&explicitConfigFile, "config", "c", "", "path/to/config.yml where parameters can be set instead of on the command line")
82+
// Set annotation to run bash completion function for the config flag and only show .yaml or .yml files
83+
rootCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, []string{"yaml", "yml"})
84+
8085
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Logs extra information")
8186
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
8287

8388
rootCmd.PersistentFlags().BoolP("traffic", "t", false, "Log JSON websocket traffic to stdout")
8489
viper.BindPFlag("traffic", rootCmd.PersistentFlags().Lookup("traffic"))
8590

86-
//Is it nicer to have one loop per argument or group the commands together if they all are used in the same commands?
87-
for _, command := range []*cobra.Command{buildCmd, catwalkCmd, evalCmd, getCmd, reloadCmd, removeCmd, setCmd} {
88-
command.PersistentFlags().StringVarP(&explicitConfigFile, "config", "c", "", "path/to/config.yml where parameters can be set instead of on the command line")
89-
}
90-
91-
//since several commands are using the same flag, the viper binding has to be done in the commands prerun function, otherwise they overwrite.
91+
rootCmd.PersistentFlags().StringP("engine", "e", "", "URL to the Qlik Associative Engine (default \"localhost:9076\")")
92+
viper.BindPFlag("engine", rootCmd.PersistentFlags().Lookup("engine"))
9293

93-
for _, command := range []*cobra.Command{buildCmd, catwalkCmd, evalCmd, getCmd, reloadCmd, removeCmd, setCmd} {
94-
command.PersistentFlags().StringP("engine", "e", "", "URL to engine (default \"localhost:9076\")")
95-
}
96-
97-
for _, command := range []*cobra.Command{buildCmd, evalCmd, getCmd, reloadCmd, removeCmd, setCmd} {
98-
command.PersistentFlags().String("ttl", "30", "Engine session time to live in seconds")
99-
}
94+
rootCmd.PersistentFlags().String("ttl", "30", "Qlik Associative Engine session time to live in seconds")
95+
viper.BindPFlag("ttl", rootCmd.PersistentFlags().Lookup("ttl"))
10096

101-
for _, command := range []*cobra.Command{buildCmd, evalCmd, getCmd, reloadCmd, setCmd, removeCmd} {
102-
//not binding to viper since binding a map does not seem to work.
103-
command.PersistentFlags().StringToStringVar(&headersMap, "headers", nil, "Headers to use when connecting to qix engine")
104-
}
97+
//not binding to viper since binding a map does not seem to work.
98+
rootCmd.PersistentFlags().StringToStringVar(&headersMap, "headers", nil, "Http headers to use when connecting to Qlik Associative Engine")
10599

106-
for _, command := range []*cobra.Command{buildCmd, catwalkCmd, evalCmd, getAssociationsCmd, getConnectionsCmd, getConnectionCmd, getDimensionsCmd, getDimensionCmd, getFieldsCmd, getKeysCmd, getFieldCmd, getMeasuresCmd, getMeasureCmd, getMetaCmd, getObjectsCmd, getObjectCmd, getScriptCmd, getStatusCmd, getTablesCmd, reloadCmd, removeCmd, setCmd} {
107-
command.PersistentFlags().StringP("app", "a", "", "App name, if no app is specified a session app is used instead.")
108-
}
100+
rootCmd.PersistentFlags().StringP("app", "a", "", "App name, if no app is specified a session app is used instead.")
101+
viper.BindPFlag("app", rootCmd.PersistentFlags().Lookup("app"))
102+
// Set annotation to run bash completion function for the app flag
103+
rootCmd.PersistentFlags().SetAnnotation("app", cobra.BashCompCustom, []string{"__corectl_get_apps"})
109104

110105
for _, command := range []*cobra.Command{buildCmd, setAllCmd, setConnectionsCmd} {
111106
// Don't bind these to viper since paths are treated separately to support relative paths!
112107
command.PersistentFlags().String("connections", "", "path/to/connections.yml that contains connections that are used in the reload. Note that when specifying connections in the config file they are specified inline, not as a file reference!")
108+
// Set annotation to run bash completion function for the connections flag and only show .yml or .yaml files
109+
command.PersistentFlags().SetAnnotation("connections", cobra.BashCompFilenameExt, []string{"yml", "yaml"})
113110
}
114111

115112
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
116113
// Don't bind these to viper since paths are treated separately to support relative paths!
117114
command.PersistentFlags().String("dimensions", "", "A list of generic dimension json paths")
115+
// Set annotation to run bash completion function for the dimensions flag and only show .json files
116+
command.PersistentFlags().SetAnnotation("dimensions", cobra.BashCompFilenameExt, []string{"json"})
118117
}
119118

120119
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
121120
// Don't bind these to viper since paths are treated separately to support relative paths!
122121
command.PersistentFlags().String("measures", "", "A list of generic measures json paths")
122+
// Set annotation to run bash completion function for the measures flag and only show .json files
123+
command.PersistentFlags().SetAnnotation("measures", cobra.BashCompFilenameExt, []string{"json"})
123124
}
124125

125126
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
126127
// Don't bind these to viper since paths are treated separately to support relative paths!
127128
command.PersistentFlags().String("objects", "", "A list of generic object json paths")
129+
// Set annotation to run bash completion function for the objects flag and only show .json files
130+
command.PersistentFlags().SetAnnotation("objects", cobra.BashCompFilenameExt, []string{"json"})
128131
}
129132

130133
for _, command := range []*cobra.Command{buildCmd, reloadCmd} {
@@ -138,6 +141,8 @@ func init() {
138141
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
139142
// Don't bind these to viper since paths are treated separately to support relative paths!
140143
command.PersistentFlags().String("script", "", "path/to/reload-script.qvs that contains a qlik reload script. If omitted the last specified reload script for the current app is reloaded")
144+
// Set annotation to run bash completion function for the script flag and only show .qvs files
145+
command.PersistentFlags().SetAnnotation("script", cobra.BashCompFilenameExt, []string{"qvs"})
141146
}
142147

143148
for _, command := range []*cobra.Command{getAppsCmd, getConnectionsCmd, getDimensionsCmd, getMeasuresCmd, getObjectsCmd} {
@@ -189,3 +194,95 @@ func getEntities(ccmd *cobra.Command, args []string, entityType string, printAsJ
189194
}
190195
printer.PrintGenericEntities(allInfos, entityType, printAsJSON)
191196
}
197+
198+
const bashCompletionFunc = `
199+
200+
__custom_func()
201+
{
202+
case ${last_command} in
203+
corectl_get_dimension_properties | corectl_get_dimension_layout)
204+
__corectl_get_dimensions
205+
;;
206+
corectl_get_measure_properties | corectl_get_measure_layout)
207+
__corectl_get_measures
208+
;;
209+
corectl_get_object_data | corectl_get_object_properties | corectl_get_object_layout)
210+
__corectl_get_objects
211+
;;
212+
corectl_get_connection)
213+
__corectl_get_connections
214+
;;
215+
*)
216+
COMPREPLY+=( $( compgen -W "" -- "$cur" ) )
217+
;;
218+
esac
219+
}
220+
221+
__extract_flags_to_forward()
222+
{
223+
local forward_flags
224+
local result
225+
forward_flags=( "--engine" "-e" "--app" "-a" "--config" "-c" "--headers" "--ttl" );
226+
while [[ $# -gt 0 ]]; do
227+
for i in "${forward_flags[@]}"
228+
do
229+
case $1 in
230+
$i)
231+
result+="$1=";
232+
shift;
233+
result+="$1 "
234+
;;
235+
$i=*)
236+
result+="$1 "
237+
;;
238+
esac
239+
done
240+
shift
241+
done
242+
echo "$result";
243+
}
244+
245+
__corect_render_compreply()
246+
{
247+
if [[ $? -eq 0 ]]; then
248+
COMPREPLY+=( $( compgen -W "$1" -- "$cur" ) )
249+
else
250+
COMPREPLY+=( $( compgen -W "" -- "$cur" ) )
251+
fi
252+
}
253+
254+
__corectl_get_dimensions()
255+
{
256+
local flags=$(__extract_flags_to_forward ${words[@]})
257+
local corectl_out=$(corectl get dimensions --json $flags | jq '.[] | .qId' 2>/dev/null)
258+
__corect_render_compreply "${corectl_out[*]}"
259+
}
260+
261+
__corectl_get_measures()
262+
{
263+
local flags=$(__extract_flags_to_forward ${words[@]})
264+
local corectl_out=$(corectl get measures --json $flags | jq '.[] | .qId' 2>/dev/null)
265+
__corect_render_compreply "${corectl_out[*]}"
266+
}
267+
268+
__corectl_get_objects()
269+
{
270+
local flags=$(__extract_flags_to_forward ${words[@]})
271+
local corectl_out=$(corectl get objects --json $flags | jq '.[] | .qId' 2>/dev/null)
272+
__corect_render_compreply "${corectl_out[*]}"
273+
}
274+
275+
__corectl_get_connections()
276+
{
277+
local flags=$(__extract_flags_to_forward ${words[@]})
278+
local corectl_out=$(corectl get connections --json $flags | jq '.[] | .qId' 2>/dev/null)
279+
__corect_render_compreply "${corectl_out[*]}"
280+
}
281+
282+
__corectl_get_apps()
283+
{
284+
local config=$(__extract_flags_to_forward ${words[@]})
285+
local corectl_out=$(corectl get apps --json $config | jq '.[] | .name' 2>/dev/null)
286+
__corect_render_compreply "${corectl_out[*]}"
287+
}
288+
`

‎cmd/set.go‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ var setCmd = &cobra.Command{
1717

1818
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
1919
rootCmd.PersistentPreRun(rootCmd, args)
20-
viper.BindPFlag("app", ccmd.PersistentFlags().Lookup("app"))
21-
viper.BindPFlag("engine", ccmd.PersistentFlags().Lookup("engine"))
2220
viper.BindPFlag("no-save", ccmd.PersistentFlags().Lookup("no-save"))
23-
viper.BindPFlag("ttl", ccmd.PersistentFlags().Lookup("ttl"))
2421
},
2522
}
2623

0 commit comments

Comments
 (0)
⌂ 换网址