Add bd config command for external integration configuration

- Add GetAllConfig/DeleteConfig methods to storage interface
- Implement config set/get/list/unset subcommands with JSON support
- Add comprehensive tests for config operations
- Create CONFIG.md with full documentation and examples
- Update README.md with config section
- Support namespace conventions (jira.*, linear.*, github.*, custom.*)

Closes bd-60

Amp-Thread-ID: https://ampcode.com/threads/T-33db7481-de7c-475e-b562-6afb7fb4bc7a
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-23 14:14:00 -07:00
parent feac3f86e7
commit e8eb0cb6ae
7 changed files with 741 additions and 76 deletions

175
cmd/bd/config.go Normal file
View File

@@ -0,0 +1,175 @@
package main
import (
"context"
"fmt"
"os"
"sort"
"github.com/spf13/cobra"
)
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage configuration settings",
Long: `Manage configuration settings for external integrations and preferences.
Configuration is stored per-project in .beads/*.db and is version-control-friendly.
Common namespaces:
- jira.* Jira integration settings
- linear.* Linear integration settings
- github.* GitHub integration settings
- custom.* Custom integration settings
Examples:
bd config set jira.url "https://company.atlassian.net"
bd config set jira.project "PROJ"
bd config get jira.url
bd config list
bd config unset jira.url`,
}
var configSetCmd = &cobra.Command{
Use: "set <key> <value>",
Short: "Set a configuration value",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
// Config operations work in direct mode only
if err := ensureDirectMode("config set requires direct database access"); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
key := args[0]
value := args[1]
ctx := context.Background()
if err := store.SetConfig(ctx, key, value); err != nil {
fmt.Fprintf(os.Stderr, "Error setting config: %v\n", err)
os.Exit(1)
}
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"value": value,
})
} else {
fmt.Printf("Set %s = %s\n", key, value)
}
},
}
var configGetCmd = &cobra.Command{
Use: "get <key>",
Short: "Get a configuration value",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Config operations work in direct mode only
if err := ensureDirectMode("config get requires direct database access"); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
key := args[0]
ctx := context.Background()
value, err := store.GetConfig(ctx, key)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting config: %v\n", err)
os.Exit(1)
}
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"value": value,
})
} else {
if value == "" {
fmt.Printf("%s (not set)\n", key)
} else {
fmt.Printf("%s\n", value)
}
}
},
}
var configListCmd = &cobra.Command{
Use: "list",
Short: "List all configuration",
Run: func(cmd *cobra.Command, args []string) {
// Config operations work in direct mode only
if err := ensureDirectMode("config list requires direct database access"); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
ctx := context.Background()
config, err := store.GetAllConfig(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error listing config: %v\n", err)
os.Exit(1)
}
if jsonOutput {
outputJSON(config)
return
}
if len(config) == 0 {
fmt.Println("No configuration set")
return
}
// Sort keys for consistent output
keys := make([]string, 0, len(config))
for k := range config {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println("\nConfiguration:")
for _, k := range keys {
fmt.Printf(" %s = %s\n", k, config[k])
}
},
}
var configUnsetCmd = &cobra.Command{
Use: "unset <key>",
Short: "Delete a configuration value",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Config operations work in direct mode only
if err := ensureDirectMode("config unset requires direct database access"); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
key := args[0]
ctx := context.Background()
if err := store.DeleteConfig(ctx, key); err != nil {
fmt.Fprintf(os.Stderr, "Error deleting config: %v\n", err)
os.Exit(1)
}
if jsonOutput {
outputJSON(map[string]string{
"key": key,
})
} else {
fmt.Printf("Unset %s\n", key)
}
},
}
func init() {
configCmd.AddCommand(configSetCmd)
configCmd.AddCommand(configGetCmd)
configCmd.AddCommand(configListCmd)
configCmd.AddCommand(configUnsetCmd)
rootCmd.AddCommand(configCmd)
}

171
cmd/bd/config_test.go Normal file
View File

