Skip to content

Commit 6e597cc

Browse files
Refactor GenerateToolsetsHelp() to use strings.Builder pattern
Co-authored-by: SamMorrowDrums <[email protected]>
1 parent cdc281f commit 6e597cc

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

pkg/github/tools.go

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package github
22

33
import (
44
"context"
5-
"fmt"
65
"strings"
76

87
"github.com/github/github-mcp-server/pkg/registry"
@@ -266,17 +265,19 @@ func GenerateToolsetsHelp() string {
266265
// Get toolset group to derive defaults and available toolsets
267266
r := NewRegistry(stubTranslator).Build()
268267

269-
// Format default tools from metadata
268+
// Format default tools from metadata using strings.Builder
269+
var defaultBuf strings.Builder
270270
defaultIDs := r.DefaultToolsetIDs()
271-
defaultStrings := make([]string, len(defaultIDs))
272271
for i, id := range defaultIDs {
273-
defaultStrings[i] = string(id)
272+
if i > 0 {
273+
defaultBuf.WriteString(", ")
274+
}
275+
defaultBuf.WriteString(string(id))
274276
}
275-
defaultTools := strings.Join(defaultStrings, ", ")
276277

277278
// Get all available toolsets (excludes context and dynamic for display)
278279
allToolsets := r.AvailableToolsets("context", "dynamic")
279-
var availableToolsLines []string
280+
var availableBuf strings.Builder
280281
const maxLineLength = 70
281282
currentLine := ""
282283

@@ -288,27 +289,37 @@ func GenerateToolsetsHelp() string {
288289
case len(currentLine)+len(id)+2 <= maxLineLength:
289290
currentLine += ", " + id
290291
default:
291-
availableToolsLines = append(availableToolsLines, currentLine)
292+
if availableBuf.Len() > 0 {
293+
availableBuf.WriteString(",\n\t ")
294+
}
295+
availableBuf.WriteString(currentLine)
292296
currentLine = id
293297
}
294298
}
295299
if currentLine != "" {
296-
availableToolsLines = append(availableToolsLines, currentLine)
297-
}
298-
299-
availableTools := strings.Join(availableToolsLines, ",\n\t ")
300-
301-
toolsetsHelp := fmt.Sprintf("Comma-separated list of tool groups to enable (no spaces).\n"+
302-
"Available: %s\n", availableTools) +
303-
"Special toolset keywords:\n" +
304-
" - all: Enables all available toolsets\n" +
305-
fmt.Sprintf(" - default: Enables the default toolset configuration of:\n\t %s\n", defaultTools) +
306-
"Examples:\n" +
307-
" - --toolsets=actions,gists,notifications\n" +
308-
" - Default + additional: --toolsets=default,actions,gists\n" +
309-
" - All tools: --toolsets=all"
310-
311-
return toolsetsHelp
300+
if availableBuf.Len() > 0 {
301+
availableBuf.WriteString(",\n\t ")
302+
}
303+
availableBuf.WriteString(currentLine)
304+
}
305+
306+
// Build the complete help text using strings.Builder
307+
var buf strings.Builder
308+
buf.WriteString("Comma-separated list of tool groups to enable (no spaces).\n")
309+
buf.WriteString("Available: ")
310+
buf.WriteString(availableBuf.String())
311+
buf.WriteString("\n")
312+
buf.WriteString("Special toolset keywords:\n")
313+
buf.WriteString(" - all: Enables all available toolsets\n")
314+
buf.WriteString(" - default: Enables the default toolset configuration of:\n\t ")
315+
buf.WriteString(defaultBuf.String())
316+
buf.WriteString("\n")
317+
buf.WriteString("Examples:\n")
318+
buf.WriteString(" - --toolsets=actions,gists,notifications\n")
319+
buf.WriteString(" - Default + additional: --toolsets=default,actions,gists\n")
320+
buf.WriteString(" - All tools: --toolsets=all")
321+
322+
return buf.String()
312323
}
313324

314325
// stubTranslator is a passthrough translator for cases where we need a Registry

pkg/github/tools_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,34 @@ func TestContainsToolset(t *testing.T) {
151151
})
152152
}
153153
}
154+
155+
func TestGenerateToolsetsHelp(t *testing.T) {
156+
// Generate the help text
157+
helpText := GenerateToolsetsHelp()
158+
159+
// Verify help text is not empty
160+
require.NotEmpty(t, helpText)
161+
162+
// Verify it contains expected sections
163+
assert.Contains(t, helpText, "Comma-separated list of tool groups to enable")
164+
assert.Contains(t, helpText, "Available:")
165+
assert.Contains(t, helpText, "Special toolset keywords:")
166+
assert.Contains(t, helpText, "all: Enables all available toolsets")
167+
assert.Contains(t, helpText, "default: Enables the default toolset configuration")
168+
assert.Contains(t, helpText, "Examples:")
169+
assert.Contains(t, helpText, "--toolsets=actions,gists,notifications")
170+
assert.Contains(t, helpText, "--toolsets=default,actions,gists")
171+
assert.Contains(t, helpText, "--toolsets=all")
172+
173+
// Verify it contains some expected default toolsets
174+
assert.Contains(t, helpText, "context")
175+
assert.Contains(t, helpText, "repos")
176+
assert.Contains(t, helpText, "issues")
177+
assert.Contains(t, helpText, "pull_requests")
178+
assert.Contains(t, helpText, "users")
179+
180+
// Verify it contains some expected available toolsets
181+
assert.Contains(t, helpText, "actions")
182+
assert.Contains(t, helpText, "gists")
183+
assert.Contains(t, helpText, "notifications")
184+
}

0 commit comments

Comments
 (0)