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

Commit 1e33a3c

Browse files
gabbaxxwennmo
authored andcommitted
Completion on bash (#184)
1 parent dfe97f6 commit 1e33a3c

2 files changed

Lines changed: 49 additions & 35 deletions

File tree

‎cmd/root.go‎

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"os"
8+
"runtime"
89
"strings"
910

1011
"github.com/qlik-oss/corectl/internal"
@@ -79,8 +80,6 @@ func Execute(mainVersion string) {
7980

8081
func init() {
8182
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"})
8483

8584
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Logs extra information")
8685
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
@@ -103,35 +102,27 @@ func init() {
103102

104103
rootCmd.PersistentFlags().StringP("app", "a", "", "App name, if no app is specified a session app is used instead.")
105104
viper.BindPFlag("app", rootCmd.PersistentFlags().Lookup("app"))
106-
// Set annotation to run bash completion function for the app flag
105+
// Set annotation to run bash completion function
107106
rootCmd.PersistentFlags().SetAnnotation("app", cobra.BashCompCustom, []string{"__corectl_get_apps"})
108107

109108
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
110109
// Don't bind these to viper since paths are treated separately to support relative paths!
111110
command.PersistentFlags().String("connections", "", "Path to a yml file containing the data connection definitions")
112-
// Set annotation to run bash completion function for the connections flag and only show .yml or .yaml files
113-
command.PersistentFlags().SetAnnotation("connections", cobra.BashCompFilenameExt, []string{"yml", "yaml"})
114111
}
115112

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

123118
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
124119
// Don't bind these to viper since paths are treated separately to support relative paths!
125120
command.PersistentFlags().String("measures", "", "A list of generic measures json paths")
126-
// Set annotation to run bash completion function for the measures flag and only show .json files
127-
command.PersistentFlags().SetAnnotation("measures", cobra.BashCompFilenameExt, []string{"json"})
128121
}
129122

130123
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
131124
// Don't bind these to viper since paths are treated separately to support relative paths!
132125
command.PersistentFlags().String("objects", "", "A list of generic object json paths")
133-
// Set annotation to run bash completion function for the objects flag and only show .json files
134-
command.PersistentFlags().SetAnnotation("objects", cobra.BashCompFilenameExt, []string{"json"})
135126
}
136127

137128
for _, command := range []*cobra.Command{buildCmd, reloadCmd} {
@@ -145,8 +136,6 @@ func init() {
145136
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
146137
// Don't bind these to viper since paths are treated separately to support relative paths!
147138
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")
148-
// Set annotation to run bash completion function for the script flag and only show .qvs files
149-
command.PersistentFlags().SetAnnotation("script", cobra.BashCompFilenameExt, []string{"qvs"})
150139
}
151140

152141
for _, command := range []*cobra.Command{getAppsCmd, getConnectionsCmd, getDimensionsCmd, getMeasuresCmd, getObjectsCmd} {
@@ -156,8 +145,13 @@ func init() {
156145
for _, command := range []*cobra.Command{removeCmd} {
157146
command.PersistentFlags().Bool("suppress", false, "Suppress all confirmation dialogues")
158147
}
159-
160148
catwalkCmd.PersistentFlags().String("catwalk-url", "https://catwalk.core.qlik.com", "Url to an instance of catwalk, if not provided the qlik one will be used.")
149+
150+
if runtime.GOOS != "windows" {
151+
// Do not add bash completion annotations for paths and files as they are not compatible with windows. On windows
152+
// we instead rely on the default bash behavior
153+
addFileRelatedBashAnnotations()
154+
}
161155
}
162156

163157
// GetRelativeParameter returns a parameter from the config file.
@@ -249,47 +243,43 @@ const bashCompletionFunc = `
249243
echo "$result";
250244
}
251245
252-
__corectl_render_compreply()
246+
__corectl_call_corectl()
253247
{
254-
if [[ $? -eq 0 ]]; then
255-
COMPREPLY+=( "${1[@]}" )
256-
else
257-
COMPREPLY+=( $( compgen -W "" -- "$cur" ) )
258-
fi
248+
local flags=$(__extract_flags_to_forward ${words[@]})
249+
local corectl_out
250+
local errorcode
251+
corectl_out=$(corectl $1 $flags 2>/dev/null)
252+
errorcode=$?
253+
if [[ errorcode -eq 0 ]]; then
254+
local IFS=$'\n'
255+
COMPREPLY=( $(compgen -W "${corectl_out}" -- "$cur") )
256+
else
257+
COMPREPLY=()
258+
fi;
259259
}
260260
261261
__corectl_get_dimensions()
262262
{
263-
local flags=$(__extract_flags_to_forward ${words[@]})
264-
local corectl_out=$(corectl get dimensions --bash $flags 2>/dev/null)
265-
__corectl_render_compreply "${corectl_out[*]}"
263+
__corectl_call_corectl "get dimensions --bash"
266264
}
267265
268266
__corectl_get_measures()
269267
{
270-
local flags=$(__extract_flags_to_forward ${words[@]})
271-
local corectl_out=$(corectl get measures --bash $flags 2>/dev/null)
272-
__corectl_render_compreply "${corectl_out[*]}"
268+
__corectl_call_corectl "get measures --bash"
273269
}
274270
275271
__corectl_get_objects()
276272
{
277-
local flags=$(__extract_flags_to_forward ${words[@]})
278-
local corectl_out=$(corectl get objects --bash $flags 2>/dev/null)
279-
__corectl_render_compreply "${corectl_out[*]}"
273+
__corectl_call_corectl "get objects --bash"
280274
}
281275
282276
__corectl_get_connections()
283277
{
284-
local flags=$(__extract_flags_to_forward ${words[@]})
285-
local corectl_out=$(corectl get connections --bash $flags 2>/dev/null)
286-
__corectl_render_compreply "${corectl_out[*]}"
278+
__corectl_call_corectl "get connections --bash"
287279
}
288280
289281
__corectl_get_apps()
290282
{
291-
local config=$(__extract_flags_to_forward ${words[@]})
292-
local corectl_out=$(corectl get apps --bash $config 2>/dev/null)
293-
__corectl_render_compreply "${corectl_out[*]}"
283+
__corectl_call_corectl "get apps --bash"
294284
}
295285
`

‎cmd/utils.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ Note that bash-completion is required and needs to be installed on your system.`
157157
},
158158
}
159159

160+
// Set annotation to run bash completion function for files (not compatible git bash)
161+
func addFileRelatedBashAnnotations() {
162+
rootCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, []string{"yaml", "yml"})
163+
164+
for _, command := range []*cobra.Command{buildCmd, setAllCmd, setConnectionsCmd} {
165+
command.PersistentFlags().SetAnnotation("connections", cobra.BashCompFilenameExt, []string{"yml", "yaml"})
166+
}
167+
168+
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
169+
command.PersistentFlags().SetAnnotation("dimensions", cobra.BashCompFilenameExt, []string{"json"})
170+
}
171+
172+
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
173+
command.PersistentFlags().SetAnnotation("measures", cobra.BashCompFilenameExt, []string{"json"})
174+
}
175+
176+
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
177+
command.PersistentFlags().SetAnnotation("objects", cobra.BashCompFilenameExt, []string{"json"})
178+
}
179+
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
180+
command.PersistentFlags().SetAnnotation("script", cobra.BashCompFilenameExt, []string{"qvs"})
181+
}
182+
}
183+
160184
func init() {
161185
rootCmd.AddCommand(buildCmd)
162186
rootCmd.AddCommand(catwalkCmd)

0 commit comments

Comments
 (0)
⌂ 换网址