mirror of https://github.com/databricks/cli.git
Use profile information when getting a token using the CLI (#855)
## Changes Use stored profile information when the user provides the profile flag when using the `databricks auth token` command. ## Tests Run the command with and without the profile flag ``` ./cli auth token Databricks Host: https://e2-dogfood.staging.cloud.databricks.com/ { "access_token": "****", "token_type": "Bearer", "expiry": "2023-10-10T14:24:11.85617+02:00" }% ./cli auth token --profile DEFAULT { "access_token": "*****", "token_type": "Bearer", "expiry": "2023-10-10T14:24:11.85617+02:00" }% ./cli auth token https://e2-dogfood.staging.cloud.databricks.com/ { "access_token": "*****", "token_type": "Bearer", "expiry": "2023-10-11T09:24:55.046029+02:00" }% ./cli auth token --profile DEFAULT https://e2-dogfood.staging.cloud.databricks.com/ Error: providing both a profile and a host parameters is not supported ```
This commit is contained in:
parent
943ea89728
commit
77101c9b85
|
@ -60,20 +60,10 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
|
||||||
profileName = profile
|
profileName = profile
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the chosen profile has a hostname and the user hasn't specified a host, infer the host from the profile.
|
err := setHost(ctx, profileName, persistentAuth, args)
|
||||||
_, profiles, err := databrickscfg.LoadProfiles(func(p databrickscfg.Profile) bool {
|
|
||||||
return p.Name == profileName
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if persistentAuth.Host == "" {
|
|
||||||
if len(profiles) > 0 && profiles[0].Host != "" {
|
|
||||||
persistentAuth.Host = profiles[0].Host
|
|
||||||
} else {
|
|
||||||
configureHost(ctx, persistentAuth, args, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer persistentAuth.Close()
|
defer persistentAuth.Close()
|
||||||
|
|
||||||
// We need the config without the profile before it's used to initialise new workspace client below.
|
// We need the config without the profile before it's used to initialise new workspace client below.
|
||||||
|
@ -135,3 +125,21 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setHost(ctx context.Context, profileName string, persistentAuth *auth.PersistentAuth, args []string) error {
|
||||||
|
// 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(func(p databrickscfg.Profile) bool {
|
||||||
|
return p.Name == profileName
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if persistentAuth.Host == "" {
|
||||||
|
if len(profiles) > 0 && profiles[0].Host != "" {
|
||||||
|
persistentAuth.Host = profiles[0].Host
|
||||||
|
} else {
|
||||||
|
configureHost(ctx, persistentAuth, args, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package auth
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/databricks/cli/libs/auth"
|
"github.com/databricks/cli/libs/auth"
|
||||||
|
@ -21,8 +22,20 @@ func newTokenCommand(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()
|
||||||
if persistentAuth.Host == "" {
|
|
||||||
configureHost(ctx, persistentAuth, args, 0)
|
var profileName string
|
||||||
|
profileFlag := cmd.Flag("profile")
|
||||||
|
if profileFlag != nil {
|
||||||
|
profileName = profileFlag.Value.String()
|
||||||
|
// If a profile is provided we read the host from the .databrickscfg file
|
||||||
|
if profileName != "" && len(args) > 0 {
|
||||||
|
return errors.New("providing both a profile and a host parameters is not supported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := setHost(ctx, profileName, persistentAuth, args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
defer persistentAuth.Close()
|
defer persistentAuth.Close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue