Added prompts for Databricks profile for auth login command (#502)

## Changes
Added prompts for Databricks profile for auth login command

## Tests
```
andrew.nester@HFW9Y94129 cli % ./cli auth login https://xxxx-databricks.com
✔ Databricks Profile Name: my-profile█
```
This commit is contained in:
Andrew Nester 2023-06-21 12:58:28 +02:00 committed by GitHub
parent ccbcccd929
commit 7db1990c56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 12 deletions

View File

@ -2,9 +2,13 @@ package auth
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/auth"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/databricks-sdk-go/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -20,7 +24,40 @@ var loginCmd = &cobra.Command{
defer perisistentAuth.Close() defer perisistentAuth.Close()
ctx, cancel := context.WithTimeout(cmd.Context(), loginTimeout) ctx, cancel := context.WithTimeout(cmd.Context(), loginTimeout)
defer cancel() defer cancel()
return perisistentAuth.Challenge(ctx)
var profileName string
profileFlag := cmd.Flag("profile")
if profileFlag != nil && profileFlag.Value.String() != "" {
profileName = profileFlag.Value.String()
} else {
prompt := cmdio.Prompt(ctx)
prompt.Label = "Databricks Profile Name"
prompt.Default = perisistentAuth.ProfileName()
prompt.AllowEdit = true
profile, err := prompt.Run()
if err != nil {
return err
}
profileName = profile
}
err := perisistentAuth.Challenge(ctx)
if err != nil {
return err
}
err = databrickscfg.SaveToProfile(ctx, &config.Config{
Host: perisistentAuth.Host,
AccountID: perisistentAuth.AccountID,
AuthType: "databricks-cli",
Profile: profileName,
})
if err != nil {
return err
}
cmdio.LogString(ctx, fmt.Sprintf("Profile %s was successfully saved", profileName))
return nil
}, },
} }

View File

@ -16,8 +16,6 @@ import (
"time" "time"
"github.com/databricks/cli/libs/auth/cache" "github.com/databricks/cli/libs/auth/cache"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/retries" "github.com/databricks/databricks-sdk-go/retries"
"github.com/pkg/browser" "github.com/pkg/browser"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@ -97,7 +95,7 @@ func (a *PersistentAuth) Load(ctx context.Context) (*oauth2.Token, error) {
return refreshed, nil return refreshed, nil
} }
func (a *PersistentAuth) profileName() string { func (a *PersistentAuth) ProfileName() string {
// TODO: get profile name from interactive input // TODO: get profile name from interactive input
if a.AccountID != "" { if a.AccountID != "" {
return fmt.Sprintf("ACCOUNT-%s", a.AccountID) return fmt.Sprintf("ACCOUNT-%s", a.AccountID)
@ -132,12 +130,7 @@ func (a *PersistentAuth) Challenge(ctx context.Context) error {
if err != nil { if err != nil {
return fmt.Errorf("store: %w", err) return fmt.Errorf("store: %w", err)
} }
return databrickscfg.SaveToProfile(ctx, &config.Config{ return nil
Host: a.Host,
AccountID: a.AccountID,
AuthType: "databricks-cli",
Profile: a.profileName(),
})
} }
func (a *PersistentAuth) init(ctx context.Context) error { func (a *PersistentAuth) init(ctx context.Context) error {

View File

@ -47,8 +47,8 @@ func loadOrCreateConfigFile(filename string) (*config.File, error) {
func matchOrCreateSection(ctx context.Context, configFile *config.File, cfg *config.Config) (*ini.Section, error) { func matchOrCreateSection(ctx context.Context, configFile *config.File, cfg *config.Config) (*ini.Section, error) {
section, err := findMatchingProfile(configFile, func(s *ini.Section) bool { section, err := findMatchingProfile(configFile, func(s *ini.Section) bool {
if cfg.Profile == s.Name() { if cfg.Profile != "" {
return true return cfg.Profile == s.Name()
} }
raw := s.KeysHash() raw := s.KeysHash()
if cfg.AccountID != "" { if cfg.AccountID != "" {