@@ -0,0 +1,171 @@
package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
)
func TestConfigCommands(t *testing.T) {
ctx := context.Background()
store, cleanup := setupTestDB(t)
defer cleanup()
// Test SetConfig
err := store.SetConfig(ctx, "test.key", "test-value")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
// Test GetConfig
value, err := store.GetConfig(ctx, "test.key")
if err != nil {
t.Fatalf("GetConfig failed: %v", err)
}
if value != "test-value" {
t.Errorf("Expected 'test-value', got '%s'", value)
}
// Test GetConfig for non-existent key
value, err = store.GetConfig(ctx, "nonexistent.key")
if err != nil {
t.Fatalf("GetConfig for nonexistent key failed: %v", err)
}
if value != "" {
t.Errorf("Expected empty string for nonexistent key, got '%s'", value)
}
// Test SetConfig update
err = store.SetConfig(ctx, "test.key", "updated-value")
if err != nil {
t.Fatalf("SetConfig update failed: %v", err)
}
value, err = store.GetConfig(ctx, "test.key")
if err != nil {
t.Fatalf("GetConfig after update failed: %v", err)
}
if value != "updated-value" {
t.Errorf("Expected 'updated-value', got '%s'", value)
}
// Test GetAllConfig
err = store.SetConfig(ctx, "jira.url", "https://example.atlassian.net")
if err != nil {
t.Fatalf("SetConfig for jira.url failed: %v", err)
}
err = store.SetConfig(ctx, "jira.project", "PROJ")
if err != nil {
t.Fatalf("SetConfig for jira.project failed: %v", err)
}
config, err := store.GetAllConfig(ctx)
if err != nil {
t.Fatalf("GetAllConfig failed: %v", err)
}
// Should have at least our test keys (may have default compaction config too)
if len(config) < 3 {
t.Errorf("Expected at least 3 config entries, got %d", len(config))
}
if config["test.key"] != "updated-value" {
t.Errorf("Expected 'updated-value' for test.key, got '%s'", config["test.key"])
}
if config["jira.url"] != "https://example.atlassian.net" {
t.Errorf("Expected jira.url in config, got '%s'", config["jira.url"])
}
if config["jira.project"] != "PROJ" {
t.Errorf("Expected jira.project in config, got '%s'", config["jira.project"])
}
// Test DeleteConfig
err = store.DeleteConfig(ctx, "test.key")
if err != nil {
t.Fatalf("DeleteConfig failed: %v", err)
}
value, err = store.GetConfig(ctx, "test.key")
if err != nil {
t.Fatalf("GetConfig after delete failed: %v", err)
}
if value != "" {
t.Errorf("Expected empty string after delete, got '%s'", value)
}
// Test DeleteConfig for non-existent key (should not error)
err = store.DeleteConfig(ctx, "nonexistent.key")
if err != nil {
t.Fatalf("DeleteConfig for nonexistent key failed: %v", err)
}
}
func TestConfigNamespaces(t *testing.T) {
ctx := context.Background()
store, cleanup := setupTestDB(t)
defer cleanup()
// Test various namespace conventions
namespaces := map[string]string{
"jira.url": "https://example.atlassian.net",
"jira.project": "PROJ",
"jira.status_map.todo": "open",
"linear.team_id": "team-123",
"github.org": "myorg",
"custom.my_integration.field": "value",
}
for key, val := range namespaces {
err := store.SetConfig(ctx, key, val)
if err != nil {
t.Fatalf("SetConfig for %s failed: %v", key, err)
}
}
// Verify all set correctly
for key, expected := range namespaces {
value, err := store.GetConfig(ctx, key)
if err != nil {
t.Fatalf("GetConfig for %s failed: %v", key, err)
}
if value != expected {
t.Errorf("Expected '%s' for %s, got '%s'", expected, key, value)
}
}
// Test GetAllConfig returns all
config, err := store.GetAllConfig(ctx)
if err != nil {
t.Fatalf("GetAllConfig failed: %v", err)
}
for key, expected := range namespaces {
if config[key] != expected {
t.Errorf("Expected '%s' for %s in GetAllConfig, got '%s'", expected, key, config[key])
}
}
}
// setupTestDB creates a temporary test database
func setupTestDB(t *testing.T) (*sqlite.SQLiteStorage, func()) {
tmpDir, err := os.MkdirTemp("", "bd-test-config-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
testDB := filepath.Join(tmpDir, "test.db")
store, err := sqlite.New(testDB)
if err != nil {
os.RemoveAll(tmpDir)
t.Fatalf("Failed to create test database: %v", err)
}
cleanup := func() {
store.Close()
os.RemoveAll(tmpDir)
}
return store, cleanup
}