Add bracket expansion feature for target specification#53
Merged
Conversation
## 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.
9ca8078 to
cb34ba0
Compare
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 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
[1-10],[01-05](with intelligent zero-padding)[a-z],[A-Z][web,api,db]server[1-3].[dev,prod].example.com🔧 Technical Implementation
File/Args → Bracket Expansion → CIDR Expansion-foption)🎮 User Experience
[]instead of{}to differentiate from bash brace expansion[1-10]→ 1, 2, 3, ..., 10[01-10]→ 01, 02, 03, ..., 10[001-100]→ 001, 002, ..., 100🏗️ Use Cases
Infrastructure Monitoring
Service Health Monitoring
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
Test Categories
📊 Performance Impact
🔄 Backward Compatibility
📚 Documentation
🎛️ Configuration Examples
Development Environments
Production Monitoring
Infrastructure Validation
This feature significantly enhances mping's usability for enterprise-scale network monitoring and infrastructure management scenarios.
🏁 Ready for Merge