mirror of https://github.com/databricks/cli.git
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:
parent
5df8935de4
commit
d9ab465ff9
|
@ -43,9 +43,37 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
|
|||
|
||||
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
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 len(profiles) > 0 && profiles[0].Host != "" {
|
||||
persistentAuth.Host = profiles[0].Host
|
||||
} else {
|
||||
configureHost(ctx, persistentAuth, args, 0)
|
||||
}
|
||||
}
|
||||
defer persistentAuth.Close()
|
||||
|
||||
// We need the config without the profile before it's used to initialise new workspace client below.
|
||||
|
@ -66,22 +94,7 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
|
|||
ctx, cancel := context.WithTimeout(ctx, loginTimeout)
|
||||
defer cancel()
|
||||
|
||||
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
|
||||
}
|
||||
err := persistentAuth.Challenge(ctx)
|
||||
err = persistentAuth.Challenge(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue