diff --git a/cmd/modern/main.go b/cmd/modern/main.go index b5fbc2ac..83ba3a0a 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -45,10 +45,32 @@ func main() { cmdparser.Initialize(initializeCallback) rootCmd.Execute() } else { + initializeEnvVars() legacyCmd.Execute(version) } } +// initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD +// if the currentContext is set and if these env vars are not already set. +// In terms of precedence, command line switches/flags take higher precedence +// than env variables and env variables take higher precedence over config +// file info. +func initializeEnvVars() { + initializeCallback() + if config.CurrentContextName() != "" { + server, username, password := config.GetCurrentContextInfo() + if os.Getenv("SQLCMDSERVER") == "" { + os.Setenv("SQLCMDSERVER", server) + } + if os.Getenv("SQLCMDUSER") == "" { + os.Setenv("SQLCMDUSER", username) + } + if os.Getenv("SQLCMDPASSWORD") == "" { + os.Setenv("SQLCMDPASSWORD", password) + } + } +} + // isFirstArgModernCliSubCommand is TEMPORARY code, to be removed when // we remove the Kong based CLI func isFirstArgModernCliSubCommand() (isNewCliCommand bool) { diff --git a/internal/config/context.go b/internal/config/context.go index 25ace2f2..42ee526f 100644 --- a/internal/config/context.go +++ b/internal/config/context.go @@ -75,10 +75,12 @@ func CurrentContext() (endpoint Endpoint, user *User) { } } - for _, u := range config.Users { - if u.Name == *c.User { - user = &u - break + if UserExists(c) { + for _, u := range config.Users { + if u.Name == *c.User { + user = &u + break + } } } } @@ -94,6 +96,23 @@ func CurrentContext() (endpoint Endpoint, user *User) { return } +// GetCurrentContextInfo returns endpoint and basic auth info +// associated with current context +func GetCurrentContextInfo() (server string, username string, password string) { + endpoint, user := CurrentContext() + server = fmt.Sprintf("%s,%d", endpoint.Address, endpoint.Port) + if user != nil { + username = user.BasicAuth.Username + if user.AuthenticationType == "basic" { + password = decryptCallback( + user.BasicAuth.Password, + user.BasicAuth.PasswordEncrypted, + ) + } + } + return +} + // DeleteContext removes the context with the given name from the application's // configuration. If the context does not exist, the function does nothing. The // function also updates the CurrentContext field in the configuration to the diff --git a/internal/config/user.go b/internal/config/user.go index 2fb33a04..b0a1e658 100644 --- a/internal/config/user.go +++ b/internal/config/user.go @@ -5,8 +5,9 @@ package config import ( "fmt" - . "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig" "strconv" + + . "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig" ) // AddUser adds a new user to the configuration. @@ -123,3 +124,9 @@ func userOrdinal(name string) (ordinal int) { } return } + +// UserExists checks if the current context has a 'user', e.g. a context used +// for trusted authentication will not have a user. +func UserExists(context Context) bool { + return context.ContextDetails.User != nil && *context.ContextDetails.User != "" +}