Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cmd/modern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 23 additions & 4 deletions internal/config/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion internal/config/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 != ""
}