Skip to content

add file support for port#1559

Merged
Mzack9999 merged 1 commit intodevfrom
1558_support_port_file
Oct 6, 2025
Merged

add file support for port#1559
Mzack9999 merged 1 commit intodevfrom
1558_support_port_file

Conversation

@dogancanbakir
Copy link
Copy Markdown
Member

@dogancanbakir dogancanbakir commented Sep 26, 2025

closes #1558

Summary by CodeRabbit

  • New Features

    • Ports input now supports multiple sources: provide one or more files and/or comma-separated lists for --ports-file and --exclude-ports.
    • Combine inputs flexibly (multiple flags and mixed formats) to build comprehensive include/exclude port sets.
  • Documentation

    • Updated flag help text to clarify that values can come from files and/or comma-separated lists.

@dogancanbakir dogancanbakir self-assigned this Sep 26, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Sep 26, 2025

Walkthrough

Ports-related CLI options transition from single strings to goflags.StringSlice. Flag registration updates to accept file or comma-separated inputs. Port loading logic now parses slices (including file-expanded values) and removes direct os.ReadFile usage. Exclusion ports parsing aligns with the new slice-based approach. Tests updated accordingly.

Changes

Cohort / File(s) Summary
CLI options schema and flags
pkg/runner/options.go
Changed Options.PortsFile and Options.ExcludePorts from string to goflags.StringSlice. Updated ParseOptions to use StringSliceVarP with goflags.FileCommaSeparatedStringSliceOptions. Descriptions updated to reflect file or comma-separated inputs.
Ports loading and parsing
pkg/runner/ports.go
Replaced file-based read with slice-based parsePortsSlice for both ports and exclude-ports. Presence checks now use slice length. Removed os import.
Tests adaptation
pkg/runner/ports_test.go
Adjusted tests to construct Options.ExcludePorts as goflags.StringSlice{...}. Added goflags import.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant CLI as CLI User
  participant Flags as goflags Parser
  participant Runner as runner.ParseOptions
  participant Ports as runner.loadPorts

  CLI->>Flags: Provide --ports-file / --exclude-ports<br/>(file or comma-separated)
  Note over Flags: Handles file expansion + splitting into slices
  Flags-->>Runner: Options{PortsFile:StringSlice, ExcludePorts:StringSlice}

  Runner->>Ports: parsePortsSlice(Options.PortsFile)
  Ports-->>Runner: []int ports

  Runner->>Ports: parsePortsSlice(Options.ExcludePorts)
  Ports-->>Runner: []int excluded

  Runner->>Runner: Apply exclusions and proceed
  Note over Runner: No direct file I/O in ports.go
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I thump my paws at flags made neat,
From single string to slices sweet.
No OS nibble, just parse with ease,
Files or commas—either please.
Ports now hop in tidy rows,
Excludes follow where rabbit goes.
Bug burrowed out—onward it flows! 🐇🚀

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Out of Scope Changes Check ⚠️ Warning While the changes correctly restore file support for the ports-file option, the pull request also refactors ExcludePorts from a string to a goflags.StringSlice and updates related tests, which was not requested in issue #1558 and is unrelated to fixing the –pf/–ports-file functionality. To keep this PR focused and aligned with issue #1558, revert or remove the ExcludePorts type change and its test modifications or split that refactoring into a separate pull request.
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title concisely indicates the primary change of adding file support for ports flags by focusing on file-based input for ports, matching the core alterations to the ports-file option, though the singular form “port” is slightly awkward.
Linked Issues Check ✅ Passed The pull request updates the PortsFile flag binding to use a StringSlice with file-or-CSV parsing options and revises the ports-loading logic to iterate over the provided slice, thereby restoring the functionality of the –pf/–ports-file option as described in issue #1558.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 1558_support_port_file

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/runner/ports.go (1)

27-36: Harden parsing: prevent 0/negative ports (critical).

Switching to parsePortsSlice for ports-file means inputs like "0", "-1", or "-1-2" will be accepted and even expanded, producing invalid ports. Enforce 1 ≤ port ≤ 65535 for single ports and ranges.

