Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to represent a loaded configuration file #349

Merged
merged 2 commits into from
Mar 31, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (c *Config) EnsureResolved() error {
if len(c.Loaders) == 0 {
c.Loaders = []Loader{
ConfigAttributes,
KnownConfigLoader{},
ConfigFile,
}
}
for _, loader := range c.Loaders {
Expand Down
77 changes: 52 additions & 25 deletions config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,65 @@ import (
"gopkg.in/ini.v1"
)

type KnownConfigLoader struct{}
// File represents the contents of a databrickscfg file.
type File struct {
*ini.File

func (l KnownConfigLoader) Name() string {
return "config-file"
path string
}

func (l KnownConfigLoader) Configure(cfg *Config) error {
// Skip loading config file if some authentication is already explicitly
// configured directly in the config by a user.
// See: https://github.com/databricks/databricks-sdk-go/issues/304
if cfg.Profile == "" && (l.isAnyAuthConfigured(cfg) || cfg.IsAzure()) {
return nil
}
configFile := cfg.ConfigFile
if configFile == "" {
configFile = "~/.databrickscfg"
// Path returns the path of the loaded databrickscfg file.
func (f *File) Path() string {
return f.path
}

// LoadFile loads the databrickscfg file at the specified path.
// The function loads ~/.databrickscfg if the specified path is an empty string.
// The function expands ~ to the user's home directory.
func LoadFile(path string) (*File, error) {
if path == "" {
path = "~/.databrickscfg"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment from @shreyas-goenka on another thread:

This not work for windows, can be added as a followup though

The permutation test shows that it does work, but I agree it looks weird.

Follow up: use filepath.Join here and check for prefix with os.PathSeparator.

}
if strings.HasPrefix(configFile, "~") {

// Expand ~ to home directory.
if strings.HasPrefix(path, "~") {
homedir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot find homedir: %w", err)
return nil, fmt.Errorf("cannot find homedir: %w", err)
}
configFile = fmt.Sprintf("%s%s", homedir, configFile[1:])
path = fmt.Sprintf("%s%s", homedir, path[1:])
}
_, err := os.Stat(configFile)
if os.IsNotExist(err) {
// early return for non-configured machines
logger.Debugf(context.Background(), "%s not found on current host", configFile)

iniFile, err := ini.Load(path)
if err != nil {
return nil, err
}

return &File{iniFile, path}, err
}

var ConfigFile = configFileLoader{}

type configFileLoader struct{}

func (l configFileLoader) Name() string {
return "config-file"
}

func (l configFileLoader) Configure(cfg *Config) error {
// Skip loading config file if some authentication is already explicitly
// configured directly in the config by a user.
// See: https://github.com/databricks/databricks-sdk-go/issues/304
if cfg.Profile == "" && (l.isAnyAuthConfigured(cfg) || cfg.IsAzure()) {
return nil
}
iniFile, err := ini.Load(configFile)
configFile, err := LoadFile(cfg.ConfigFile)
if err != nil {
if os.IsNotExist(err) {
// early return for non-configured machines
logger.Debugf(context.Background(), "%s not found on current host", configFile)
return nil
}
return fmt.Errorf("cannot parse config file: %w", err)
}
// don't modify the config, so that debug appears clearly
Expand All @@ -50,23 +77,23 @@ func (l KnownConfigLoader) Configure(cfg *Config) error {
if !hasExplicitProfile {
profile = "DEFAULT"
}
profileValues := iniFile.Section(profile)
profileValues := configFile.Section(profile)
if len(profileValues.Keys()) == 0 {
if !hasExplicitProfile {
logger.Debugf(context.Background(), "%s has no %s profile configured", configFile, profile)
return nil
}
return fmt.Errorf("%s has no %s profile configured", configFile, profile)
return fmt.Errorf("%s has no %s profile configured", configFile.Path(), profile)
}
logger.Debugf(context.Background(), "Loading %s profile from %s", profile, configFile)
err = ConfigAttributes.ResolveFromStringMap(cfg, profileValues.KeysHash())
if err != nil {
return fmt.Errorf("%s %s profile: %w", configFile, profile, err)
return fmt.Errorf("%s %s profile: %w", configFile.Path(), profile, err)
}
return nil
}

func (l KnownConfigLoader) isAnyAuthConfigured(cfg *Config) bool {
func (l configFileLoader) isAnyAuthConfigured(cfg *Config) bool {
for _, a := range ConfigAttributes {
if a.Auth == "" {
continue
Expand Down