feat: add multi-prefix support via allowed_prefixes config (#881)
feat: add multi-prefix support via allowed_prefixes config Adds support for allowing multiple prefixes in a beads database via the allowed_prefixes config key. This is needed for Gas Town which uses different prefixes for different purposes (hq-, gt-, rig-specific prefixes). Usage: bd config set allowed_prefixes "gt,hq,hmc" PR #881 by @web3dev1337
This commit is contained in:
@@ -72,10 +72,36 @@ func ValidateIDFormat(id string) (string, error) {
|
||||
// ValidatePrefix checks that the requested prefix matches the database prefix.
|
||||
// Returns an error if they don't match (unless force is true).
|
||||
func ValidatePrefix(requestedPrefix, dbPrefix string, force bool) error {
|
||||
return ValidatePrefixWithAllowed(requestedPrefix, dbPrefix, "", force)
|
||||
}
|
||||
|
||||
// ValidatePrefixWithAllowed checks that the requested prefix is allowed.
|
||||
// It matches if:
|
||||
// - force is true
|
||||
// - dbPrefix is empty
|
||||
// - requestedPrefix matches dbPrefix
|
||||
// - requestedPrefix is in the comma-separated allowedPrefixes list
|
||||
// Returns an error if none of these conditions are met.
|
||||
func ValidatePrefixWithAllowed(requestedPrefix, dbPrefix, allowedPrefixes string, force bool) error {
|
||||
if force || dbPrefix == "" || dbPrefix == requestedPrefix {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if requestedPrefix is in the allowed list
|
||||
if allowedPrefixes != "" {
|
||||
for _, allowed := range strings.Split(allowedPrefixes, ",") {
|
||||
allowed = strings.TrimSpace(allowed)
|
||||
if allowed == requestedPrefix {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build helpful error message
|
||||
if allowedPrefixes != "" {
|
||||
return fmt.Errorf("prefix mismatch: database uses '%s' (allowed: %s) but you specified '%s' (use --force to override)",
|
||||
dbPrefix, allowedPrefixes, requestedPrefix)
|
||||
}
|
||||
return fmt.Errorf("prefix mismatch: database uses '%s' but you specified '%s' (use --force to override)", dbPrefix, requestedPrefix)
|
||||
}
|
||||
|
||||
|
||||
@@ -195,6 +195,43 @@ func TestValidatePrefix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePrefixWithAllowed(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requestedPrefix string
|
||||
dbPrefix string
|
||||
allowedPrefixes string
|
||||
force bool
|
||||
wantError bool
|
||||
}{
|
||||
// Basic cases (same as ValidatePrefix)
|
||||
{"matching prefixes", "bd", "bd", "", false, false},
|
||||
{"empty db prefix", "bd", "", "", false, false},
|
||||
{"mismatched with force", "foo", "bd", "", true, false},
|
||||
{"mismatched without force", "foo", "bd", "", false, true},
|
||||
|
||||
// Multi-prefix cases (Gas Town use case)
|
||||
{"allowed prefix gt", "gt", "hq", "gt,hmc", false, false},
|
||||
{"allowed prefix hmc", "hmc", "hq", "gt,hmc", false, false},
|
||||
{"primary prefix still works", "hq", "hq", "gt,hmc", false, false},
|
||||
{"prefix not in allowed list", "foo", "hq", "gt,hmc", false, true},
|
||||
|
||||
// Edge cases
|
||||
{"allowed with spaces", "gt", "hq", "gt, hmc, foo", false, false},
|
||||
{"empty allowed list", "gt", "hq", "", false, true},
|
||||
{"single allowed prefix", "gt", "hq", "gt", false, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidatePrefixWithAllowed(tt.requestedPrefix, tt.dbPrefix, tt.allowedPrefixes, tt.force)
|
||||
if (err != nil) != tt.wantError {
|
||||
t.Errorf("ValidatePrefixWithAllowed() error = %v, wantError %v", err, tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAgentID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user