Skip to content

[fp-enhancer] Improve pkg/cli: use sliceutil.Filter/Map for functional slice operations#19705

Merged
pelikhan merged 2 commits intomainfrom
fp-enhancer/pkg-cli-round-1-81cdab3712c6e3b0
Mar 5, 2026
Merged

[fp-enhancer] Improve pkg/cli: use sliceutil.Filter/Map for functional slice operations#19705
pelikhan merged 2 commits intomainfrom
fp-enhancer/pkg-cli-round-1-81cdab3712c6e3b0

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Mar 5, 2026

Functional/Immutability Enhancements - Package: pkg/cli

Applies moderate, tasteful functional/immutability improvements to three files in pkg/cli, replacing imperative var + append patterns with declarative sliceutil.Filter and sliceutil.Map calls.

Round-Robin Progress: First pass over pkg/cli. Next run will process: pkg/console

Summary of Changes

Transformative Data Operations — 3 sites improved

1. pkg/cli/mcp_list_tools.gocompleteMCPListToolsArgs

Replaced a manual filter loop over commonMCPServerNames with sliceutil.Filter (the file already imported sliceutil):

// Before
var filtered []string
for _, s := range commonMCPServerNames {
    if toComplete == "" || strings.HasPrefix(s, toComplete) {
        filtered = append(filtered, s)
    }
}
return filtered, cobra.ShellCompDirectiveNoFileComp

// After
filtered := sliceutil.Filter(commonMCPServerNames, func(s string) bool {
    return toComplete == "" || strings.HasPrefix(s, toComplete)
})
return filtered, cobra.ShellCompDirectiveNoFileComp

2. pkg/cli/audit_report_analysis.gogenerateFindings

Replaced pre-allocation + imperative index loop for first-3 tool names with a single sliceutil.Map call on a capped slice (file already imported sliceutil):

// Before
toolNames := make([]string, 0, min(3, len(processedRun.MissingTools)))
for i := 0; i < len(processedRun.MissingTools) && i < 3; i++ {
    toolNames = append(toolNames, processedRun.MissingTools[i].Tool)
}

// After
toolNames := sliceutil.Map(processedRun.MissingTools[:min(3, len(processedRun.MissingTools))], func(t MissingToolReport) string { return t.Tool })

3. pkg/cli/firewall_log.goanalyzeFirewallLogs

Replaced a manual filter loop over glob results with sliceutil.Filter (adds sliceutil import):

// Before
var firewallLogs []string
for _, file := range files {
    basename := filepath.Base(file)
    if strings.Contains(basename, "firewall") ||
        (strings.Contains(basename, "access") && !strings.Contains(basename, "access-")) {
        firewallLogs = append(firewallLogs, file)
    }
}

// After
firewallLogs := sliceutil.Filter(files, func(file string) bool {
    basename := filepath.Base(file)
    return strings.Contains(basename, "firewall") ||
        (strings.Contains(basename, "access") && !strings.Contains(basename, "access-"))
})

Benefits

  • Immutability: All three sites eliminate var x []T mutation — values are now assigned once and not modified
  • Clarity: The predicate/transform is explicit and self-contained, making intent immediately clear
  • Consistency: Uses the existing sliceutil helpers already present in the file/codebase
  • No behavior change: Logic is identical; only the mechanism of collection changed

Testing

  • TestGenerateFindings / TestGenerateRecommendations — pass
  • TestParseFirewallLog* / TestAnalyzeFirewallLogs* — pass
  • make test-unit — all unit tests pass
  • make fmt — code formatted correctly
  • ✅ No behavioral changes — functionality is identical

References:

Generated by Functional Pragmatist ·

  • expires on Mar 6, 2026, 9:27 AM UTC

Replace imperative var+append loops with declarative sliceutil.Filter
and sliceutil.Map calls in pkg/cli.

- mcp_list_tools.go: completeMCPListToolsArgs uses sliceutil.Filter for
  filtering common MCP server names by completion prefix
- audit_report_analysis.go: generateFindings uses sliceutil.Map to
  transform MissingToolReport slice to string slice (first 3 tools)
- firewall_log.go: analyzeFirewallLogs uses sliceutil.Filter to select
  firewall log files by name pattern (adds sliceutil import)

All three changes reduce mutable state by replacing var+append patterns
with direct immutable assignments using the existing sliceutil helpers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review March 5, 2026 12:25
Copilot AI review requested due to automatic review settings March 5, 2026 12:25
@pelikhan pelikhan merged commit 79d2b50 into main Mar 5, 2026
49 checks passed
@pelikhan pelikhan deleted the fp-enhancer/pkg-cli-round-1-81cdab3712c6e3b0 branch March 5, 2026 12:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces three imperative var + append slice-building patterns with declarative sliceutil.Filter and sliceutil.Map calls in pkg/cli, improving code clarity and consistency with existing codebase patterns.

Changes:

  • Replaced manual filter loop in completeMCPListToolsArgs with sliceutil.Filter
  • Replaced manual filter loop in analyzeFirewallLogs with sliceutil.Filter (adding the sliceutil import)
  • Replaced manual index-bounded loop in generateFindings with sliceutil.Map on a capped slice

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/cli/mcp_list_tools.go Replaced imperative filter loop with sliceutil.Filter for MCP server name completion
pkg/cli/firewall_log.go Replaced imperative filter loop with sliceutil.Filter for firewall log file matching; added sliceutil import
pkg/cli/audit_report_analysis.go Replaced pre-allocated loop with sliceutil.Map on a capped slice for extracting tool names

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

for i := 0; i < len(processedRun.MissingTools) && i < 3; i++ {
toolNames = append(toolNames, processedRun.MissingTools[i].Tool)
}
toolNames := sliceutil.Map(processedRun.MissingTools[:min(3, len(processedRun.MissingTools))], func(t MissingToolReport) string { return t.Tool })
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sliceutil.Map call is written as a single long line (~150 chars), while the sliceutil.Map call just above on lines 94-96 in the same function uses a multi-line format with the callback body on its own line. For consistency within this function, consider breaking this into multiple lines to match the style of the Map call on lines 94-96.

Suggested change
toolNames := sliceutil.Map(processedRun.MissingTools[:min(3, len(processedRun.MissingTools))], func(t MissingToolReport) string { return t.Tool })
toolNames := sliceutil.Map(
processedRun.MissingTools[:min(3, len(processedRun.MissingTools))],
func(t MissingToolReport) string {
return t.Tool
},
)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants