Skip to content

feat: Add -A switch #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2023
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
49 changes: 10 additions & 39 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ SOFTWARE
## github.com/Azure/azure-sdk-for-go/sdk/azidentity

* Name: github.com/Azure/azure-sdk-for-go/sdk/azidentity
* Version: v1.1.0
* License: [MIT](https://github.com/Azure/azure-sdk-for-go/blob/sdk/azidentity/v1.1.0/sdk/azidentity/LICENSE.txt)
* Version: v1.2.1
* License: [MIT](https://github.com/Azure/azure-sdk-for-go/blob/sdk/azidentity/v1.2.1/sdk/azidentity/LICENSE.txt)

```
MIT License
Expand Down Expand Up @@ -99,8 +99,8 @@ SOFTWARE
## github.com/AzureAD/microsoft-authentication-library-for-go/apps

* Name: github.com/AzureAD/microsoft-authentication-library-for-go/apps
* Version: v0.5.1
* License: [MIT](https://github.com/AzureAD/microsoft-authentication-library-for-go/blob/v0.5.1/LICENSE)
* Version: v0.8.1
* License: [MIT](https://github.com/AzureAD/microsoft-authentication-library-for-go/blob/v0.8.1/LICENSE)

```
MIT License
Expand Down Expand Up @@ -188,35 +188,6 @@ SOFTWARE.

```

## github.com/alecthomas/kong

* Name: github.com/alecthomas/kong
* Version: v0.6.2-0.20220922001058-c62bf25854a0
* License: [MIT](https://github.com/alecthomas/kong/blob/c62bf25854a0/COPYING)

```
Copyright (C) 2018 Alec Thomas

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

```

## github.com/beorn7/perks/quantile

* Name: github.com/beorn7/perks/quantile
Expand Down Expand Up @@ -344,8 +315,8 @@ SOFTWARE.
## github.com/docker/distribution

* Name: github.com/docker/distribution
* Version: v2.8.1
* License: [Apache-2.0](https://github.com/docker/distribution/blob/v2.8.1/LICENSE)
* Version: v2.8.2
* License: [Apache-2.0](https://github.com/docker/distribution/blob/v2.8.2/LICENSE)

```
Apache License
Expand Down Expand Up @@ -1437,11 +1408,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

```

## github.com/golang-jwt/jwt
## github.com/golang-jwt/jwt/v4

* Name: github.com/golang-jwt/jwt
* Version: v3.2.2
* License: [MIT](https://github.com/golang-jwt/jwt/blob/v3.2.2/LICENSE)
* Name: github.com/golang-jwt/jwt/v4
* Version: v4.4.2
* License: [MIT](https://github.com/golang-jwt/jwt/blob/v4.4.2/LICENSE)

```
Copyright (c) 2012 Dave Grijalva
Expand Down
4 changes: 3 additions & 1 deletion cmd/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type SQLCmdArguments struct {
TrimSpaces bool
MultiSubnetFailover bool
Password string
DedicatedAdminConnection bool
// Keep Help at the end of the list
Help bool
}
Expand Down Expand Up @@ -232,10 +233,10 @@ func setFlags(rootCmd *cobra.Command, args *SQLCmdArguments) {

// Using PersistentFlags() for ErrorSeverityLevel due to data type uint8 , which is not supported in Flags()
rootCmd.PersistentFlags().Uint8VarP(&args.ErrorSeverityLevel, "error-severity-level", "V", 0, "Controls the severity level that is used to set the ERRORLEVEL variable on exit.")

var screenWidth int
screenWidthPtr := &screenWidth
rootCmd.Flags().IntVarP(screenWidthPtr, "screen-width", "w", 0, localizer.Sprintf("Specifies the screen width for output"))
rootCmd.Flags().BoolVarP(&args.DedicatedAdminConnection, "dedicated-admin-connection", "A", false, localizer.Sprintf("Dedicated administrator connection"))
}

func normalizeFlags(rootCmd *cobra.Command) error {
Expand Down Expand Up @@ -388,6 +389,7 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
connect.LogLevel = args.DriverLoggingLevel
connect.ExitOnError = args.ExitOnError
connect.ErrorSeverityLevel = args.ErrorSeverityLevel
connect.DedicatedAdminConnection = args.DedicatedAdminConnection
}

func isConsoleInitializationRequired(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments) bool {
Expand Down
12 changes: 10 additions & 2 deletions cmd/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sqlcmd

import (
"bytes"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -68,8 +69,8 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
{[]string{"-h", "2", "-?"}, func(args SQLCmdArguments) bool {
return args.Help && args.Headers == 2
}},
{[]string{"-u"}, func(args SQLCmdArguments) bool {
return args.UnicodeOutputFile
{[]string{"-u", "-A"}, func(args SQLCmdArguments) bool {
return args.UnicodeOutputFile && args.DedicatedAdminConnection
}},
{[]string{"--version"}, func(args SQLCmdArguments) bool {
return args.Version
Expand Down Expand Up @@ -98,7 +99,10 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
Run: func(cmd *cobra.Command, argss []string) {
// Command logic goes here
},
SilenceErrors: true,
SilenceUsage: true,
}
cmd.SetOut(new(bytes.Buffer))
setFlags(cmd, arguments)
cmd.SetArgs(test.commandLine)
err := cmd.Execute()
Expand Down Expand Up @@ -136,6 +140,8 @@ func TestInvalidCommandLine(t *testing.T) {
},
Run: func(cmd *cobra.Command, argss []string) {
},
SilenceErrors: true,
SilenceUsage: true,
}
setFlags(cmd, arguments)
cmd.SetArgs(test.commandLine)
Expand Down Expand Up @@ -169,6 +175,8 @@ func TestValidateFlags(t *testing.T) {
},
Run: func(cmd *cobra.Command, argss []string) {
},
SilenceErrors: true,
SilenceUsage: true,
}

setFlags(cmd, arguments)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/docker/go-connections v0.4.0
github.com/golang-sql/sqlexp v0.1.0
github.com/google/uuid v1.3.0
github.com/microsoft/go-mssqldb v0.20.0
github.com/microsoft/go-mssqldb v1.0.0
github.com/opencontainers/image-spec v1.0.2
github.com/peterh/liner v1.2.2
github.com/pkg/errors v0.9.1
Expand All @@ -26,9 +26,9 @@ require (

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.1.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v0.8.1 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
Expand All @@ -39,7 +39,7 @@ require (
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
Expand Down
19 changes: 8 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.0.0/go.mod h1:uGG2W01BaETf0Ozp+QxxKJdMBNRWPdstHG0Fmdwn1/U=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.1.2 h1:lneMk5qtUMulXa/eVxjVd+/bDYMEDIqYpLzLa2/EsNI=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.1.2/go.mod h1:uGG2W01BaETf0Ozp+QxxKJdMBNRWPdstHG0Fmdwn1/U=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0 h1:QkAcEIAKbNL4KoFr4SathZPhDhF4mVwpBMFlYjyAqy8=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0/go.mod h1:bhXu1AjYL+wutSL/kpSq6s7733q2Rb0yuot9Zgfqa/0=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.1 h1:T8quHYlUGyb/oqtSTwqlCr1ilJHrDv+ZtpSfo+hm1BU=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.1/go.mod h1:gLa1CL2RNE4s7M3yopJ/p0iq5DdY6Yv5ZUt9MTRZOQM=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 h1:jp0dGvZ7ZK0mgqnTSClMxa5xuRL7NZgHameVYF6BurY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1 h1:BWe8a+f/t+7KY7zH2mqygeUD0t8hNFXe08p1Pb3/jKE=
github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1/go.mod h1:Vt9sXTKwMyGcOxSmLDMnGPgqsUg7m8pe215qMLrDXw4=
github.com/AzureAD/microsoft-authentication-library-for-go v0.8.1 h1:oPdPEZFSbl7oSPEAIPMPBMUmiL+mqgzBJwM/9qYcwNg=
github.com/AzureAD/microsoft-authentication-library-for-go v0.8.1/go.mod h1:4qFor3D/HDsvBME35Xy9rwW9DecL+M2sNw1ybjPtwA0=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
Expand Down Expand Up @@ -119,11 +119,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt/v4 v4.2.0 h1:besgBTC8w8HjP6NzQdxwKH9Z5oQMZ24ThTrHp3cZ8eU=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
Expand Down Expand Up @@ -239,8 +236,8 @@ github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8Bz
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microsoft/go-mssqldb v0.20.0 h1:jAfvs5TIR311fEuNgohFzMtUO0IcCHyI1kWAVws4ES8=
github.com/microsoft/go-mssqldb v0.20.0/go.mod h1:ukJCBnnzLzpVF0qYRT+eg1e+eSwjeQ7IvenUv8QPook=
github.com/microsoft/go-mssqldb v1.0.0 h1:k2p2uuG8T5T/7Hp7/e3vMGTnnR0sU4h8d1CcC71iLHU=
github.com/microsoft/go-mssqldb v1.0.0/go.mod h1:+4wZTUnz/SV6nffv+RRRB/ss8jPng5Sho2SmM1l2ts4=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/term v0.0.0-20221120202655-abb19827d345 h1:J9c53/kxIH+2nTKBEfZYFMlhghtHpIHSXpm5VRGHSnU=
Expand Down
1 change: 1 addition & 0 deletions pkg/sqlcmd-linter/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var AllowedImports = map[string][]string{
`"github.com/microsoft/go-mssqldb`: {},
`"github.com/microsoft/go-sqlcmd`: {},
`"github.com/spf13/cobra`: {`cmd/sqlcmd`, `cmd/modern`},
`"github.com/spf13/pflag`: {`cmd/sqlcmd`, `cmd/modern`},
`"github.com/spf13/viper`: {`cmd/sqlcmd`, `cmd/modern`},
`"github.com/stretchr/testify`: {},
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/sqlcmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type ConnectSettings struct {
Database string
// ApplicationName is the name of the application to be included in the connection string
ApplicationName string
// DedicatedAdminConnection forces the connection to occur over tcp on the dedicated admin port. Requires Browser service access
DedicatedAdminConnection bool
}

func (c ConnectSettings) authenticationMethod() string {
Expand Down Expand Up @@ -147,6 +149,9 @@ func (connect ConnectSettings) ConnectionString() (connectionString string, err
if connect.ApplicationName != "" {
query.Add(`app name`, connect.ApplicationName)
}
if connect.DedicatedAdminConnection {
query.Set("protocol", "admin")
}
connectionURL.RawQuery = query.Encode()
return connectionURL.String(), nil
}
17 changes: 15 additions & 2 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func TestConnectionStringFromSqlCmd(t *testing.T) {
&ConnectSettings{ServerName: `\\someserver\pipe\sql\query`},
"sqlserver://someserver?pipe=sql%5Cquery&protocol=np",
},
{
&ConnectSettings{DedicatedAdminConnection: true},
"sqlserver://.?protocol=admin",
},
{
&ConnectSettings{ServerName: `tcp:someserver`, DedicatedAdminConnection: true},
"sqlserver://someserver?protocol=admin",
},
{
&ConnectSettings{ServerName: `admin:someserver`, DedicatedAdminConnection: true},
"sqlserver://someserver?protocol=admin",
},
}

for i, test := range commands {
Expand Down Expand Up @@ -633,7 +645,8 @@ func TestSqlcmdPrefersSharedMemoryProtocol(t *testing.T) {
t.Skip("Only valid on Windows amd64")
}
assert.EqualValuesf(t, "lpc", msdsn.ProtocolParsers[0].Protocol(), "lpc should be first protocol")
assert.EqualValuesf(t, "tcp", msdsn.ProtocolParsers[1].Protocol(), "tcp should be second protocol")
assert.EqualValuesf(t, "np", msdsn.ProtocolParsers[2].Protocol(), "np should be third protocol")
assert.Truef(t, msdsn.ProtocolParsers[1].Hidden(), "Protocol %s should be hidden", msdsn.ProtocolParsers[1].Protocol())
assert.EqualValuesf(t, "tcp", msdsn.ProtocolParsers[2].Protocol(), "tcp should be second protocol")
assert.EqualValuesf(t, "np", msdsn.ProtocolParsers[3].Protocol(), "np should be third protocol")

}
11 changes: 6 additions & 5 deletions pkg/sqlcmd/sqlcmd_windows_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

func init() {
if len(msdsn.ProtocolParsers) == 3 {
// reorder the protocol parsers to lpc->tcp->np
if len(msdsn.ProtocolParsers) == 4 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment on to why this is scoped to just amd64 on windows (per filename)? Not ARM64?

// reorder the protocol parsers to lpc->admin->tcp->np
// ODBC follows this same order.
// Named pipes/shared memory package doesn't support ARM
var tcp = msdsn.ProtocolParsers[0]
msdsn.ProtocolParsers[0] = msdsn.ProtocolParsers[2]
msdsn.ProtocolParsers[2] = msdsn.ProtocolParsers[1]
msdsn.ProtocolParsers[1] = tcp
msdsn.ProtocolParsers[0] = msdsn.ProtocolParsers[3]
msdsn.ProtocolParsers[3] = msdsn.ProtocolParsers[2]
msdsn.ProtocolParsers[2] = tcp
}
}