Also wrap underlying errors with %w for better context.

Apply:

-        ports, err := parsePortsSlice(options.PortsFile)
+        ports, err := parsePortsSlice(options.PortsFile)
         if err != nil {
-            return nil, fmt.Errorf("could not read ports: %s", err)
+            return nil, fmt.Errorf("could not read ports: %w", err)
         }
         portsFileMap, err = excludePorts(options, ports)
         if err != nil {
-            return nil, fmt.Errorf("could not read ports: %s", err)
+            return nil, fmt.Errorf("could not read ports: %w", err)
         }

And update parsePortsSlice to validate bounds:

 func parsePortsSlice(ranges []string) ([]*port.Port, error) {
@@
-        if strings.Contains(r, "-") {
+        if strings.Contains(r, "-") {
             parts := strings.Split(r, "-")
             if len(parts) != portListStrParts {
                 return nil, fmt.Errorf("invalid port selection segment: '%s'", r)
             }
 
             p1, err := strconv.Atoi(parts[0])
             if err != nil {
                 return nil, fmt.Errorf("invalid port number: '%s'", parts[0])
             }
 
             p2, err := strconv.Atoi(parts[1])
             if err != nil {
                 return nil, fmt.Errorf("invalid port number: '%s'", parts[1])
             }
 
-            if p1 > p2 || p2 > 65535 {
-                return nil, fmt.Errorf("invalid port range: %d-%d", p1, p2)
-            }
+            if p1 < 1 || p2 < 1 || p1 > p2 || p2 > 65535 {
+                return nil, fmt.Errorf("invalid port range: %d-%d", p1, p2)
+            }
 
             for i := p1; i <= p2; i++ {
                 port := &port.Port{Port: i, Protocol: portProtocol}
                 ports = append(ports, port)
             }
         } else {
             portNumber, err := strconv.Atoi(r)
-            if err != nil || portNumber > 65535 {
+            if err != nil || portNumber < 1 || portNumber > 65535 {
                 return nil, fmt.Errorf("invalid port number: '%s'", r)
             }
             port := &port.Port{Port: portNumber, Protocol: portProtocol}
             ports = append(ports, port)
         }
🧹 Nitpick comments (1)
pkg/runner/ports_test.go (1)

51-57: Add tests for UDP, ranges, and file-expanded inputs.

Now that exclusions/ports file go through slice parsing, add coverage for:

  • UDP prefix (e.g., "u:53", "u:53-55")
  • Invalid numbers (e.g., "0", "-1", "-1-2") to confirm they error out once parse is hardened
  • File-expanded values when using FileCommaSeparatedStringSliceOptions

I can draft these test cases if you want.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f9ad2b and a264449.

📒 Files selected for processing (3)
  • pkg/runner/options.go (2 hunks)
  • pkg/runner/ports.go (2 hunks)
  • pkg/runner/ports_test.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
pkg/runner/ports.go (1)
pkg/port/port.go (1)
  • Port (10-17)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: build-mac
  • GitHub Check: release-test-mac
  • GitHub Check: build-linux
  • GitHub Check: build-windows
  • GitHub Check: release-test-windows
  • GitHub Check: Functional Test (ubuntu-latest)
  • GitHub Check: Lint Test
  • GitHub Check: Analyze (go)
🔇 Additional comments (3)
pkg/runner/ports_test.go (1)

7-7: Import change looks good.

Aligns tests with updated API using goflags.StringSlice.

pkg/runner/options.go (2)

155-157: Flag wiring looks correct.

Using FileCommaSeparatedStringSliceOptions on -ep/-pf aligns with the new parsing path.


57-59: API change applied; no string expectations remain Ensure downstreams consume goflags.StringSlice for PortsFile and ExcludePorts.

@Mzack9999 Mzack9999 merged commit f0618a8 into dev Oct 6, 2025
13 checks passed
@Mzack9999 Mzack9999 deleted the 1558_support_port_file branch October 6, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

the -pf -ports-file=ppo not work

2 participants