Infer host from profile during login (#629)

## Changes
A pretty annoying part of the current CLI experience is that when
logging in with `databricks auth login`, you always need to type the
name of the host. This seems unnecessary if you have already logged into
a host before, since the CLI can read the previous host from your
`.databrickscfg` file.

This change handles this case by setting the host if unspecified to the
host in the corresponding profile. Combined with autocomplete, this
makes the login process simple:
```
databricks auth login --profile prof<tab><enter>
```

## Tests
Logged in to an existing profile by running the above command (but for a
real profile I had).
This commit is contained in:
Miles Yucht 2023-08-02 11:43:42 +02:00 committed by GitHub
parent 5df8935de4
commit d9ab465ff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 17 deletions

View File

@ -43,8 +43,36 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
cmd.RunE = func(cmd *cobra.Command, args []string) error { cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context() ctx := cmd.Context()
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 = persistentAuth.ProfileName()
prompt.AllowEdit = true
profile, err := prompt.Run()
if err != nil {
return err
}
profileName = profile
}
// If the chosen profile has a hostname and the user hasn't specified a host, infer the host from the profile.
_, profiles, err := databrickscfg.LoadProfiles(databrickscfg.DefaultPath, func(p databrickscfg.Profile) bool {
return p.Name == profileName
})
if err != nil {
return err
}
if persistentAuth.Host == "" { if persistentAuth.Host == "" {
configureHost(ctx, persistentAuth, args, 0) if len(profiles) > 0 && profiles[0].Host != "" {
persistentAuth.Host = profiles[0].Host
} else {
configureHost(ctx, persistentAuth, args, 0)
}
} }
defer persistentAuth.Close() defer persistentAuth.Close()
@ -66,22 +94,7 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
ctx, cancel := context.WithTimeout(ctx, loginTimeout) ctx, cancel := context.WithTimeout(ctx, loginTimeout)
defer cancel() defer cancel()
var profileName string err = persistentAuth.Challenge(ctx)
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 = persistentAuth.ProfileName()
prompt.AllowEdit = true
profile, err := prompt.Run()
if err != nil {
return err
}
profileName = profile
}
err := persistentAuth.Challenge(ctx)
if err != nil { if err != nil {
return err return err
} }