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

Completion on bash #184

Merged
merged 6 commits into from Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 25 additions & 35 deletions cmd/root.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"runtime"
"strings"

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

func init() {
rootCmd.PersistentFlags().StringVarP(&explicitConfigFile, "config", "c", "", "path/to/config.yml where parameters can be set instead of on the command line")
// Set annotation to run bash completion function for the config flag and only show .yaml or .yml files
rootCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, []string{"yaml", "yml"})

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

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

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

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

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

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

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

for _, command := range []*cobra.Command{getAppsCmd, getConnectionsCmd, getDimensionsCmd, getMeasuresCmd, getObjectsCmd} {
Expand All @@ -156,8 +145,13 @@ func init() {
for _, command := range []*cobra.Command{removeCmd} {
command.PersistentFlags().Bool("suppress", false, "Suppress all confirmation dialogues")
}

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.")

if runtime.GOOS != "windows" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment to why this is not applicable for windows

// Do not add bash completion annotations for paths and files as they are not compatible with windows. On windows
// we instead rely on the default bash behavior
addFileRelatedBashAnnotations()
}
}

// GetRelativeParameter returns a parameter from the config file.
Expand Down Expand Up @@ -249,47 +243,43 @@ const bashCompletionFunc = `
echo "$result";
}

__corectl_render_compreply()
__corectl_call_corectl()
{
if [[ $? -eq 0 ]]; then
COMPREPLY+=( "${1[@]}" )
else
COMPREPLY+=( $( compgen -W "" -- "$cur" ) )
fi
local flags=$(__extract_flags_to_forward ${words[@]})
local corectl_out
local errorcode
corectl_out=$(corectl $1 $flags 2>/dev/null)
errorcode=$?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to a common function for all commands?

if [[ errorcode -eq 0 ]]; then
local IFS=$'\n'
COMPREPLY=( $(compgen -W "${corectl_out}" -- "$cur") )
else
COMPREPLY=()
fi;
}

__corectl_get_dimensions()
{
local flags=$(__extract_flags_to_forward ${words[@]})
local corectl_out=$(corectl get dimensions --bash $flags 2>/dev/null)
__corectl_render_compreply "${corectl_out[*]}"
__corectl_call_corectl "get dimensions --bash"
}

__corectl_get_measures()
{
local flags=$(__extract_flags_to_forward ${words[@]})
local corectl_out=$(corectl get measures --bash $flags 2>/dev/null)
__corectl_render_compreply "${corectl_out[*]}"
__corectl_call_corectl "get measures --bash"
}

__corectl_get_objects()
{
local flags=$(__extract_flags_to_forward ${words[@]})
local corectl_out=$(corectl get objects --bash $flags 2>/dev/null)
__corectl_render_compreply "${corectl_out[*]}"
__corectl_call_corectl "get objects --bash"
}

__corectl_get_connections()
{
local flags=$(__extract_flags_to_forward ${words[@]})
local corectl_out=$(corectl get connections --bash $flags 2>/dev/null)
__corectl_render_compreply "${corectl_out[*]}"
__corectl_call_corectl "get connections --bash"
}

__corectl_get_apps()
{
local config=$(__extract_flags_to_forward ${words[@]})
local corectl_out=$(corectl get apps --bash $config 2>/dev/null)
__corectl_render_compreply "${corectl_out[*]}"
__corectl_call_corectl "get apps --bash"
}
`
24 changes: 24 additions & 0 deletions cmd/utils.go
Expand Up @@ -157,6 +157,30 @@ Note that bash-completion is required and needs to be installed on your system.`
},
}

// Set annotation to run bash completion function for files (not compatible git bash)
func addFileRelatedBashAnnotations() {
rootCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, []string{"yaml", "yml"})

for _, command := range []*cobra.Command{buildCmd, setAllCmd, setConnectionsCmd} {
command.PersistentFlags().SetAnnotation("connections", cobra.BashCompFilenameExt, []string{"yml", "yaml"})
}

for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
command.PersistentFlags().SetAnnotation("dimensions", cobra.BashCompFilenameExt, []string{"json"})
}

for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
command.PersistentFlags().SetAnnotation("measures", cobra.BashCompFilenameExt, []string{"json"})
}

for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
command.PersistentFlags().SetAnnotation("objects", cobra.BashCompFilenameExt, []string{"json"})
}
for _, command := range []*cobra.Command{buildCmd, setAllCmd} {
command.PersistentFlags().SetAnnotation("script", cobra.BashCompFilenameExt, []string{"qvs"})
}
}

func init() {
rootCmd.AddCommand(buildCmd)
rootCmd.AddCommand(catwalkCmd)
Expand Down