feat(config): add validate command for sync config

Add `bd config validate` command to validate sync-related configuration:

- sync.mode: validates values (local, git-branch, external)
- conflict.strategy: validates values (lww, manual, ours, theirs)
- federation.sovereignty: validates values (none, isolated, federated)
- federation.remote: ensures set when sync.mode is 'external'
- Remote URL format: validates dolthub://, gs://, s3://, file://, etc.

Also validates existing config via doctor.CheckConfigValues (sync.branch,
routing.mode, etc.)

Closes: hq-ew1mbr.29

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
lizzy
2026-01-17 14:02:14 -08:00
committed by Steve Yegge
parent e82e15a8c2
commit 9ddd7a2620
2 changed files with 416 additions and 0 deletions

View File

@@ -3,10 +3,13 @@ package main
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/steveyegge/beads/cmd/bd/doctor"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/syncbranch"
)
@@ -268,10 +271,195 @@ var configUnsetCmd = &cobra.Command{
},
}
var configValidateCmd = &cobra.Command{
Use: "validate",
Short: "Validate sync-related configuration",
Long: `Validate sync-related configuration settings.
Checks:
- sync.mode is a valid value (local, git-branch, external)
- conflict.strategy is valid (lww, manual, ours, theirs)
- federation.sovereignty is valid (if set)
- federation.remote is set when sync.mode requires it
- Remote URL format is valid (dolthub://, gs://, s3://, file://)
- sync.branch is a valid git branch name
- routing.mode is valid (auto, maintainer, contributor)
Examples:
bd config validate
bd config validate --json`,
Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Find repo root by walking up to find .beads directory
repoPath := findBeadsRepoRoot(cwd)
if repoPath == "" {
fmt.Fprintf(os.Stderr, "Error: not in a beads repository (no .beads directory found)\n")
os.Exit(1)
}
// Run the existing doctor config values check
doctorCheck := doctor.CheckConfigValues(repoPath)
// Run additional sync-related validations
syncIssues := validateSyncConfig(repoPath)
// Combine results
allIssues := []string{}
if doctorCheck.Detail != "" {
allIssues = append(allIssues, strings.Split(doctorCheck.Detail, "\n")...)
}
allIssues = append(allIssues, syncIssues...)
// Output results
if jsonOutput {
result := map[string]interface{}{
"valid": len(allIssues) == 0,
"issues": allIssues,
}
outputJSON(result)
return
}
if len(allIssues) == 0 {
fmt.Println("✓ All sync-related configuration is valid")
return
}
fmt.Println("Configuration validation found issues:")
for _, issue := range allIssues {
if issue != "" {
fmt.Printf(" • %s\n", issue)
}
}
fmt.Println("\nRun 'bd config set <key> <value>' to fix configuration issues.")
os.Exit(1)
},
}
// validateSyncConfig performs additional sync-related config validation
// beyond what doctor.CheckConfigValues covers.
func validateSyncConfig(repoPath string) []string {
var issues []string
// Load config.yaml directly from the repo path
configPath := repoPath + "/.beads/config.yaml"
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile(configPath)
// Try to read config, but don't error if it doesn't exist
if err := v.ReadInConfig(); err != nil {
// Config file doesn't exist or is unreadable - nothing to validate
return issues
}
// Get config from yaml
syncMode := v.GetString("sync.mode")
conflictStrategy := v.GetString("conflict.strategy")
federationSov := v.GetString("federation.sovereignty")
federationRemote := v.GetString("federation.remote")
// Validate sync.mode
validSyncModes := map[string]bool{
"": true, // not set is valid (uses default)
"local": true,
"git-branch": true,
"external": true,
}
if syncMode != "" && !validSyncModes[syncMode] {
issues = append(issues, fmt.Sprintf("sync.mode: %q is invalid (valid values: local, git-branch, external)", syncMode))
}
// Validate conflict.strategy
validConflictStrategies := map[string]bool{
"": true, // not set is valid (uses default lww)
"lww": true, // last-write-wins (default)
"manual": true, // require manual resolution
"ours": true, // prefer local changes
"theirs": true, // prefer remote changes
}
if conflictStrategy != "" && !validConflictStrategies[conflictStrategy] {
issues = append(issues, fmt.Sprintf("conflict.strategy: %q is invalid (valid values: lww, manual, ours, theirs)", conflictStrategy))
}
// Validate federation.sovereignty
validSovereignties := map[string]bool{
"": true, // not set is valid
"none": true, // no sovereignty restrictions
"isolated": true, // fully isolated, no federation
"federated": true, // participates in federation
}
if federationSov != "" && !validSovereignties[federationSov] {
issues = append(issues, fmt.Sprintf("federation.sovereignty: %q is invalid (valid values: none, isolated, federated)", federationSov))
}
// Validate federation.remote when required
if syncMode == "external" && federationRemote == "" {
issues = append(issues, "federation.remote: required when sync.mode is 'external'")
}
// Validate remote URL format
if federationRemote != "" {
if !isValidRemoteURL(federationRemote) {
issues = append(issues, fmt.Sprintf("federation.remote: %q is not a valid remote URL (expected dolthub://, gs://, s3://, file://, or standard git URL)", federationRemote))
}
}
return issues
}
// isValidRemoteURL validates remote URL formats for sync configuration
func isValidRemoteURL(url string) bool {
// Valid URL schemes for beads remotes
validSchemes := []string{
"dolthub://",
"gs://",
"s3://",
"file://",
"https://",
"http://",
"ssh://",
}
for _, scheme := range validSchemes {
if strings.HasPrefix(url, scheme) {
return true
}
}
// Also allow standard git remote patterns (user@host:path)
// The host must have at least one character before the colon
// Pattern: username@hostname:path where hostname has at least 2 chars
gitSSHPattern := regexp.MustCompile(`^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9._-]*:.+$`)
return gitSSHPattern.MatchString(url)
}
// findBeadsRepoRoot walks up from the given path to find the repo root (containing .beads)
func findBeadsRepoRoot(startPath string) string {
path := startPath
for {
beadsDir := path + "/.beads"
if info, err := os.Stat(beadsDir); err == nil && info.IsDir() {
return path
}
parent := path[:strings.LastIndex(path, "/")]
if parent == path || parent == "" {
return ""
}
path = parent
}
}
func init() {
configCmd.AddCommand(configSetCmd)
configCmd.AddCommand(configGetCmd)
configCmd.AddCommand(configListCmd)
configCmd.AddCommand(configUnsetCmd)
configCmd.AddCommand(configValidateCmd)
rootCmd.AddCommand(configCmd)
}