Skip to content

Commit c9512bf

Browse files
Deduplicate config file write logic and use env.Get for consistency
Extract writeConfigFile helper to consolidate the repeated default-comment, backup, and save sequence shared by SaveToProfile and DeleteProfile. Also switch auth login from os.Getenv to env.Get(ctx, ...) for DATABRICKS_CONFIG_FILE so it respects context-level env overrides, consistent with the rest of the codebase.
1 parent aabaf7f commit c9512bf

2 files changed

Lines changed: 14 additions & 21 deletions

File tree

cmd/auth/login.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"os"
98
"runtime"
109
"strings"
1110
"time"
@@ -249,7 +248,7 @@ depends on the existing profiles you have set in your configuration file
249248
WorkspaceID: authArguments.WorkspaceID,
250249
Experimental_IsUnifiedHost: authArguments.IsUnifiedHost,
251250
ClusterID: clusterID,
252-
ConfigFile: os.Getenv("DATABRICKS_CONFIG_FILE"),
251+
ConfigFile: env.Get(ctx, "DATABRICKS_CONFIG_FILE"),
253252
ServerlessComputeID: serverlessComputeID,
254253
Scopes: scopesList,
255254
}, clearKeys...)

libs/databrickscfg/ops.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ func AuthCredentialKeys() []string {
9494
return keys
9595
}
9696

97+
func writeConfigFile(ctx context.Context, configFile *config.File) error {
98+
section := configFile.Section(ini.DefaultSection)
99+
if len(section.Keys()) == 0 && section.Comment == "" {
100+
section.Comment = defaultComment
101+
}
102+
if err := backupConfigFile(ctx, configFile); err != nil {
103+
return err
104+
}
105+
return configFile.SaveTo(configFile.Path())
106+
}
107+
97108
func backupConfigFile(ctx context.Context, configFile *config.File) error {
98109
orig, backupErr := os.ReadFile(configFile.Path())
99110
if len(orig) > 0 && backupErr == nil {
@@ -147,16 +158,7 @@ func SaveToProfile(ctx context.Context, cfg *config.Config, clearKeys ...string)
147158
key.SetValue(attr.GetString(cfg))
148159
}
149160

150-
// Add a comment to the default section if it's empty.
151-
section = configFile.Section(ini.DefaultSection)
152-
if len(section.Keys()) == 0 && section.Comment == "" {
153-
section.Comment = defaultComment
154-
}
155-
156-
if err := backupConfigFile(ctx, configFile); err != nil {
157-
return err
158-
}
159-
return configFile.SaveTo(configFile.Path())
161+
return writeConfigFile(ctx, configFile)
160162
}
161163

162164
// DeleteProfile removes the named profile section from the databrickscfg file.
@@ -175,15 +177,7 @@ func DeleteProfile(ctx context.Context, profileName, configFilePath string) erro
175177

176178
configFile.DeleteSection(profileName)
177179

178-
section := configFile.Section(ini.DefaultSection)
179-
if len(section.Keys()) == 0 && section.Comment == "" {
180-
section.Comment = defaultComment
181-
}
182-
183-
if err := backupConfigFile(ctx, configFile); err != nil {
184-
return err
185-
}
186-
return configFile.SaveTo(configFile.Path())
180+
return writeConfigFile(ctx, configFile)
187181
}
188182

189183
func ValidateConfigAndProfileHost(cfg *config.Config, profile string) error {

0 commit comments

Comments
 (0)