From 4de0ac6abdcfd245cd0ce382deb11720286863b1 Mon Sep 17 00:00:00 2001 From: stringsbuilder Date: Tue, 7 Apr 2026 12:07:50 +0800 Subject: [PATCH] refactor: replace Split in loops with more efficient SplitSeq and gofmt the code (#1888) Signed-off-by: stringsbuilder Co-authored-by: Alon Girmonsky <1990761+alongir@users.noreply.github.com> --- cmd/mcpRunner.go | 35 +++++++++++++++++------------------ cmd/mcp_test.go | 2 +- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/mcpRunner.go b/cmd/mcpRunner.go index ebe8c55bb..de318ee1d 100644 --- a/cmd/mcpRunner.go +++ b/cmd/mcpRunner.go @@ -86,9 +86,9 @@ type mcpContent struct { } type mcpPrompt struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - Arguments []mcpPromptArg `json:"arguments,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Arguments []mcpPromptArg `json:"arguments,omitempty"` } type mcpPromptArg struct { @@ -117,11 +117,11 @@ type mcpGetPromptResult struct { // Hub MCP API response types type hubMCPResponse struct { - Name string `json:"name"` - Description string `json:"description"` - Version string `json:"version"` - Tools []hubMCPTool `json:"tools"` - Prompts []hubMCPPrompt `json:"prompts"` + Name string `json:"name"` + Description string `json:"description"` + Version string `json:"version"` + Tools []hubMCPTool `json:"tools"` + Prompts []hubMCPPrompt `json:"prompts"` } type hubMCPTool struct { @@ -131,9 +131,9 @@ type hubMCPTool struct { } type hubMCPPrompt struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - Arguments []hubMCPPromptArg `json:"arguments,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Arguments []hubMCPPromptArg `json:"arguments,omitempty"` } type hubMCPPromptArg struct { @@ -151,10 +151,10 @@ type mcpServer struct { stdout io.Writer backendInitialized bool backendMu sync.Mutex - setFlags []string // --set flags to pass to 'kubeshark tap' when starting - directURL string // If set, connect directly to this URL (no kubectl/proxy) - urlMode bool // True when using direct URL mode - allowDestructive bool // If true, enable start/stop tools + setFlags []string // --set flags to pass to 'kubeshark tap' when starting + directURL string // If set, connect directly to this URL (no kubectl/proxy) + urlMode bool // True when using direct URL mode + allowDestructive bool // If true, enable start/stop tools cachedHubMCP *hubMCPResponse // Cached tools/prompts from Hub cachedAt time.Time // When the cache was populated hubMCPMu sync.Mutex @@ -772,7 +772,6 @@ func (s *mcpServer) callHubTool(toolName string, args map[string]any) (string, b return prettyJSON.String(), false } - func (s *mcpServer) callGetFileURL(args map[string]any) (string, bool) { filePath, _ := args["path"].(string) if filePath == "" { @@ -869,8 +868,8 @@ func (s *mcpServer) callStartKubeshark(args map[string]any) (string, bool) { // Add namespaces if provided if v, ok := args["namespaces"].(string); ok && v != "" { - namespaces := strings.Split(v, ",") - for _, ns := range namespaces { + namespaces := strings.SplitSeq(v, ",") + for ns := range namespaces { ns = strings.TrimSpace(ns) if ns != "" { cmdArgs = append(cmdArgs, "-n", ns) diff --git a/cmd/mcp_test.go b/cmd/mcp_test.go index 62096ed3c..78c1e2e1d 100644 --- a/cmd/mcp_test.go +++ b/cmd/mcp_test.go @@ -417,7 +417,7 @@ func TestMCP_CommandArgs(t *testing.T) { cmdArgs = append(cmdArgs, v) } if v, _ := tc.args["namespaces"].(string); v != "" { - for _, ns := range strings.Split(v, ",") { + for ns := range strings.SplitSeq(v, ",") { cmdArgs = append(cmdArgs, "-n", strings.TrimSpace(ns)) } }