From d23d4aef3a41136d987a731c735df471fbc726ff Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Thu, 22 Jun 2023 17:20:10 +0200 Subject: [PATCH] Fixed error on multiple profiles and failure to create a new profile with configured cluster (#513) --- cmd/auth/login.go | 4 +++- libs/databrickscfg/loader.go | 3 +++ libs/databrickscfg/ops.go | 4 +--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/auth/login.go b/cmd/auth/login.go index c905d6cf..13db6147 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -49,11 +49,12 @@ var loginCmd = &cobra.Command{ return err } + // We need the config without the profile before it's used to initialise new workspace client below. + // Otherwise it will complain about non existing profile because it was not yet saved. cfg := config.Config{ Host: perisistentAuth.Host, AccountID: perisistentAuth.AccountID, AuthType: "databricks-cli", - Profile: profileName, } if configureCluster { @@ -77,6 +78,7 @@ var loginCmd = &cobra.Command{ cfg.ClusterID = clusterId } + cfg.Profile = profileName err = databrickscfg.SaveToProfile(ctx, &cfg) if err != nil { return err diff --git a/libs/databrickscfg/loader.go b/libs/databrickscfg/loader.go index 8179703a..e0507961 100644 --- a/libs/databrickscfg/loader.go +++ b/libs/databrickscfg/loader.go @@ -76,6 +76,9 @@ func (l profileFromHostLoader) Configure(cfg *config.Config) error { // Normalized version of the configured host. host := normalizeHost(cfg.Host) match, err := findMatchingProfile(configFile, func(s *ini.Section) bool { + if cfg.Profile != "" { + return cfg.Profile == s.Name() + } key, err := s.GetKey("host") if err != nil { log.Tracef(ctx, "section %s: %s", s.Name(), err) diff --git a/libs/databrickscfg/ops.go b/libs/databrickscfg/ops.go index 4a4a27b0..9f3b8db4 100644 --- a/libs/databrickscfg/ops.go +++ b/libs/databrickscfg/ops.go @@ -89,8 +89,6 @@ func SaveToProfile(ctx context.Context, cfg *config.Config) error { return err } - // zeroval profile name before adding it to a section - cfg.Profile = "" cfg.ConfigFile = "" // clear old keys in case we're overriding the section @@ -99,7 +97,7 @@ func SaveToProfile(ctx context.Context, cfg *config.Config) error { } for _, attr := range config.ConfigAttributes { - if attr.IsZero(cfg) { + if attr.IsZero(cfg) || attr.Name == "profile" { continue } key := section.Key(attr.Name)