Skip to content

Commit 14713ab

Browse files
Merge branch 'main' into main
2 parents 3459de9 + c702a83 commit 14713ab

19 files changed

Lines changed: 386 additions & 47 deletions

File tree

cmd/sqlcmd/sqlcmd.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type SQLCmdArguments struct {
8282
ChangePassword string
8383
ChangePasswordAndExit string
8484
TraceFile string
85+
ServerNameOverride string
8586
// Keep Help at the end of the list
8687
Help bool
8788
}
@@ -294,6 +295,8 @@ func Execute(version string) {
294295
// We need to rewrite the arguments to add -i and -v in front of each space-delimited value to be Cobra-friendly.
295296
// For flags like -r we need to inject the default value if the user omits it
296297
func convertOsArgs(args []string) (cargs []string) {
298+
args = preprocessHelpFlags(args)
299+
297300
flag := ""
298301
first := true
299302
for i, a := range args {
@@ -323,6 +326,31 @@ func convertOsArgs(args []string) (cargs []string) {
323326
return
324327
}
325328

329+
// preprocessHelpFlags converts -h (without number) and -help to help flags.
330+
// -h with a number is left alone for header count backward compatibility.
331+
func preprocessHelpFlags(args []string) []string {
332+
result := make([]string, 0, len(args))
333+
for i := 0; i < len(args); i++ {
334+
arg := args[i]
335+
if arg == "-help" {
336+
result = append(result, "--help")
337+
continue
338+
}
339+
if arg == "-h" {
340+
if i+1 < len(args) {
341+
if _, err := strconv.Atoi(args[i+1]); err == nil {
342+
result = append(result, arg) // -h <number> for headers
343+
continue
344+
}
345+
}
346+
result = append(result, "-?")
347+
continue
348+
}
349+
result = append(result, arg)
350+
}
351+
return result
352+
}
353+
326354
// If args[i] is the given flag and args[i+1] is another flag, returns the value to append after the flag
327355
func checkDefaultValue(args []string, i int) (val string) {
328356
flags := map[rune]string{
@@ -411,6 +439,7 @@ func setFlags(rootCmd *cobra.Command, args *SQLCmdArguments) {
411439
rootCmd.Flags().StringVarP(&args.InitialQuery, "initial-query", "q", "", localizer.Sprintf("Executes a query when sqlcmd starts, but does not exit sqlcmd when the query has finished running. Multiple-semicolon-delimited queries can be executed"))
412440
rootCmd.Flags().StringVarP(&args.Query, "query", "Q", "", localizer.Sprintf("Executes a query when sqlcmd starts and then immediately exits sqlcmd. Multiple-semicolon-delimited queries can be executed"))
413441
rootCmd.Flags().StringVarP(&args.Server, "server", "S", "", localizer.Sprintf("%s Specifies the instance of SQL Server to which to connect. It sets the sqlcmd scripting variable %s.", localizer.ConnStrPattern, localizer.ServerEnvVar))
442+
rootCmd.Flags().StringVar(&args.ServerNameOverride, "server-name", "", localizer.Sprintf("Specifies the server name to use for authentication when tunneling through a proxy. Use with -S to specify the dial address separately from the server name sent to SQL Server."))
414443
_ = rootCmd.Flags().IntP(disableCmdAndWarn, "X", 0, localizer.Sprintf("%s Disables commands that might compromise system security. Passing 1 tells sqlcmd to exit when disabled commands are run.", "-X[1]"))
415444
rootCmd.Flags().StringVar(&args.AuthenticationMethod, "authentication-method", "", localizer.Sprintf(
416445
"Specifies the SQL authentication method to use to connect to Azure SQL Database. One of: %s",
@@ -738,6 +767,7 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
738767
}
739768
connect.HostNameInCertificate = args.HostNameInCertificate
740769
connect.ServerCertificate = args.ServerCertificate
770+
connect.ServerNameOverride = args.ServerNameOverride
741771
connect.PacketSize = args.PacketSize
742772
connect.WorkstationName = args.WorkstationName
743773
connect.LogLevel = args.DriverLoggingLevel

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,30 @@ func TestConvertOsArgs(t *testing.T) {
598598
}
599599
}
600600

601+
func TestPreprocessHelpFlags(t *testing.T) {
602+
type test struct {
603+
name string
604+
in []string
605+
expected []string
606+
}
607+
608+
tests := []test{
609+
{"empty args", []string{}, []string{}},
610+
{"-help to --help", []string{"-help"}, []string{"--help"}},
611+
{"-h alone to -?", []string{"-h"}, []string{"-?"}},
612+
{"-h with number stays", []string{"-h", "5"}, []string{"-h", "5"}},
613+
{"-h with flag becomes -?", []string{"-h", "-S"}, []string{"-?", "-S"}},
614+
{"mixed args", []string{"-S", "server", "-h", "-Q", "select 1"}, []string{"-S", "server", "-?", "-Q", "select 1"}},
615+
{"preserve -h N in context", []string{"-S", "srv", "-h", "10", "-Q", "x"}, []string{"-S", "srv", "-h", "10", "-Q", "x"}},
616+
}
617+
for _, c := range tests {
618+
t.Run(c.name, func(t *testing.T) {
619+
actual := preprocessHelpFlags(c.in)
620+
assert.Equal(t, c.expected, actual, "Incorrect preprocessed args")
621+
})
622+
}
623+
}
624+
601625
func TestEncryptionOptions(t *testing.T) {
602626
type test struct {
603627
input string

internal/translations/catalog.go

Lines changed: 51 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/translations/locales/de-DE/out.gotext.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,11 @@
26722672
],
26732673
"fuzzy": true
26742674
},
2675+
{
2676+
"id": "Specifies the server name to use for authentication when tunneling through a proxy. Use with -S to specify the dial address separately from the server name sent to SQL Server.",
2677+
"message": "Specifies the server name to use for authentication when tunneling through a proxy. Use with -S to specify the dial address separately from the server name sent to SQL Server.",
2678+
"translation": ""
2679+
},
26752680
{
26762681
"id": "{_X1} Disables commands that might compromise system security. Passing 1 tells sqlcmd to exit when disabled commands are run.",
26772682
"message": "{_X1} Disables commands that might compromise system security. Passing 1 tells sqlcmd to exit when disabled commands are run.",
@@ -3671,6 +3676,11 @@
36713676
],
36723677
"fuzzy": true
36733678
},
3679+
{
3680+
"id": "Server name override is not supported with the current authentication method",
3681+
"message": "Server name override is not supported with the current authentication method",
3682+
"translation": ""
3683+
},
36743684
{
36753685
"id": "Password:",
36763686
"message": "Password:",

0 commit comments

Comments
 (0)