Skip to content

Commit b08d6bf

Browse files
committed
Merge conflicts
2 parents c932956 + a44d32d commit b08d6bf

File tree

7 files changed

+71
-15
lines changed

7 files changed

+71
-15
lines changed

build/azure-pipelines/build-common.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ steps:
5050
displayName: 'Go: build sqlcmd'
5151
inputs:
5252
command: 'build'
53-
arguments: '-o $(Build.BinariesDirectory)/${{ parameters.BinaryName }} -ldflags="-X main.version=${{ parameters.VersionTag }}"'
53+
arguments: '-o $(Build.BinariesDirectory)/${{ parameters.BinaryName }} -ldflags="-s -w -X main.version=${{ parameters.VersionTag }}"'
54+
workingDirectory: '$(Build.SourcesDirectory)/cmd/modern'
55+
env:
56+
GOOS: ${{ parameters.OS }}
57+
GOARCH: ${{ parameters.Arch }}
58+
GOBIN: $(Build.SourcesDirectory)
59+
CGO_ENABLED: 0 # Enables Docker image based off 'scratch'
60+
61+
- task: Go@0
62+
displayName: 'Go: build sqlcmd with debug symbols'
63+
inputs:
64+
command: 'build'
65+
arguments: '-o $(Build.BinariesDirectory)/${{ parameters.BinaryName }}_debug -ldflags="-X main.version=${{ parameters.VersionTag }}"'
5466
workingDirectory: '$(Build.SourcesDirectory)/cmd/modern'
5567
env:
5668
GOOS: ${{ parameters.OS }}

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 c.User != nil && 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

+2-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.

pkg/sqlcmd/connect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (connect ConnectSettings) ConnectionString() (connectionString string, err
127127
query.Add("applicationintent", connect.ApplicationIntent)
128128
}
129129
if connect.LoginTimeoutSeconds > 0 {
130-
query.Add("connection timeout", fmt.Sprint(connect.LoginTimeoutSeconds))
130+
query.Add("dial timeout", fmt.Sprint(connect.LoginTimeoutSeconds))
131131
}
132132
if connect.PacketSize > 0 {
133133
query.Add("packet size", fmt.Sprint(connect.PacketSize))

pkg/sqlcmd/sqlcmd_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestConnectionStringFromSqlCmd(t *testing.T) {
3939
"sqlserver://.?database=somedatabase&trustservercertificate=true&workstation+id=mystation",
4040
},
4141
{
42-
&ConnectSettings{WorkstationName: "mystation", Encrypt: "false", Database: "somedatabase"},
43-
"sqlserver://.?database=somedatabase&encrypt=false&workstation+id=mystation",
42+
&ConnectSettings{WorkstationName: "mystation", Encrypt: "false", Database: "somedatabase", LoginTimeoutSeconds: 50},
43+
"sqlserver://.?database=somedatabase&dial+timeout=50&encrypt=false&workstation+id=mystation",
4444
},
4545
{
4646
&ConnectSettings{TrustServerCertificate: true, Password: pwd, ServerName: `someserver\instance`, Database: "somedatabase", UserName: "someuser"},
@@ -631,10 +631,11 @@ func newConnect(t testing.TB) *ConnectSettings {
631631
}
632632

633633
func TestSqlcmdPrefersSharedMemoryProtocol(t *testing.T) {
634-
if runtime.GOOS != "windows" {
635-
t.Skip()
634+
if runtime.GOOS != "windows" || runtime.GOARCH != "amd64" {
635+
t.Skip("Only valid on Windows amd64")
636636
}
637637
assert.EqualValuesf(t, "lpc", msdsn.ProtocolParsers[0].Protocol(), "lpc should be first protocol")
638-
assert.EqualValuesf(t, "np", msdsn.ProtocolParsers[1].Protocol(), "np should be second protocol")
639-
assert.EqualValuesf(t, "tcp", msdsn.ProtocolParsers[2].Protocol(), "tcp should be third protocol")
638+
assert.EqualValuesf(t, "tcp", msdsn.ProtocolParsers[1].Protocol(), "tcp should be second protocol")
639+
assert.EqualValuesf(t, "np", msdsn.ProtocolParsers[2].Protocol(), "np should be third protocol")
640+
640641
}

pkg/sqlcmd/sqlcmd_windows_amd64.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88

99
func init() {
1010
if len(msdsn.ProtocolParsers) == 3 {
11-
// reorder the protocol parsers to lpc->np->tcp
11+
// reorder the protocol parsers to lpc->tcp->np
1212
// ODBC follows this same order.
1313
var tcp = msdsn.ProtocolParsers[0]
1414
msdsn.ProtocolParsers[0] = msdsn.ProtocolParsers[2]
15-
msdsn.ProtocolParsers[2] = tcp
15+
msdsn.ProtocolParsers[2] = msdsn.ProtocolParsers[1]
16+
msdsn.ProtocolParsers[1] = tcp
1617
}
1718
}

0 commit comments

Comments
 (0)