Skip to content

Commit 78564f8

Browse files
refactor(cli): apply immutability improvements to pkg/cli (#18105)
1 parent bfa05d0 commit 78564f8

3 files changed

Lines changed: 15 additions & 29 deletions

File tree

pkg/cli/completions.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/github/gh-aw/pkg/fileutil"
99
"github.com/github/gh-aw/pkg/logger"
1010
"github.com/github/gh-aw/pkg/parser"
11+
"github.com/github/gh-aw/pkg/sliceutil"
1112
"github.com/github/gh-aw/pkg/workflow"
1213
"github.com/spf13/cobra"
1314
)
@@ -109,12 +110,9 @@ func CompleteEngineNames(cmd *cobra.Command, args []string, toComplete string) (
109110

110111
engines := ValidEngineNames()
111112

112-
var filtered []string
113-
for _, engine := range engines {
114-
if toComplete == "" || strings.HasPrefix(engine, toComplete) {
115-
filtered = append(filtered, engine)
116-
}
117-
}
113+
filtered := sliceutil.Filter(engines, func(engine string) bool {
114+
return toComplete == "" || strings.HasPrefix(engine, toComplete)
115+
})
118116

119117
completionsLog.Printf("Found %d matching engines", len(filtered))
120118
return filtered, cobra.ShellCompDirectiveNoFileComp
@@ -145,12 +143,12 @@ func CompleteMCPServerNames(workflowFile string) func(cmd *cobra.Command, args [
145143
return nil, cobra.ShellCompDirectiveNoFileComp
146144
}
147145

148-
var servers []string
149-
for _, config := range mcpConfigs {
150-
if toComplete == "" || strings.HasPrefix(config.Name, toComplete) {
151-
servers = append(servers, config.Name)
152-
}
153-
}
146+
servers := sliceutil.FilterMap(mcpConfigs,
147+
func(config parser.MCPServerConfig) bool {
148+
return toComplete == "" || strings.HasPrefix(config.Name, toComplete)
149+
},
150+
func(config parser.MCPServerConfig) string { return config.Name },
151+
)
154152

155153
completionsLog.Printf("Found %d matching MCP servers", len(servers))
156154
return servers, cobra.ShellCompDirectiveNoFileComp

pkg/cli/gateway_logs.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/github/gh-aw/pkg/console"
2626
"github.com/github/gh-aw/pkg/logger"
27+
"github.com/github/gh-aw/pkg/sliceutil"
2728
"github.com/github/gh-aw/pkg/stringutil"
2829
"github.com/github/gh-aw/pkg/timeutil"
2930
)
@@ -517,13 +518,7 @@ func renderGatewayMetricsTable(metrics *GatewayMetrics, verbose bool) string {
517518
output.WriteString("├────────────────────────────┼──────────┼────────────┼───────────┼────────┤\n")
518519

519520
// Sort servers by request count
520-
var serverNames []string
521-
for name := range metrics.Servers {
522-
serverNames = append(serverNames, name)
523-
}
524-
sort.Slice(serverNames, func(i, j int) bool {
525-
return metrics.Servers[serverNames[i]].RequestCount > metrics.Servers[serverNames[j]].RequestCount
526-
})
521+
serverNames := getSortedServerNames(metrics)
527522

528523
for _, serverName := range serverNames {
529524
server := metrics.Servers[serverName]
@@ -560,10 +555,7 @@ func renderGatewayMetricsTable(metrics *GatewayMetrics, verbose bool) string {
560555
output.WriteString("├──────────────────────────┼───────┼──────────┼──────────┼──────────┤\n")
561556

562557
// Sort tools by call count
563-
var toolNames []string
564-
for name := range server.Tools {
565-
toolNames = append(toolNames, name)
566-
}
558+
toolNames := sliceutil.MapToSlice(server.Tools)
567559
sort.Slice(toolNames, func(i, j int) bool {
568560
return server.Tools[toolNames[i]].CallCount > server.Tools[toolNames[j]].CallCount
569561
})
@@ -587,10 +579,7 @@ func renderGatewayMetricsTable(metrics *GatewayMetrics, verbose bool) string {
587579

588580
// getSortedServerNames returns server names sorted by request count
589581
func getSortedServerNames(metrics *GatewayMetrics) []string {
590-
var names []string
591-
for name := range metrics.Servers {
592-
names = append(names, name)
593-
}
582+
names := sliceutil.MapToSlice(metrics.Servers)
594583
sort.Slice(names, func(i, j int) bool {
595584
return metrics.Servers[names[i]].RequestCount > metrics.Servers[names[j]].RequestCount
596585
})

pkg/cli/run_interactive.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ func RunSpecificWorkflowInteractively(ctx context.Context, workflowName string,
439439

440440
// buildCommandString builds the equivalent command string for display
441441
func buildCommandString(workflowName string, inputs []string, repoOverride, refOverride string, autoMergePRs, push bool, engineOverride string) string {
442-
var parts []string
443-
parts = append(parts, string(constants.CLIExtensionPrefix), "run", workflowName)
442+
parts := []string{string(constants.CLIExtensionPrefix), "run", workflowName}
444443

445444
// Add inputs
446445
for _, input := range inputs {

0 commit comments

Comments
 (0)