Skip to content

Commit a44d32d

Browse files
Establish conn to curret-context if no args given (#261)
sqlcmd when invoked with no arguments, executes in backward compat mode and ignores current-context info for establishing connection. This commit changes the default behavior to establish connection to the endpoint associated with current-context in sqlcmd config file by setting SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD as env variables. If these env variables are already set then their values are not overridden. This ensures that users can connect to sql server instance pointed by current-context if sqlcmd is invoked with no arguments.
1 parent 9a108e1 commit a44d32d

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

cmd/modern/main.go

+22
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,32 @@ func main() {
4545
cmdparser.Initialize(initializeCallback)
4646
rootCmd.Execute()
4747
} else {
48+
initializeEnvVars()
4849
legacyCmd.Execute(version)
4950
}
5051
}
5152

53+
// initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD
54+
// if the currentContext is set and if these env vars are not already set.
55+
// In terms of precedence, command line switches/flags take higher precedence
56+
// than env variables and env variables take higher precedence over config
57+
// file info.
58+
func initializeEnvVars() {
59+
initializeCallback()
60+
if config.CurrentContextName() != "" {
61+
server, username, password := config.GetCurrentContextInfo()
62+
if os.Getenv("SQLCMDSERVER") == "" {
63+
os.Setenv("SQLCMDSERVER", server)
64+
}
65+
if os.Getenv("SQLCMDUSER") == "" {
66+
os.Setenv("SQLCMDUSER", username)
67+
}
68+
if os.Getenv("SQLCMDPASSWORD") == "" {
69+
os.Setenv("SQLCMDPASSWORD", password)
70+
}
71+
}
72+
}
73+
5274
// isFirstArgModernCliSubCommand is TEMPORARY code, to be removed when
5375
// we remove the Kong based CLI
5476
func isFirstArgModernCliSubCommand() (isNewCliCommand bool) {

internal/config/context.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ func CurrentContext() (endpoint Endpoint, user *User) {
7575
}
7676
}
7777

78-
for _, u := range config.Users {
79-
if u.Name == *c.User {
80-
user = &u
81-
break
78+
if UserExists(c) {
79+
for _, u := range config.Users {
80+
if u.Name == *c.User {
81+
user = &u
82+
break
83+
}
8284
}
8385
}
8486
}
@@ -94,6 +96,23 @@ func CurrentContext() (endpoint Endpoint, user *User) {
9496
return
9597
}
9698

99+
// GetCurrentContextInfo returns endpoint and basic auth info
100+
// associated with current context
101+
func GetCurrentContextInfo() (server string, username string, password string) {
102+
endpoint, user := CurrentContext()
103+
server = fmt.Sprintf("%s,%d", endpoint.Address, endpoint.Port)
104+
if user != nil {
105+
username = user.BasicAuth.Username
106+
if user.AuthenticationType == "basic" {
107+
password = decryptCallback(
108+
user.BasicAuth.Password,
109+
user.BasicAuth.PasswordEncrypted,
110+
)
111+
}
112+
}
113+
return
114+
}
115+
97116
// DeleteContext removes the context with the given name from the application's
98117
// configuration. If the context does not exist, the function does nothing. The
99118
// function also updates the CurrentContext field in the configuration to the

internal/config/user.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ package config
55

66
import (
77
"fmt"
8-
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
98
"strconv"
9+
10+
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
1011
)
1112

1213
// AddUser adds a new user to the configuration.
@@ -123,3 +124,9 @@ func userOrdinal(name string) (ordinal int) {
123124
}
124125
return
125126
}
127+
128+
// UserExists checks if the current context has a 'user', e.g. a context used
129+
// for trusted authentication will not have a user.
130+
func UserExists(context Context) bool {
131+
return context.ContextDetails.User != nil && *context.ContextDetails.User != ""
132+
}

0 commit comments

Comments
 (0)