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..7892bddd18 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. @@ -59,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) { @@ -99,3 +104,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, MatchAllProfiles) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + return profiles.Names(), cobra.ShellCompDirectiveNoFileComp +}