Skip to content

Commit f241917

Browse files
Address Copilot review comments for serverlist command
- Use strings.TrimSpace in serverlistCommand for consistency - Fix test formatting to put each test case on its own line - Add InstanceName validation before using as map key - Document :serverlist command in README.md
1 parent ec0a3cd commit f241917

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ switches are most important to you to have implemented next in the new sqlcmd.
155155
- The new `--driver-logging-level` command line parameter allows you to see traces from the `go-mssqldb` client driver. Use `64` to see all traces.
156156
- Sqlcmd can now print results using a vertical format. Use the new `--vertical` command line option to set it. It's also controlled by the `SQLCMDFORMAT` scripting variable.
157157
- `:help` displays a list of available sqlcmd commands.
158+
- `:serverlist` lists local SQL Server instances discovered via the SQL Server Browser service.
158159

159160
```
160161
1> select session_id, client_interface_name, program_name from sys.dm_exec_sessions where session_id=@@spid

pkg/sqlcmd/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ go [<n>]
657657
}
658658

659659
func serverlistCommand(s *Sqlcmd, args []string, line uint) error {
660-
if len(args) > 0 && args[0] != "" {
660+
if len(args) > 0 && strings.TrimSpace(args[0]) != "" {
661661
return InvalidCommandError("SERVERLIST", line)
662662
}
663663
ListLocalServers(s.GetOutput())

pkg/sqlcmd/commands_test.go

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

6163
for _, test := range commands {
6264
cmd, args := c.matchCommand(test.line)

pkg/sqlcmd/serverlist.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func parseInstances(msg []byte) msdsn.BrowserData {
9494
if len(instanceDict) == 0 {
9595
break
9696
}
97-
results[strings.ToUpper(instanceDict["InstanceName"])] = instanceDict
97+
// Only add if InstanceName key exists and is non-empty
98+
if instName, ok := instanceDict["InstanceName"]; ok && instName != "" {
99+
results[strings.ToUpper(instName)] = instanceDict
100+
}
98101
instanceDict = map[string]string{}
99102
continue
100103
}

0 commit comments

Comments
 (0)