From c01af5cf5d01853b929028f3b3650a716bc6ef88 Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Tue, 11 Jul 2023 18:28:25 +0200 Subject: [PATCH 1/2] Support tab completion for profiles --- cmd/root/auth.go | 1 + libs/databrickscfg/profiles.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/cmd/root/auth.go b/cmd/root/auth.go index 61068ab384..ae7f739682 100644 --- a/cmd/root/auth.go +++ b/cmd/root/auth.go @@ -23,6 +23,7 @@ var currentUser int func init() { RootCmd.PersistentFlags().StringP("profile", "p", "", "~/.databrickscfg profile") + RootCmd.RegisterFlagCompletionFunc("profile", databrickscfg.ProfileCompletion) } func MustAccountClient(cmd *cobra.Command, args []string) error { diff --git a/libs/databrickscfg/profiles.go b/libs/databrickscfg/profiles.go index 60b2a89a2f..19eeef0f63 100644 --- a/libs/databrickscfg/profiles.go +++ b/libs/databrickscfg/profiles.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/databricks/databricks-sdk-go/config" + "github.com/spf13/cobra" ) // Profile holds a subset of the keys in a databrickscfg profile. @@ -99,3 +100,11 @@ func LoadProfiles(path string, fn ProfileMatchFunction) (file string, profiles P return } + +func ProfileCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + _, profiles, err := LoadProfiles(DefaultPath, func(p Profile) bool { return true }) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + return profiles.Names(), cobra.ShellCompDirectiveNoFileComp +} From 1c29764b462f2c281f8b7c720c811288fdc812d0 Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Wed, 12 Jul 2023 11:52:46 +0200 Subject: [PATCH 2/2] Abstract --- libs/databrickscfg/profiles.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/databrickscfg/profiles.go b/libs/databrickscfg/profiles.go index 19eeef0f63..7892bddd18 100644 --- a/libs/databrickscfg/profiles.go +++ b/libs/databrickscfg/profiles.go @@ -60,6 +60,10 @@ func MatchAccountProfiles(p Profile) bool { return p.Host != "" && p.AccountID != "" } +func MatchAllProfiles(p Profile) bool { + return true +} + const DefaultPath = "~/.databrickscfg" func LoadProfiles(path string, fn ProfileMatchFunction) (file string, profiles Profiles, err error) { @@ -102,7 +106,7 @@ func LoadProfiles(path string, fn ProfileMatchFunction) (file string, profiles P } func ProfileCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - _, profiles, err := LoadProfiles(DefaultPath, func(p Profile) bool { return true }) + _, profiles, err := LoadProfiles(DefaultPath, MatchAllProfiles) if err != nil { return nil, cobra.ShellCompDirectiveError }