Refactor: extract duplicated validation and flag logic (bd-g5p7)
- Created internal/validation package for centralized validation logic - Created cmd/bd/flags.go for shared flag registration - Updated create and update commands to use shared logic - Added support for 'P1' style priority to update command - Added tests for validation logic Amp-Thread-ID: https://ampcode.com/threads/T-c8d369a3-32f0-42a0-96d1-fd589e89bd6b Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
47
internal/validation/bead.go
Normal file
47
internal/validation/bead.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// ParsePriority extracts and validates a priority value from content.
|
||||
// Supports both numeric (0-4) and P-prefix format (P0-P4).
|
||||
// Returns the parsed priority (0-4) or -1 if invalid.
|
||||
func ParsePriority(content string) int {
|
||||
content = strings.TrimSpace(content)
|
||||
|
||||
// Handle "P1", "P0", etc. format
|
||||
if strings.HasPrefix(strings.ToUpper(content), "P") {
|
||||
content = content[1:] // Strip the "P" prefix
|
||||
}
|
||||
|
||||
var p int
|
||||
if _, err := fmt.Sscanf(content, "%d", &p); err == nil && p >= 0 && p <= 4 {
|
||||
return p
|
||||
}
|
||||
return -1 // Invalid
|
||||
}
|
||||
|
||||
// ParseIssueType extracts and validates an issue type from content.
|
||||
// Returns the validated type or error if invalid.
|
||||
func ParseIssueType(content string) (types.IssueType, error) {
|
||||
issueType := types.IssueType(strings.TrimSpace(content))
|
||||
|
||||
// Validate issue type
|
||||
validTypes := map[types.IssueType]bool{
|
||||
types.TypeBug: true,
|
||||
types.TypeFeature: true,
|
||||
types.TypeTask: true,
|
||||
types.TypeEpic: true,
|
||||
types.TypeChore: true,
|
||||
}
|
||||
|
||||
if !validTypes[issueType] {
|
||||
return types.TypeTask, fmt.Errorf("invalid issue type: %s", content)
|
||||
}
|
||||
|
||||
return issueType, nil
|
||||
}
|
||||
Reference in New Issue
Block a user