Skip to content

Add bracket expansion feature for target specification#53

Merged
servak merged 6 commits intomainfrom
feature/bracket-expansion
Jul 21, 2025
Merged

Add bracket expansion feature for target specification#53
servak merged 6 commits intomainfrom
feature/bracket-expansion

Conversation

@servak
Copy link
Owner

@servak servak commented Jul 21, 2025

🎯 Feature Overview

This PR introduces bracket expansion syntax for target specification, enabling efficient monitoring of large numbers of hosts with pattern-based notation.

✨ Key Features

📋 Expansion Syntax

  • Numeric ranges: [1-10], [01-05] (with intelligent zero-padding)
  • Character ranges: [a-z], [A-Z]
  • Lists: [web,api,db]
  • Multiple brackets: server[1-3].[dev,prod].example.com

🔧 Technical Implementation

  • Integrated into unified processing flow: File/Args → Bracket Expansion → CIDR Expansion
  • Works with both command-line arguments and file input (-f option)
  • Cross-platform compatibility (Windows, macOS, Linux)
  • Shell-independent operation (works in files where shell expansion doesn't)

🎮 User Experience

  • Intuitive syntax: Uses [] instead of {} to differentiate from bash brace expansion
  • Zero-padding intelligence: Automatically detects format from start value
    • [1-10] → 1, 2, 3, ..., 10
    • [01-10] → 01, 02, 03, ..., 10
    • [001-100] → 001, 002, ..., 100
  • Error handling: Invalid ranges preserved as-is for graceful degradation

🏗️ Use Cases

Infrastructure Monitoring

# Monitor entire server farm
mping web[01-20].prod.example.com

# Multi-tier architecture
mping server[1-5].[web,api,db].datacenter.com

# Network equipment across racks
mping switch[01-48].rack[a-f].datacenter.internal

Service Health Monitoring

# API endpoints across regions
mping https://api[1-3].[us,eu,ap].service.com/health

# Load balancer health checks
mping tcp://lb[1-4].cluster.internal:80

File-based Batch Operations

# targets.txt
web[01-05].prod.example.com
db[01-02].[master,replica].prod.example.com
https://monitor[1-3].ops.example.com/health

mping -f targets.txt

🧪 Quality Assurance

Comprehensive Test Coverage

  • 55 test cases covering all expansion types
  • Integration tests for combined bracket + CIDR expansion
  • File I/O testing with temporary files
  • Error case validation for invalid patterns
  • Legacy functionality preserved (100% backward compatibility)

Test Categories

  • Numeric range expansion (basic, zero-padded, three-digit)
  • Character range expansion (lowercase, uppercase)
  • List expansion (simple, with spaces, mixed content)
  • Multiple bracket combinations
  • Protocol URL support
  • Invalid input handling
  • File-based expansion
  • CIDR + bracket combination

📊 Performance Impact

  • Minimal overhead: Processing occurs once during target parsing
  • Memory efficient: Expansion happens before probe initialization
  • Concurrent friendly: Expanded targets distributed across probe workers

🔄 Backward Compatibility

  • 100% compatible: All existing functionality preserved
  • Non-breaking: No changes to existing APIs or behavior
  • Additive: Pure feature addition with no side effects

📚 Documentation

  • Comprehensive README section with examples and use cases
  • Inline code documentation for all new functions
  • Integration examples showing real-world usage patterns

🎛️ Configuration Examples

Development Environments

mping app[dev,staging,prod].company.com

Production Monitoring

mping https://[web,api,cdn][1-5].[us-west,eu-central].example.com

Infrastructure Validation

mping db[01-10].cluster[a-c].datacenter.com 10.0.[1-3].0/24

This feature significantly enhances mping's usability for enterprise-scale network monitoring and infrastructure management scenarios.

🏁 Ready for Merge

  • ✅ All tests passing
  • ✅ Documentation complete
  • ✅ Zero breaking changes
  • ✅ Feature-complete implementation

servak added 4 commits July 22, 2025 05:54
## Features Added

### Bracket Expansion Syntax
- **Numeric ranges**: `[1-10]`, `[01-05]` (with zero-padding support)
- **Character ranges**: `[a-z]`, `[A-Z]`
- **Lists**: `[web,db,cache]`
- **Multiple brackets**: `server[1-3].[dev,prod].example.com`
- **Protocol support**: `https://api[1-5].example.com`

### Processing Flow
```
File/Args → Bracket Expansion → CIDR Expansion → ProbeManager
```

### Zero-Padding Intelligence
- `[1-10]` → 1, 2, 3, ..., 10 (no padding)
- `[01-10]` → 01, 02, 03, ..., 10 (2-digit padding)
- `[001-100]` → 001, 002, ..., 100 (3-digit padding)

### Error Handling
- Invalid ranges preserved as-is: `[10-5]` → `[10-5]`
- Malformed syntax handled gracefully
- Mixed valid/invalid input supported

## Implementation Details

### Core Functions
- `parseBrackets()`: Main bracket expansion processor
- `expandHost()`: Multi-bracket combination handler
- `expandRange()`: Numeric/character range expansion
- `expandList()`: Comma-separated list expansion
- `expandCharRange()`: Character sequence generation

### Integration
- Integrated into `parseHostnames()` before CIDR expansion
- Works with both command-line args and file input (`-f` option)
- Maintains backward compatibility with existing functionality

## Testing
- Comprehensive test suite covering all expansion types
- Integration tests for combined bracket + CIDR expansion
- File I/O testing with temporary files
- Error case validation
- IPv4/IPv6 CIDR expansion testing
- Comment parsing and URL preservation tests

## Use Cases
- **Development**: `server[1-5].[dev,staging,prod].example.com`
- **Infrastructure**: `db[01-10].cluster.internal`
- **Monitoring**: `https://api[1-3].service.com/health`
- **Batch operations**: File-based target lists with patterns

## Differentiation from Shell Expansion
- Uses `[]` instead of `{}` to avoid confusion with bash brace expansion
- Cross-platform compatibility (Windows, macOS, Linux)
- Works in files where shell expansion doesn't apply
- Explicit, documented syntax independent of shell capabilities

This feature significantly improves mping's usability for large-scale network monitoring scenarios.
- Add detailed Target Expansion section to README
- Cover all bracket expansion features: numeric ranges, character ranges, lists
- Include zero-padding documentation with examples
- Document file-based expansion usage with -f option
- Show combination with CIDR ranges
- Provide real-world infrastructure monitoring examples
- Highlight cross-platform and shell-independent benefits
- Replace \!(a && b) with (\!a || \!b) for better readability
- Fixes golangci-lint staticcheck QF1001 warning
- No functional changes, all tests still pass
- Overall command package coverage: 22.6% → 35.1% (+55%)
- All util.go functions now have 95-100% coverage

- **TestParseCidr**: IPv4/IPv6 CIDR expansion with edge cases
- **TestIpInc**: IP address increment functionality
- **TestFile2hostnames**: File parsing with comments, URLs, protocols
- **TestParseHostnames**: Integration testing of all parsing steps
- **TestParseHostnamesIntegration**: End-to-end workflow testing

- Comprehensive edge case coverage
- Temporary file handling for file-based tests
- IPv4 and IPv6 CIDR notation testing
- Comment parsing and URL preservation
- Protocol prefix support validation
- Error handling for invalid inputs
- Integration testing of bracket + CIDR expansion

- All 75+ test cases passing
- Zero-padding validation
- Character range testing (a-z, A-Z)
- File I/O error handling
- Mixed valid/invalid input scenarios

This brings the util functions from 0% to near-perfect test coverage,
ensuring reliability for the bracket expansion feature and existing
network target processing functionality.
@servak servak force-pushed the feature/bracket-expansion branch from 9ca8078 to cb34ba0 Compare July 21, 2025 21:12
servak added 2 commits July 22, 2025 06:23
- Rename parseHostnames to ExpandTargets for better clarity
- Rename parseCidr to parseCIDR for proper naming convention
- Extract collectTargets function to separate concerns
- Add comprehensive tests for new collectTargets function
- Update test function names to match refactored functions
@servak servak merged commit a5f3150 into main Jul 21, 2025
4 checks passed
@servak servak deleted the feature/bracket-expansion branch July 21, 2025 21:30
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.

1 participant