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:
Steve Yegge
2025-11-20 19:11:27 -05:00
parent e9f5a0c35a
commit bbfedb060a
7 changed files with 102 additions and 59 deletions

View 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
}

View File

@@ -0,0 +1,52 @@
package validation
import (
"testing"
)
func TestParsePriority(t *testing.T) {
tests := []struct {
input string
expected int
}{
// Numeric format
{"0", 0},
{"1", 1},
{"2", 2},
{"3", 3},
{"4", 4},
// P-prefix format (uppercase)
{"P0", 0},
{"P1", 1},
{"P2", 2},
{"P3", 3},
{"P4", 4},
// P-prefix format (lowercase)
{"p0", 0},
{"p1", 1},
{"p2", 2},
// With whitespace
{" 1 ", 1},
{" P1 ", 1},
// Invalid cases (returns -1)
{"5", -1}, // Out of range
{"-1", -1}, // Negative
{"P5", -1}, // Out of range with prefix
{"abc", -1}, // Not a number
{"P", -1}, // Just the prefix
{"PP1", -1}, // Double prefix
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := ParsePriority(tt.input)
if got != tt.expected {
t.Errorf("ParsePriority(%q) = %d, want %d", tt.input, got, tt.expected)
}
})
}
}