Skip to content

Commit ec0a3cd

Browse files
Address Copilot review comments on serverlist command
- Fix fmt.Fprintf spacing in ListLocalServers - Sort instance names for deterministic output - Add validation for missing ServerName in instances - Add argument validation for helpCommand - Add test cases for :SERVERLIST and :serverlist - Add assertion for :serverlist in TestHelpCommand
1 parent 3412442 commit ec0a3cd

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

pkg/sqlcmd/commands.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ func xmlCommand(s *Sqlcmd, args []string, line uint) error {
608608

609609
// helpCommand displays the list of available sqlcmd commands
610610
func helpCommand(s *Sqlcmd, args []string, line uint) error {
611+
if len(args) > 0 && strings.TrimSpace(args[0]) != "" {
612+
return InvalidCommandError("HELP", line)
613+
}
611614
helpText := `:!! [<command>]
612615
- Executes a command in the operating system shell.
613616
:connect server[\instance] [-l timeout] [-U user [-P password]]

pkg/sqlcmd/commands_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func TestCommandParsing(t *testing.T) {
5555
{`:RESET`, "RESET", []string{""}},
5656
{`RESET`, "RESET", []string{""}},
5757
{`:HELP`, "HELP", []string{""}},
58-
{`:help`, "HELP", []string{""}},
59-
}
58+
{`:help`, "HELP", []string{""}}, {`:SERVERLIST`, "SERVERLIST", []string{""}},
59+
{`:serverlist`, "SERVERLIST", []string{""}}}
6060

6161
for _, test := range commands {
6262
cmd, args := c.matchCommand(test.line)
@@ -479,5 +479,6 @@ func TestHelpCommand(t *testing.T) {
479479
assert.Contains(t, output, ":out", "help should list :out")
480480
assert.Contains(t, output, ":error", "help should list :error")
481481
assert.Contains(t, output, ":r", "help should list :r")
482+
assert.Contains(t, output, ":serverlist", "help should list :serverlist")
482483
assert.Contains(t, output, "go", "help should list go")
483484
}

pkg/sqlcmd/serverlist.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net"
11+
"sort"
1112
"strings"
1213
"time"
1314

@@ -19,7 +20,7 @@ import (
1920
func ListLocalServers(w io.Writer) {
2021
instances := GetLocalServerInstances()
2122
for _, s := range instances {
22-
fmt.Fprintln(w, " ", s)
23+
fmt.Fprintf(w, " %s\n", s)
2324
}
2425
}
2526

@@ -52,11 +53,24 @@ func GetLocalServerInstances() []string {
5253

5354
data := parseInstances(resp[:read])
5455
instances := make([]string, 0, len(data))
56+
57+
// Sort instance names for deterministic output
58+
instanceNames := make([]string, 0, len(data))
5559
for s := range data {
60+
instanceNames = append(instanceNames, s)
61+
}
62+
sort.Strings(instanceNames)
63+
64+
for _, s := range instanceNames {
65+
serverName := data[s]["ServerName"]
66+
if serverName == "" {
67+
// Skip instances without a ServerName
68+
continue
69+
}
5670
if s == "MSSQLSERVER" {
57-
instances = append(instances, "(local)", data[s]["ServerName"])
71+
instances = append(instances, "(local)", serverName)
5872
} else {
59-
instances = append(instances, fmt.Sprintf(`%s\%s`, data[s]["ServerName"], s))
73+
instances = append(instances, fmt.Sprintf(`%s\%s`, serverName, s))
6074
}
6175
}
6276
return instances

0 commit comments

Comments
 (0)