-
Notifications
You must be signed in to change notification settings - Fork 168
Add profile on databricks auth login
#423
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package databrickscfg | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/databricks-sdk-go/config" | ||
| "gopkg.in/ini.v1" | ||
| ) | ||
|
|
||
| const fileMode = 0o600 | ||
|
|
||
| func loadOrCreateConfigFile(filename string) (*config.File, error) { | ||
| if filename == "" { | ||
| filename = "~/.databrickscfg" | ||
| } | ||
| // Expand ~ to home directory, as we need a deterministic name for os.OpenFile | ||
| // to work in the cases when ~/.databrickscfg does not exist yet | ||
| if strings.HasPrefix(filename, "~") { | ||
| homedir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot find homedir: %w", err) | ||
| } | ||
| filename = fmt.Sprintf("%s%s", homedir, filename[1:]) | ||
| } | ||
| configFile, err := config.LoadFile(filename) | ||
| if err != nil && os.IsNotExist(err) { | ||
| file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("create %s: %w", filename, err) | ||
| } | ||
| defer file.Close() | ||
| configFile, err = config.LoadFile(filename) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("load created %s: %w", filename, err) | ||
| } | ||
| } else if err != nil { | ||
| return nil, fmt.Errorf("parse %s: %w", filename, err) | ||
| } | ||
| return configFile, nil | ||
| } | ||
|
|
||
| func matchOrCreateSection(ctx context.Context, configFile *config.File, cfg *config.Config) (*ini.Section, error) { | ||
| section, err := findMatchingProfile(configFile, func(s *ini.Section) bool { | ||
| if cfg.Profile == s.Name() { | ||
| return true | ||
| } | ||
| raw := s.KeysHash() | ||
| if cfg.AccountID != "" { | ||
| // here we rely on map zerovals for matching with accounts: | ||
| // if profile has no account id, the raw["account_id"] will be empty | ||
| return cfg.AccountID == raw["account_id"] | ||
| } | ||
| if cfg.Host == "" { | ||
| return false | ||
| } | ||
| host, ok := raw["host"] | ||
| if !ok { | ||
| log.Tracef(ctx, "section %s: no host", s.Name()) | ||
| return false | ||
| } | ||
|
nfx marked this conversation as resolved.
|
||
| // Check if this section matches the normalized host | ||
| return normalizeHost(host) == normalizeHost(cfg.Host) | ||
| }) | ||
| if err == errNoMatchingProfiles { | ||
| section, err = configFile.NewSection(cfg.Profile) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot create new profile: %w", err) | ||
| } | ||
| } else if err != nil { | ||
| return nil, err | ||
| } | ||
| return section, nil | ||
| } | ||
|
|
||
| func SaveToProfile(ctx context.Context, cfg *config.Config) error { | ||
| configFile, err := loadOrCreateConfigFile(cfg.ConfigFile) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| section, err := matchOrCreateSection(ctx, configFile, cfg) | ||
| if err != nil { | ||
| 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 | ||
| for _, oldKey := range section.KeyStrings() { | ||
| section.DeleteKey(oldKey) | ||
| } | ||
|
|
||
| for _, attr := range config.ConfigAttributes { | ||
| if attr.IsZero(cfg) { | ||
| continue | ||
| } | ||
| key := section.Key(attr.Name) | ||
| key.SetValue(attr.GetString(cfg)) | ||
| } | ||
|
|
||
| orig, backupErr := os.ReadFile(configFile.Path()) | ||
|
nfx marked this conversation as resolved.
|
||
| if len(orig) > 0 && backupErr == nil { | ||
| log.Infof(ctx, "Backing up in %s.bak", configFile.Path()) | ||
| err = os.WriteFile(configFile.Path()+".bak", orig, fileMode) | ||
| if err != nil { | ||
| return fmt.Errorf("backup: %w", err) | ||
| } | ||
| log.Infof(ctx, "Overwriting %s", configFile.Path()) | ||
| } else if backupErr != nil { | ||
| log.Warnf(ctx, "Failed to backup %s: %v. Proceeding to save", | ||
| configFile.Path(), backupErr) | ||
| } else { | ||
| log.Infof(ctx, "Saving %s", configFile.Path()) | ||
| } | ||
| return configFile.SaveTo(configFile.Path()) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why all caps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
account profiles should get special treatment. i'll add
ACCOUNTprofile to denote the default account in the subsequent PRs