beads/cmd/bd/import.go
coffeegoddd☕️✨ fa87dd2bdd feat: add global beads_global database for shared-server mode
Adds a project-agnostic beads_global database to shared-server mode so
clients can track issues globally across all projects on the server.

- Add EnsureGlobalDatabase() to create the DB during shared server init
- Add --global persistent flag to switch CLI commands to the global DB
- Add global_dolt_database field to metadata.json for connection info
- Seed global DB with issue_prefix="global" and sentinel project ID
- Export/import default to global-issues.jsonl with --global flag
- --global errors when not in shared-server mode (additive, no breakage)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 09:18:43 -07:00

105 lines
3.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
)
var importCmd = &cobra.Command{
Use: "import [file]",
Short: "Import issues from a JSONL file into the database",
Long: `Import issues from a JSONL file (newline-delimited JSON) into the database.
If no file is specified, imports from .beads/issues.jsonl (the git-tracked
export). This is the incremental counterpart to 'bd export': new issues are
created and existing issues are updated (upsert semantics).
Memory records (lines with "_type":"memory") are automatically detected and
imported as persistent memories (equivalent to 'bd remember'). This makes
'bd export | bd import' a full round-trip for both issues and memories.
This command makes the git-tracked JSONL portable again — after 'git pull'
brings new issues, 'bd import' loads them into the local Dolt database.
EXAMPLES:
bd import # Import from .beads/issues.jsonl
bd import backup.jsonl # Import from a specific file
bd import --dry-run # Show what would be imported`,
GroupID: "sync",
RunE: runImport,
}
var (
importDryRun bool
)
func init() {
importCmd.Flags().BoolVar(&importDryRun, "dry-run", false, "Show what would be imported without importing")
rootCmd.AddCommand(importCmd)
}
func runImport(cmd *cobra.Command, args []string) error {
ctx := rootCtx
// Determine source file
var jsonlPath string
if len(args) > 0 {
jsonlPath = args[0]
} else {
// Default: .beads/issues.jsonl (or .beads/global-issues.jsonl with --global)
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
return fmt.Errorf("%s — %s", activeWorkspaceNotFoundError(), diagHint())
}
if globalFlag {
jsonlPath = filepath.Join(beadsDir, "global-issues.jsonl")
} else {
jsonlPath = filepath.Join(beadsDir, "issues.jsonl")
}
}
// Check file exists
info, err := os.Stat(jsonlPath)
if err != nil {
return fmt.Errorf("cannot read %s: %w", jsonlPath, err)
}
if info.Size() == 0 {
fmt.Fprintf(os.Stderr, "Empty file: %s\n", jsonlPath)
return nil
}
if importDryRun {
fmt.Fprintf(os.Stderr, "Would import from: %s (%d bytes)\n", jsonlPath, info.Size())
return nil
}
// store is the global Dolt store, opened by main.go's PersistentPreRunE
if store == nil {
return fmt.Errorf("no database — run 'bd init' or 'bd bootstrap' first")
}
result, err := importFromLocalJSONLFull(ctx, store, jsonlPath)
if err != nil {
return fmt.Errorf("import failed: %w", err)
}
commitMsg := fmt.Sprintf("bd import: %d issues", result.Issues)
if result.Memories > 0 {
commitMsg += fmt.Sprintf(", %d memories", result.Memories)
}
commitMsg += fmt.Sprintf(" from %s", filepath.Base(jsonlPath))
if err := store.Commit(ctx, commitMsg); err != nil {
return fmt.Errorf("commit: %w", err)
}
if result.Memories > 0 {
fmt.Fprintf(os.Stderr, "Imported %d issues and %d memories from %s\n", result.Issues, result.Memories, jsonlPath)
} else {
fmt.Fprintf(os.Stderr, "Imported %d issues from %s\n", result.Issues, jsonlPath)
}
return nil
}