1759 lines
55 KiB
Go
1759 lines
55 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/doltserver"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/doltutil"
|
|
"github.com/steveyegge/beads/internal/ui"
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
var doltCmd = &cobra.Command{
|
|
Use: "dolt",
|
|
GroupID: "setup",
|
|
Short: "Configure Dolt database settings",
|
|
Long: `Configure and manage Dolt database settings and server lifecycle.
|
|
|
|
Beads uses a dolt sql-server for all database operations. The server is
|
|
auto-started transparently when needed. Use these commands for explicit
|
|
control or diagnostics.
|
|
|
|
Server lifecycle:
|
|
bd dolt start Start the Dolt server for this project
|
|
bd dolt stop Stop the Dolt server for this project
|
|
bd dolt status Show Dolt server status
|
|
|
|
Configuration:
|
|
bd dolt show Show current Dolt configuration with connection test
|
|
bd dolt set <k> <v> Set a configuration value
|
|
bd dolt test Test server connection
|
|
|
|
Version control:
|
|
bd dolt commit Commit pending changes
|
|
bd dolt push Push commits to Dolt remote
|
|
bd dolt pull Pull commits from Dolt remote
|
|
|
|
Remote management:
|
|
bd dolt remote add <name> <url> Add a Dolt remote
|
|
bd dolt remote list List configured remotes
|
|
bd dolt remote remove <name> Remove a Dolt remote
|
|
|
|
Configuration keys for 'bd dolt set':
|
|
database Database name (default: issue prefix or "beads")
|
|
host Server host (default: 127.0.0.1)
|
|
port Server port (auto-detected; override with bd dolt set port <N>)
|
|
user MySQL user (default: root)
|
|
data-dir Custom dolt data directory (absolute path; default: .beads/dolt)
|
|
|
|
Flags for 'bd dolt set':
|
|
--update-config Also write to config.yaml for team-wide defaults
|
|
|
|
Examples:
|
|
bd dolt set database myproject
|
|
bd dolt set host 192.168.1.100 --update-config
|
|
bd dolt set data-dir /home/user/.beads-dolt/myproject
|
|
bd dolt test`,
|
|
}
|
|
|
|
var doltShowCmd = &cobra.Command{
|
|
Use: "show",
|
|
Short: "Show current Dolt configuration with connection status",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
showDoltConfig(true)
|
|
},
|
|
}
|
|
|
|
var doltSetCmd = &cobra.Command{
|
|
Use: "set <key> <value>",
|
|
Short: "Set a Dolt configuration value",
|
|
Long: `Set a Dolt configuration value in metadata.json.
|
|
|
|
Keys:
|
|
database Database name (default: issue prefix or "beads")
|
|
host Server host (default: 127.0.0.1)
|
|
port Server port (auto-detected; override with bd dolt set port <N>)
|
|
user MySQL user (default: root)
|
|
data-dir Custom dolt data directory (absolute path; default: .beads/dolt)
|
|
|
|
Use --update-config to also write to config.yaml for team-wide defaults.
|
|
|
|
Examples:
|
|
bd dolt set database myproject
|
|
bd dolt set host 192.168.1.100
|
|
bd dolt set port 3307 --update-config
|
|
bd dolt set data-dir /home/user/.beads-dolt/myproject`,
|
|
Args: cobra.ExactArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if isEmbeddedMode() {
|
|
fmt.Fprintln(os.Stderr, "Error: 'bd dolt set' is not supported in embedded mode (no Dolt server)")
|
|
os.Exit(1)
|
|
}
|
|
key := args[0]
|
|
value := args[1]
|
|
updateConfig, _ := cmd.Flags().GetBool("update-config")
|
|
setDoltConfig(key, value, updateConfig)
|
|
},
|
|
}
|
|
|
|
var doltTestCmd = &cobra.Command{
|
|
Use: "test",
|
|
Short: "Test connection to Dolt server",
|
|
Long: `Test the connection to the configured Dolt server.
|
|
|
|
This verifies that:
|
|
1. The server is reachable at the configured host:port
|
|
2. The connection can be established
|
|
|
|
Use this before switching to server mode to ensure the server is running.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if isEmbeddedMode() {
|
|
fmt.Fprintln(os.Stderr, "Error: 'bd dolt test' is not supported in embedded mode (no Dolt server)")
|
|
os.Exit(1)
|
|
}
|
|
testDoltConnection()
|
|
},
|
|
}
|
|
|
|
// isRemoteNotFoundErr checks whether the error is a Dolt "remote not found"
|
|
// error. This typically happens when the remote was added via `dolt remote add`
|
|
// (filesystem config) but not via `bd dolt remote add` (which also registers it
|
|
// in the SQL server's dolt_remotes table).
|
|
func isRemoteNotFoundErr(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
msg := strings.ToLower(err.Error())
|
|
return strings.Contains(msg, "remote") && strings.Contains(msg, "not found")
|
|
}
|
|
|
|
// isDivergedHistoryErr checks whether the error indicates that local and remote
|
|
// Dolt histories have diverged. This happens when independent pushes create
|
|
// separate commit histories with no common merge base (e.g., two agents
|
|
// bootstrapping from scratch and pushing to the same remote, or a local
|
|
// database being re-initialized while the remote retains the old history).
|
|
func isDivergedHistoryErr(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
msg := strings.ToLower(err.Error())
|
|
return strings.Contains(msg, "no common ancestor") ||
|
|
strings.Contains(msg, "can't find common ancestor") ||
|
|
strings.Contains(msg, "cannot find common ancestor")
|
|
}
|
|
|
|
// printNoRemoteGuidance prints an informational message (to stdout) when
|
|
// push or pull is attempted but no Dolt remote is configured. Exits 0 because
|
|
// the absence of a remote is a valid configuration — not an error.
|
|
func printNoRemoteGuidance() {
|
|
fmt.Println("No remote is configured — skipping.")
|
|
fmt.Println("")
|
|
fmt.Println("For solo use, pushing is optional — your issues are stored locally")
|
|
fmt.Println("in .beads/ and versioned by Dolt automatically.")
|
|
fmt.Println("")
|
|
fmt.Println("To set up remote sync (for backup or team sharing):")
|
|
fmt.Println(" bd dolt remote add origin <url>")
|
|
fmt.Println(" bd dolt push")
|
|
fmt.Println("")
|
|
fmt.Println("Supported remote URLs:")
|
|
fmt.Println(" • GitHub (via git): git+ssh://git@github.com/org/repo.git")
|
|
fmt.Println(" • DoltHub: https://doltremoteapi.dolthub.com/org/repo")
|
|
fmt.Println(" • Azure Blob Storage: az://account.blob.core.windows.net/container/path")
|
|
}
|
|
|
|
// printDivergedHistoryGuidance prints recovery guidance when push/pull fails
|
|
// due to diverged local and remote histories.
|
|
func printDivergedHistoryGuidance(operation string) {
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, "Local and remote Dolt histories have diverged.")
|
|
fmt.Fprintln(os.Stderr, "This means the local database and the remote have independent commit")
|
|
fmt.Fprintln(os.Stderr, "histories with no common merge base.")
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, "Recovery options:")
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, " 1. Keep remote, discard local (recommended if remote is authoritative):")
|
|
fmt.Fprintln(os.Stderr, " bd bootstrap # re-clone from remote")
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, " 2. Keep local, overwrite remote (if local is authoritative):")
|
|
fmt.Fprintln(os.Stderr, " bd dolt push --force # force-push local history to remote")
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, " 3. Manual recovery (re-initialize local database):")
|
|
fmt.Fprintln(os.Stderr, " rm -rf .beads/dolt # delete local Dolt database")
|
|
fmt.Fprintln(os.Stderr, " bd bootstrap # re-clone from remote")
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, "Tip: This usually happens when multiple agents independently initialize")
|
|
fmt.Fprintln(os.Stderr, "databases and push to the same remote. Use 'bd bootstrap' to clone an")
|
|
fmt.Fprintln(os.Stderr, "existing remote instead of 'bd init' to avoid divergent histories.")
|
|
}
|
|
|
|
var doltPushCmd = &cobra.Command{
|
|
Use: "push",
|
|
Short: "Push commits to Dolt remote",
|
|
Long: `Push local Dolt commits to the configured remote.
|
|
|
|
Requires a Dolt remote to be configured in the database directory.
|
|
For Hosted Dolt, set DOLT_REMOTE_USER and DOLT_REMOTE_PASSWORD environment
|
|
variables for authentication.
|
|
|
|
Use --force to overwrite remote changes (e.g., when the remote has
|
|
uncommitted changes in its working set).
|
|
|
|
Use --remote to push to a specific named remote instead of the default.
|
|
The remote must already exist (see 'bd dolt remote add').`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
if st == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: no store available\n")
|
|
os.Exit(1)
|
|
}
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
remote, _ := cmd.Flags().GetString("remote")
|
|
if remote != "" {
|
|
fmt.Printf("Pushing to Dolt remote %q...\n", remote)
|
|
if err := st.PushRemote(ctx, remote, force); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
if isRemoteNotFoundErr(err) {
|
|
fmt.Fprintf(os.Stderr, "\nRemote %q is not configured.\n", remote)
|
|
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote add <name> <url>' to add it.")
|
|
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote list' to see configured remotes.")
|
|
} else if isDivergedHistoryErr(err) {
|
|
printDivergedHistoryGuidance("push --force")
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Push complete.")
|
|
return
|
|
}
|
|
fmt.Println("Pushing to Dolt remote...")
|
|
|
|
var pushErr error
|
|
if force {
|
|
pushErr = st.ForcePush(ctx)
|
|
} else {
|
|
pushErr = st.Push(ctx)
|
|
}
|
|
if pushErr != nil {
|
|
if isRemoteNotFoundErr(pushErr) {
|
|
printNoRemoteGuidance()
|
|
return
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", pushErr)
|
|
if isDivergedHistoryErr(pushErr) {
|
|
op := "push"
|
|
if force {
|
|
op = "push --force"
|
|
}
|
|
printDivergedHistoryGuidance(op)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Push complete.")
|
|
},
|
|
}
|
|
|
|
var doltPullCmd = &cobra.Command{
|
|
Use: "pull",
|
|
Short: "Pull commits from Dolt remote",
|
|
Long: `Pull commits from the configured Dolt remote into the local database.
|
|
|
|
Requires a Dolt remote to be configured in the database directory.
|
|
For Hosted Dolt, set DOLT_REMOTE_USER and DOLT_REMOTE_PASSWORD environment
|
|
variables for authentication.
|
|
|
|
Use --remote to pull from a specific named remote instead of the default.
|
|
The remote must already exist (see 'bd dolt remote add').`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
if st == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: no store available\n")
|
|
os.Exit(1)
|
|
}
|
|
remote, _ := cmd.Flags().GetString("remote")
|
|
if remote != "" {
|
|
fmt.Printf("Pulling from Dolt remote %q...\n", remote)
|
|
if err := st.PullRemote(ctx, remote); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
if isRemoteNotFoundErr(err) {
|
|
fmt.Fprintf(os.Stderr, "\nRemote %q is not configured.\n", remote)
|
|
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote add <name> <url>' to add it.")
|
|
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote list' to see configured remotes.")
|
|
} else if isDivergedHistoryErr(err) {
|
|
printDivergedHistoryGuidance("pull")
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Pull complete.")
|
|
return
|
|
}
|
|
fmt.Println("Pulling from Dolt remote...")
|
|
if err := st.Pull(ctx); err != nil {
|
|
if isRemoteNotFoundErr(err) {
|
|
printNoRemoteGuidance()
|
|
return
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
if isDivergedHistoryErr(err) {
|
|
printDivergedHistoryGuidance("pull")
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Pull complete.")
|
|
},
|
|
}
|
|
|
|
var doltCommitCmd = &cobra.Command{
|
|
Use: "commit",
|
|
Short: "Create a Dolt commit from pending changes",
|
|
Long: `Create a Dolt commit from any uncommitted changes in the working set.
|
|
|
|
This is the primary commit point for batch mode. When auto-commit is set to
|
|
"batch", changes accumulate in the working set across multiple bd commands and
|
|
are committed together here with a descriptive summary message.
|
|
|
|
Also useful before push operations that require a clean working set, or when
|
|
auto-commit was off or changes were made externally.
|
|
|
|
For more options (--stdin, custom messages), see: bd vc commit`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
if st == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: no store available\n")
|
|
os.Exit(1)
|
|
}
|
|
msg, _ := cmd.Flags().GetString("message")
|
|
if msg == "" {
|
|
msg = fmt.Sprintf("bd: dolt commit (auto-commit) by %s", getActor())
|
|
}
|
|
if err := st.Commit(ctx, msg); err != nil {
|
|
if isDoltNothingToCommit(err) {
|
|
fmt.Println("Nothing to commit.")
|
|
return
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
commandDidExplicitDoltCommit = true
|
|
fmt.Println("Committed.")
|
|
},
|
|
}
|
|
|
|
var doltStartCmd = &cobra.Command{
|
|
Use: "start",
|
|
Short: "Start the Dolt SQL server for this project",
|
|
Long: `Start a dolt sql-server for the current beads project.
|
|
|
|
The server runs in the background on a per-project port derived from the
|
|
project path. PID and logs are stored in .beads/.
|
|
|
|
The server auto-starts transparently when needed, so manual start is rarely
|
|
required. Use this command for explicit control or diagnostics.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if isEmbeddedMode() {
|
|
fmt.Fprintln(os.Stderr, "Error: 'bd dolt start' is not supported in embedded mode (no Dolt server)")
|
|
os.Exit(1)
|
|
}
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
serverDir := doltserver.ResolveServerDir(beadsDir)
|
|
|
|
state, err := doltserver.Start(serverDir)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "already running") {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("Dolt server started (PID %d, port %d)\n", state.PID, state.Port)
|
|
fmt.Printf(" Data: %s\n", state.DataDir)
|
|
fmt.Printf(" Logs: %s\n", doltserver.LogPath(serverDir))
|
|
if doltserver.IsSharedServerMode() {
|
|
fmt.Println(" Mode: shared server")
|
|
}
|
|
},
|
|
}
|
|
|
|
var doltStopCmd = &cobra.Command{
|
|
Use: "stop",
|
|
Short: "Stop the Dolt SQL server for this project",
|
|
Long: `Stop the dolt sql-server managed by beads for the current project.
|
|
|
|
This sends a graceful shutdown signal. The server will restart automatically
|
|
on the next bd command unless auto-start is disabled.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if isEmbeddedMode() {
|
|
fmt.Fprintln(os.Stderr, "Error: 'bd dolt stop' is not supported in embedded mode (no Dolt server)")
|
|
os.Exit(1)
|
|
}
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
serverDir := doltserver.ResolveServerDir(beadsDir)
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
|
|
if err := doltserver.StopWithForce(serverDir, force); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Dolt server stopped.")
|
|
},
|
|
}
|
|
|
|
var doltStatusCmd = &cobra.Command{
|
|
Use: "status",
|
|
Short: "Show Dolt engine status",
|
|
Long: `Show the status of the Dolt engine for the current project.
|
|
|
|
In embedded mode, reports that the Dolt engine runs in-process and shows
|
|
the on-disk data directory. For beads-managed (local) servers, displays
|
|
PID, port, and data directory from the local PID file. For externally-
|
|
hosted servers (dolt_mode=server with a remote dolt_server_host), pings
|
|
the configured endpoint via SQL and reports reachability, server version,
|
|
and database.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
if isEmbeddedMode() {
|
|
showEmbeddedDoltStatus(beadsDir)
|
|
return
|
|
}
|
|
|
|
// For externally-hosted Dolt servers (non-local host with
|
|
// dolt_mode=server), the local PID file is meaningless — ping the
|
|
// configured endpoint via SQL instead (bd-q35w).
|
|
if cfg, cfgErr := configfile.Load(beadsDir); cfgErr == nil && cfg != nil &&
|
|
cfg.IsDoltServerMode() && !isLocalHost(cfg.GetDoltServerHost()) {
|
|
runExternalDoltStatus(beadsDir, cfg)
|
|
return
|
|
}
|
|
|
|
serverDir := doltserver.ResolveServerDir(beadsDir)
|
|
|
|
state, err := doltserver.IsRunning(serverDir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if jsonOutput {
|
|
outputJSON(state)
|
|
return
|
|
}
|
|
|
|
if state == nil || !state.Running {
|
|
cfg := doltserver.DefaultConfig(serverDir)
|
|
fmt.Println("Dolt server: not running")
|
|
fmt.Printf(" Expected port: %d\n", cfg.Port)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Dolt server: running")
|
|
fmt.Printf(" PID: %d\n", state.PID)
|
|
fmt.Printf(" Port: %d\n", state.Port)
|
|
fmt.Printf(" Data: %s\n", state.DataDir)
|
|
fmt.Printf(" Logs: %s\n", doltserver.LogPath(serverDir))
|
|
if doltserver.IsSharedServerMode() {
|
|
fmt.Println(" Mode: shared server")
|
|
}
|
|
},
|
|
}
|
|
|
|
// isLocalHost reports whether host refers to this machine. Used to
|
|
// distinguish beads-managed local servers from externally-hosted ones.
|
|
func isLocalHost(host string) bool {
|
|
h := strings.ToLower(strings.TrimSpace(host))
|
|
if h == "" {
|
|
return true // empty defaults to local
|
|
}
|
|
switch h {
|
|
case "localhost", "127.0.0.1", "::1", "0.0.0.0":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// runExternalDoltStatus queries an externally-hosted Dolt server and prints
|
|
// (or returns, for --json) status. Unlike the local path, there is no PID or
|
|
// log file — reachability, version, host/port/database, and TLS mode are the
|
|
// user-relevant signals.
|
|
func runExternalDoltStatus(beadsDir string, cfg *configfile.Config) {
|
|
host := cfg.GetDoltServerHost()
|
|
port := doltserver.DefaultConfig(beadsDir).Port
|
|
user := cfg.GetDoltServerUser()
|
|
database := cfg.GetDoltDatabase()
|
|
tls := cfg.GetDoltServerTLS()
|
|
password := cfg.GetDoltServerPasswordForPort(port)
|
|
|
|
dsn := doltutil.ServerDSN{
|
|
Host: host,
|
|
Port: port,
|
|
User: user,
|
|
Password: password,
|
|
TLS: tls,
|
|
Timeout: 5 * time.Second,
|
|
}.String()
|
|
|
|
result := map[string]interface{}{
|
|
"mode": "external",
|
|
"host": host,
|
|
"port": port,
|
|
"user": user,
|
|
"database": database,
|
|
"tls": tls,
|
|
}
|
|
|
|
db, openErr := sql.Open("mysql", dsn)
|
|
var running bool
|
|
var version string
|
|
var connErr error
|
|
|
|
if openErr == nil {
|
|
defer db.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if pingErr := db.PingContext(ctx); pingErr != nil {
|
|
connErr = pingErr
|
|
} else {
|
|
running = true
|
|
// Best-effort version lookup; don't treat errors as fatal.
|
|
_ = db.QueryRowContext(ctx, "SELECT @@version").Scan(&version)
|
|
}
|
|
} else {
|
|
connErr = openErr
|
|
}
|
|
|
|
result["running"] = running
|
|
if version != "" {
|
|
result["version"] = version
|
|
}
|
|
if connErr != nil {
|
|
result["error"] = connErr.Error()
|
|
}
|
|
|
|
if jsonOutput {
|
|
outputJSON(result)
|
|
return
|
|
}
|
|
|
|
if running {
|
|
fmt.Println("Dolt server: running (external)")
|
|
} else {
|
|
fmt.Println("Dolt server: not reachable (external)")
|
|
}
|
|
fmt.Printf(" Host: %s\n", host)
|
|
fmt.Printf(" Port: %d\n", port)
|
|
fmt.Printf(" Database: %s\n", database)
|
|
fmt.Printf(" User: %s\n", user)
|
|
fmt.Printf(" TLS: %t\n", tls)
|
|
if version != "" {
|
|
fmt.Printf(" Version: %s\n", version)
|
|
}
|
|
if connErr != nil {
|
|
fmt.Printf(" Error: %v\n", connErr)
|
|
}
|
|
}
|
|
|
|
// showEmbeddedDoltStatus reports Dolt engine status when running in
|
|
// embedded mode. There is no separate server process; the engine runs
|
|
// in-process and data lives at .beads/embeddeddolt/.
|
|
func showEmbeddedDoltStatus(beadsDir string) {
|
|
dataDir := filepath.Join(beadsDir, "embeddeddolt")
|
|
dataDirExists := false
|
|
if info, err := os.Stat(dataDir); err == nil && info.IsDir() {
|
|
dataDirExists = true
|
|
}
|
|
|
|
if jsonOutput {
|
|
outputJSON(map[string]interface{}{
|
|
"mode": "embedded",
|
|
// Embedded mode has an active in-process engine, but no
|
|
// separate server process. Use a server-specific field so
|
|
// clients do not read running=false as "Dolt is unavailable".
|
|
"server_running": false,
|
|
"data_dir": dataDir,
|
|
"data_dir_exists": dataDirExists,
|
|
})
|
|
return
|
|
}
|
|
|
|
fmt.Println("Dolt engine: embedded (in-process, no server)")
|
|
fmt.Printf(" Data: %s\n", dataDir)
|
|
if !dataDirExists {
|
|
fmt.Printf(" %s\n", ui.RenderWarn("Data directory does not exist — run 'bd init' to create it"))
|
|
}
|
|
}
|
|
|
|
var doltKillallCmd = &cobra.Command{
|
|
Use: "killall",
|
|
Short: "Kill all orphan Dolt server processes",
|
|
Long: `Find and kill orphan dolt sql-server processes not tracked by the
|
|
canonical PID file for the current repo's Dolt data directory.
|
|
|
|
Under an orchestrator, the canonical server lives at $GT_ROOT/.beads/. Any other
|
|
dolt sql-server processes using that shared data directory are considered
|
|
orphans and will be killed.
|
|
|
|
In standalone mode, only dolt sql-server processes using the current
|
|
project's Dolt data directory are eligible for cleanup. Other projects'
|
|
servers are preserved.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if isEmbeddedMode() {
|
|
fmt.Fprintln(os.Stderr, "Error: 'bd dolt killall' is not supported in embedded mode (no Dolt server)")
|
|
os.Exit(1)
|
|
}
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
beadsDir = "." // best effort
|
|
}
|
|
|
|
killed, err := doltserver.KillStaleServers(beadsDir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(killed) == 0 {
|
|
fmt.Println("No orphan dolt servers found.")
|
|
} else {
|
|
fmt.Printf("Killed %d orphan dolt server(s): %v\n", len(killed), killed)
|
|
}
|
|
},
|
|
}
|
|
|
|
// staleDatabasePrefixes identifies test/agent databases that should not persist
|
|
// on the production Dolt server. These accumulate from interrupted test runs and
|
|
// terminated agents, wasting server memory.
|
|
// - testdb_*: BEADS_TEST_MODE=1 FNV hash of temp paths
|
|
// - doctest_*: doctor test helpers
|
|
// - doctortest_*: doctor test helpers
|
|
// - beads_pt*: orchestrator patrol_helpers_test.go random prefixes
|
|
// - beads_vr*: orchestrator mail/router_test.go random prefixes
|
|
// - beads_t[0-9a-f]*: protocol test random prefixes (t + 8 hex chars)
|
|
var staleDatabasePrefixes = []string{"testdb_", "doctest_", "doctortest_", "beads_pt", "beads_vr", "beads_t"}
|
|
|
|
var doltCleanDatabasesCmd = &cobra.Command{
|
|
Use: "clean-databases",
|
|
Short: "Drop stale test databases from the Dolt server",
|
|
Long: `Identify and drop leftover test and agent databases that accumulate
|
|
on the shared Dolt server from interrupted test runs and terminated agents.
|
|
|
|
Stale database prefixes: testdb_*, doctest_*, doctortest_*, beads_pt*, beads_vr*, beads_t*
|
|
|
|
These waste server memory and can degrade performance under concurrent load.
|
|
Use --dry-run to see what would be dropped without actually dropping.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if isEmbeddedMode() {
|
|
fmt.Fprintln(os.Stderr, "Error: 'bd dolt clean-databases' is not supported in embedded mode (no Dolt server)")
|
|
os.Exit(1)
|
|
}
|
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
|
|
|
// Connect directly to the Dolt server via config instead of getStore(),
|
|
// which isn't initialized for dolt subcommands (beads-9vt).
|
|
db, cleanup := openDoltServerConnection()
|
|
defer cleanup()
|
|
|
|
listCtx, listCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer listCancel()
|
|
|
|
rows, err := db.QueryContext(listCtx, "SHOW DATABASES")
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error listing databases: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var stale []string
|
|
for rows.Next() {
|
|
var dbName string
|
|
if err := rows.Scan(&dbName); err != nil {
|
|
continue
|
|
}
|
|
for _, prefix := range staleDatabasePrefixes {
|
|
if strings.HasPrefix(dbName, prefix) {
|
|
stale = append(stale, dbName)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(stale) == 0 {
|
|
fmt.Println("No stale databases found.")
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Found %d stale databases:\n", len(stale))
|
|
for _, name := range stale {
|
|
fmt.Printf(" %s\n", name)
|
|
}
|
|
|
|
if dryRun {
|
|
fmt.Println("\n(dry run — no databases dropped)")
|
|
return
|
|
}
|
|
|
|
fmt.Println()
|
|
dropped := 0
|
|
failures := 0
|
|
consecutiveTimeouts := 0
|
|
const (
|
|
batchSize = 5 // Drop this many before pausing
|
|
batchPause = 2 * time.Second
|
|
backoffPause = 10 * time.Second
|
|
timeoutThreshold = 3 // Consecutive timeouts before backoff
|
|
perDropTimeout = 30 * time.Second
|
|
maxConsecFailures = 10 // Stop after this many consecutive failures
|
|
)
|
|
|
|
for i, name := range stale {
|
|
// Circuit breaker: back off when server is overwhelmed
|
|
if consecutiveTimeouts >= timeoutThreshold {
|
|
fmt.Fprintf(os.Stderr, " ⚠ %d consecutive timeouts — backing off %s\n",
|
|
consecutiveTimeouts, backoffPause)
|
|
time.Sleep(backoffPause)
|
|
consecutiveTimeouts = 0
|
|
}
|
|
|
|
// Stop if too many consecutive failures — server is likely unhealthy
|
|
if failures >= maxConsecFailures {
|
|
fmt.Fprintf(os.Stderr, "\n✗ Aborting: %d consecutive failures suggest server is unhealthy.\n", failures)
|
|
fmt.Fprintf(os.Stderr, " Dropped %d/%d before stopping.\n", dropped, len(stale))
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Per-operation timeout: DROP DATABASE can be slow on Dolt
|
|
dropCtx, dropCancel := context.WithTimeout(context.Background(), perDropTimeout)
|
|
// Escape backticks in database name to prevent SQL injection (` → ``)
|
|
safeName := strings.ReplaceAll(name, "`", "``")
|
|
_, err := db.ExecContext(dropCtx, fmt.Sprintf("DROP DATABASE `%s`", safeName)) //nolint:gosec // G201: identifier-escaped
|
|
dropCancel()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, " FAIL: %s: %v\n", name, err)
|
|
failures++
|
|
if isTimeoutError(err) {
|
|
consecutiveTimeouts++
|
|
}
|
|
} else {
|
|
fmt.Printf(" Dropped: %s\n", name)
|
|
dropped++
|
|
failures = 0
|
|
consecutiveTimeouts = 0
|
|
}
|
|
|
|
// Rate limiting: pause between batches to let the server breathe
|
|
if (i+1)%batchSize == 0 && i+1 < len(stale) {
|
|
fmt.Printf(" [%d/%d] pausing %s...\n", i+1, len(stale), batchPause)
|
|
time.Sleep(batchPause)
|
|
}
|
|
}
|
|
fmt.Printf("\nDropped %d/%d stale databases.\n", dropped, len(stale))
|
|
},
|
|
}
|
|
|
|
// confirmOverwrite prompts the user to confirm overwriting an existing remote.
|
|
// Returns true if the user confirms. Returns true without prompting if stdin is
|
|
// not a terminal (non-interactive/CI contexts).
|
|
func confirmOverwrite(surface, name, existingURL, newURL string) bool {
|
|
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
|
return true
|
|
}
|
|
fmt.Printf(" Remote %q already exists on %s: %s\n", name, surface, existingURL)
|
|
fmt.Printf(" Overwrite with: %s\n", newURL)
|
|
fmt.Print(" Overwrite? (y/N): ")
|
|
reader := bufio.NewReader(os.Stdin)
|
|
response, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
return false
|
|
}
|
|
response = strings.TrimSpace(strings.ToLower(response))
|
|
return response == "y" || response == "yes"
|
|
}
|
|
|
|
var listDoltCLIRemotes = doltutil.ListCLIRemotes
|
|
|
|
func findRemoteURL(remotes []storage.RemoteInfo, name string) string {
|
|
for _, r := range remotes {
|
|
if r.Name == name {
|
|
return r.URL
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func listRemoteSurfaces(ctx context.Context, st storage.RemoteStore, dbPath string, embedded bool) (
|
|
sqlRemotes []storage.RemoteInfo,
|
|
sqlErr error,
|
|
cliRemotes []storage.RemoteInfo,
|
|
cliErr error,
|
|
) {
|
|
sqlRemotes, sqlErr = st.ListRemotes(ctx)
|
|
if embedded {
|
|
// Embedded mode holds an exclusive flock while the store is open. Running
|
|
// `dolt remote` as a subprocess against the same directory can block on
|
|
// that flock, so treat the SQL-visible remotes as the shared surface.
|
|
return sqlRemotes, sqlErr, sqlRemotes, nil
|
|
}
|
|
if strings.TrimSpace(dbPath) == "" {
|
|
return sqlRemotes, sqlErr, nil, fmt.Errorf("local Dolt CLI directory is not configured; set BEADS_DOLT_CLI_DIR to list filesystem remotes")
|
|
}
|
|
cliRemotes, cliErr = listDoltCLIRemotes(dbPath)
|
|
return sqlRemotes, sqlErr, cliRemotes, cliErr
|
|
}
|
|
|
|
// --- Dolt remote management commands ---
|
|
|
|
var doltRemoteCmd = &cobra.Command{
|
|
Use: "remote",
|
|
Short: "Manage Dolt remotes",
|
|
Long: `Manage Dolt remotes for push/pull replication.
|
|
|
|
Subcommands:
|
|
add <name> <url> Add a new remote
|
|
list List all configured remotes
|
|
remove <name> Remove a remote`,
|
|
}
|
|
|
|
var doltRemoteAddCmd = &cobra.Command{
|
|
Use: "add <name> <url>",
|
|
Short: "Add a Dolt remote (both SQL server and CLI)",
|
|
Args: cobra.ExactArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
if st == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: no store available\n")
|
|
os.Exit(1)
|
|
}
|
|
name, url := args[0], args[1]
|
|
locator, ok := storage.UnwrapStore(st).(storage.StoreLocator)
|
|
if !ok {
|
|
fmt.Fprintf(os.Stderr, "Error: storage backend does not support store location\n")
|
|
os.Exit(1)
|
|
}
|
|
dbPath := locator.CLIDir()
|
|
embedded := isEmbeddedMode()
|
|
|
|
// Check existing remotes on both surfaces
|
|
sqlRemotes, _, cliRemotes, _ := listRemoteSurfaces(ctx, st, dbPath, embedded)
|
|
sqlURL := findRemoteURL(sqlRemotes, name)
|
|
cliURL := findRemoteURL(cliRemotes, name)
|
|
|
|
// Prompt for overwrite if either surface already has this remote.
|
|
// In embedded mode SQL and CLI share the same directory, so only
|
|
// prompt once — the SQL remove handles both.
|
|
if sqlURL != "" && sqlURL != url {
|
|
if !confirmOverwrite("SQL server", name, sqlURL, url) {
|
|
fmt.Println("Canceled.")
|
|
return
|
|
}
|
|
// Remove existing SQL remote before re-adding
|
|
if err := st.RemoveRemote(ctx, name); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error removing existing SQL remote: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
if !embedded && cliURL != "" && cliURL != url {
|
|
if !confirmOverwrite("CLI (filesystem)", name, cliURL, url) {
|
|
fmt.Println("Canceled.")
|
|
return
|
|
}
|
|
if err := doltutil.RemoveCLIRemote(dbPath, name); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error removing existing CLI remote: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// Add to SQL server (skip if already correct)
|
|
if sqlURL != url {
|
|
if err := st.AddRemote(ctx, name, url); err != nil {
|
|
if jsonOutput {
|
|
outputJSONError(err, "remote_add_failed")
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Error adding SQL remote: %v\n", err)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// Add to CLI filesystem (skip if already correct).
|
|
// In embedded mode, SQL and CLI operate on the same directory,
|
|
// so the SQL add already wrote the remote config — skip CLI.
|
|
cliFailed := false
|
|
if !embedded && dbPath == "" {
|
|
cliFailed = true
|
|
fmt.Fprintf(os.Stderr, "Warning: SQL remote added but CLI remote was not configured: set BEADS_DOLT_CLI_DIR to the local Dolt database path\n")
|
|
} else if !embedded && cliURL != url {
|
|
if err := doltutil.AddCLIRemote(dbPath, name, url); err != nil {
|
|
cliFailed = true
|
|
// Non-fatal: SQL remote was added successfully
|
|
fmt.Fprintf(os.Stderr, "Warning: SQL remote added but CLI remote failed: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Run: cd %s && dolt remote add %s %s\n",
|
|
doltutil.ShellQuote(dbPath), doltutil.ShellQuote(name), doltutil.ShellQuote(url))
|
|
}
|
|
}
|
|
|
|
// Persist the origin remote URL to config.yaml as sync.remote
|
|
// so fresh clones can bootstrap from it (the Dolt database is
|
|
// gitignored and won't survive clone).
|
|
if name == "origin" {
|
|
if err := config.SetYamlConfig("sync.remote", url); err != nil {
|
|
FatalError("failed to persist sync.remote to config.yaml: %v", err)
|
|
}
|
|
// Auto-commit the config change so it's not left dirty.
|
|
if isGitRepo() {
|
|
commitBeadsConfig("bd: update sync.remote")
|
|
}
|
|
}
|
|
|
|
suffix := "(SQL + CLI)"
|
|
if cliFailed {
|
|
suffix = "(SQL only)"
|
|
}
|
|
if jsonOutput {
|
|
outputJSON(map[string]interface{}{
|
|
"name": name,
|
|
"url": url,
|
|
})
|
|
} else {
|
|
fmt.Printf("Added remote %q → %s %s\n", name, url, suffix)
|
|
}
|
|
},
|
|
}
|
|
|
|
var doltRemoteListCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List configured Dolt remotes (SQL server + CLI)",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
if st == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: no store available\n")
|
|
os.Exit(1)
|
|
}
|
|
locator, ok := storage.UnwrapStore(st).(storage.StoreLocator)
|
|
if !ok {
|
|
fmt.Fprintf(os.Stderr, "Error: storage backend does not support store location\n")
|
|
os.Exit(1)
|
|
}
|
|
dbPath := locator.CLIDir()
|
|
embedded := isEmbeddedMode()
|
|
|
|
sqlRemotes, sqlErr, cliRemotes, cliErr := listRemoteSurfaces(ctx, st, dbPath, embedded)
|
|
if sqlErr != nil {
|
|
if jsonOutput {
|
|
outputJSONError(sqlErr, "remote_list_failed")
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Error listing SQL remotes: %v\n", sqlErr)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Build unified view
|
|
type unifiedRemote struct {
|
|
Name string `json:"name"`
|
|
SQLURL string `json:"sql_url,omitempty"`
|
|
CLIURL string `json:"cli_url,omitempty"`
|
|
Status string `json:"status"` // "ok", "sql_only", "cli_only", "conflict"
|
|
}
|
|
|
|
seen := map[string]*unifiedRemote{}
|
|
var names []string
|
|
for _, r := range sqlRemotes {
|
|
u := &unifiedRemote{Name: r.Name, SQLURL: r.URL}
|
|
seen[r.Name] = u
|
|
names = append(names, r.Name)
|
|
}
|
|
if cliErr == nil {
|
|
for _, r := range cliRemotes {
|
|
if u, ok := seen[r.Name]; ok {
|
|
u.CLIURL = r.URL
|
|
} else {
|
|
seen[r.Name] = &unifiedRemote{Name: r.Name, CLIURL: r.URL}
|
|
names = append(names, r.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Classify each remote
|
|
hasDiscrepancy := false
|
|
var unified []unifiedRemote
|
|
for _, name := range names {
|
|
u := seen[name]
|
|
switch {
|
|
case u.SQLURL != "" && u.CLIURL != "" && u.SQLURL == u.CLIURL:
|
|
u.Status = "ok"
|
|
case u.SQLURL != "" && u.CLIURL != "" && u.SQLURL != u.CLIURL:
|
|
u.Status = "conflict"
|
|
hasDiscrepancy = true
|
|
case u.SQLURL != "" && u.CLIURL == "":
|
|
u.Status = "sql_only"
|
|
hasDiscrepancy = true
|
|
case u.SQLURL == "" && u.CLIURL != "":
|
|
u.Status = "cli_only"
|
|
hasDiscrepancy = true
|
|
}
|
|
unified = append(unified, *u)
|
|
}
|
|
|
|
if jsonOutput {
|
|
outputJSON(unified)
|
|
return
|
|
}
|
|
|
|
if len(unified) == 0 {
|
|
fmt.Println("No remotes configured.")
|
|
return
|
|
}
|
|
|
|
for _, u := range unified {
|
|
url := u.SQLURL
|
|
if url == "" {
|
|
url = u.CLIURL
|
|
}
|
|
switch u.Status {
|
|
case "ok":
|
|
fmt.Printf("%-20s %s\n", u.Name, url)
|
|
case "sql_only":
|
|
fmt.Printf("%-20s %s %s\n", u.Name, url, ui.RenderWarn("[SQL only]"))
|
|
case "cli_only":
|
|
fmt.Printf("%-20s %s %s\n", u.Name, url, ui.RenderWarn("[CLI only]"))
|
|
case "conflict":
|
|
fmt.Printf("%-20s %s\n", u.Name, ui.RenderFail("[CONFLICT]"))
|
|
fmt.Printf("%-20s SQL: %s\n", "", u.SQLURL)
|
|
fmt.Printf("%-20s CLI: %s\n", "", u.CLIURL)
|
|
}
|
|
}
|
|
|
|
if cliErr != nil {
|
|
fmt.Printf("\n%s Could not read CLI remotes: %v\n", ui.RenderWarn("!"), cliErr)
|
|
}
|
|
if hasDiscrepancy {
|
|
if embedded {
|
|
fmt.Printf("\n%s Remote discrepancies detected.\n", ui.RenderWarn("!"))
|
|
} else {
|
|
fmt.Printf("\n%s Remote discrepancies detected. Run 'bd doctor --fix' to resolve.\n", ui.RenderWarn("!"))
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
var doltRemoteRemoveCmd = &cobra.Command{
|
|
Use: "remove <name>",
|
|
Short: "Remove a Dolt remote (both SQL server and CLI)",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
if st == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: no store available\n")
|
|
os.Exit(1)
|
|
}
|
|
name := args[0]
|
|
locator, ok := storage.UnwrapStore(st).(storage.StoreLocator)
|
|
if !ok {
|
|
fmt.Fprintf(os.Stderr, "Error: storage backend does not support store location\n")
|
|
os.Exit(1)
|
|
}
|
|
dbPath := locator.CLIDir()
|
|
embedded := isEmbeddedMode()
|
|
|
|
// Check both surfaces for conflicts
|
|
sqlRemotes, _, cliRemotes, _ := listRemoteSurfaces(ctx, st, dbPath, embedded)
|
|
sqlURL := findRemoteURL(sqlRemotes, name)
|
|
cliURL := findRemoteURL(cliRemotes, name)
|
|
|
|
// Refuse removal if URLs conflict — user must resolve first
|
|
forceRemove, _ := cmd.Flags().GetBool("force")
|
|
if sqlURL != "" && cliURL != "" && sqlURL != cliURL && !forceRemove {
|
|
fmt.Fprintf(os.Stderr, "Error: remote %q has conflicting URLs:\n", name)
|
|
fmt.Fprintf(os.Stderr, " SQL: %s\n CLI: %s\n", sqlURL, cliURL)
|
|
fmt.Fprintf(os.Stderr, "\nResolve the conflict first. To force remove from both:\n")
|
|
fmt.Fprintf(os.Stderr, " bd dolt remote remove %s --force\n", name)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Remove from SQL server
|
|
if sqlURL != "" {
|
|
if err := st.RemoveRemote(ctx, name); err != nil {
|
|
if jsonOutput {
|
|
outputJSONError(err, "remote_remove_failed")
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Error removing SQL remote: %v\n", err)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// Remove from CLI filesystem.
|
|
// In embedded mode, SQL and CLI operate on the same directory,
|
|
// so the SQL remove already cleared the remote config — skip CLI.
|
|
cliRemoveFailed := false
|
|
if !embedded && dbPath == "" {
|
|
cliRemoveFailed = cliURL != ""
|
|
if cliURL != "" {
|
|
fmt.Fprintf(os.Stderr, "Warning: SQL remote removed but CLI remote was not configured: set BEADS_DOLT_CLI_DIR to the local Dolt database path\n")
|
|
}
|
|
} else if !embedded && cliURL != "" {
|
|
if err := doltutil.RemoveCLIRemote(dbPath, name); err != nil {
|
|
cliRemoveFailed = true
|
|
fmt.Fprintf(os.Stderr, "Warning: SQL remote removed but CLI remote failed: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Run: cd %s && dolt remote remove %s\n",
|
|
doltutil.ShellQuote(dbPath), doltutil.ShellQuote(name))
|
|
}
|
|
}
|
|
|
|
if sqlURL == "" && cliURL == "" {
|
|
fmt.Fprintf(os.Stderr, "Error: remote %q not found on either surface\n", name)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Clear sync.remote from config.yaml if the origin remote was removed,
|
|
// so fresh clones don't try to bootstrap from a stale URL.
|
|
if name == "origin" {
|
|
if current := config.GetYamlConfig("sync.remote"); current != "" {
|
|
if err := config.UnsetYamlConfig("sync.remote"); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to clear sync.remote from config.yaml: %v\n", err)
|
|
}
|
|
if isGitRepo() {
|
|
commitBeadsConfig("bd: clear sync.remote")
|
|
}
|
|
}
|
|
}
|
|
|
|
suffix := "(SQL + CLI)"
|
|
if cliRemoveFailed || cliURL == "" {
|
|
suffix = "(SQL only)"
|
|
} else if sqlURL == "" {
|
|
suffix = "(CLI only)"
|
|
}
|
|
if jsonOutput {
|
|
outputJSON(map[string]interface{}{
|
|
"name": name,
|
|
"removed": true,
|
|
})
|
|
} else {
|
|
fmt.Printf("Removed remote %q %s\n", name, suffix)
|
|
}
|
|
},
|
|
}
|
|
|
|
// isTimeoutError checks if an error is a context deadline exceeded or timeout.
|
|
func isTimeoutError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
if err == context.DeadlineExceeded {
|
|
return true
|
|
}
|
|
// Check for net.Error timeout (covers TCP and MySQL driver timeouts)
|
|
var netErr net.Error
|
|
if errors.As(err, &netErr) && netErr.Timeout() {
|
|
return true
|
|
}
|
|
// Also catch wrapped context.DeadlineExceeded
|
|
return errors.Is(err, context.DeadlineExceeded)
|
|
}
|
|
|
|
func init() {
|
|
doltSetCmd.Flags().Bool("update-config", false, "Also write to config.yaml for team-wide defaults")
|
|
doltStopCmd.Flags().Bool("force", false, "Force stop the server")
|
|
doltPushCmd.Flags().Bool("force", false, "Force push (overwrite remote changes)")
|
|
doltPushCmd.Flags().String("remote", "", "Push to a specific named remote instead of the default")
|
|
doltPullCmd.Flags().String("remote", "", "Pull from a specific named remote instead of the default")
|
|
doltCommitCmd.Flags().StringP("message", "m", "", "Commit message (default: auto-generated)")
|
|
doltCleanDatabasesCmd.Flags().Bool("dry-run", false, "Show what would be dropped without dropping")
|
|
doltRemoteRemoveCmd.Flags().Bool("force", false, "Force remove even when SQL and CLI URLs conflict")
|
|
doltRemoteCmd.AddCommand(doltRemoteAddCmd)
|
|
doltRemoteCmd.AddCommand(doltRemoteListCmd)
|
|
doltRemoteCmd.AddCommand(doltRemoteRemoveCmd)
|
|
doltCmd.AddCommand(doltShowCmd)
|
|
doltCmd.AddCommand(doltSetCmd)
|
|
doltCmd.AddCommand(doltTestCmd)
|
|
doltCmd.AddCommand(doltCommitCmd)
|
|
doltCmd.AddCommand(doltPushCmd)
|
|
doltCmd.AddCommand(doltPullCmd)
|
|
doltCmd.AddCommand(doltStartCmd)
|
|
doltCmd.AddCommand(doltStopCmd)
|
|
doltCmd.AddCommand(doltStatusCmd)
|
|
doltCmd.AddCommand(doltKillallCmd)
|
|
doltCmd.AddCommand(doltCleanDatabasesCmd)
|
|
doltCmd.AddCommand(doltRemoteCmd)
|
|
rootCmd.AddCommand(doltCmd)
|
|
}
|
|
|
|
func selectedDoltBeadsDir() string {
|
|
beadsDir := beads.FindBeadsDir()
|
|
if beadsDir == "" {
|
|
return ""
|
|
}
|
|
prepareSelectedNoDBContext(beadsDir)
|
|
return beadsDir
|
|
}
|
|
|
|
func showDoltConfig(testConnection bool) {
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if cfg == nil {
|
|
cfg = configfile.DefaultConfig()
|
|
}
|
|
|
|
backend := cfg.GetBackend()
|
|
embedded := isEmbeddedMode()
|
|
|
|
// Resolve actual server port for connection testing
|
|
showHost := cfg.GetDoltServerHost()
|
|
dsCfg := doltserver.DefaultConfig(beadsDir)
|
|
showPort := dsCfg.Port
|
|
embeddedDataDir := filepath.Join(beadsDir, "embeddeddolt")
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"backend": backend,
|
|
}
|
|
if backend == configfile.BackendDolt {
|
|
result["database"] = cfg.GetDoltDatabase()
|
|
result["embedded"] = embedded
|
|
if embedded {
|
|
result["data_dir"] = embeddedDataDir
|
|
} else {
|
|
result["host"] = showHost
|
|
result["port"] = showPort
|
|
result["user"] = cfg.GetDoltServerUser()
|
|
result["shared_server"] = doltserver.IsSharedServerMode()
|
|
if testConnection {
|
|
result["connection_ok"] = testServerConnection(showHost, showPort)
|
|
}
|
|
}
|
|
}
|
|
outputJSON(result)
|
|
return
|
|
}
|
|
|
|
if backend != configfile.BackendDolt {
|
|
fmt.Printf("Backend: %s\n", backend)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Dolt Configuration")
|
|
fmt.Println("==================")
|
|
fmt.Printf(" Database: %s\n", cfg.GetDoltDatabase())
|
|
if embedded {
|
|
fmt.Println(" Mode: embedded (in-process Dolt engine)")
|
|
fmt.Printf(" Data: %s\n", embeddedDataDir)
|
|
} else {
|
|
fmt.Printf(" Host: %s\n", showHost)
|
|
fmt.Printf(" Port: %d\n", showPort)
|
|
fmt.Printf(" User: %s\n", cfg.GetDoltServerUser())
|
|
if doltserver.IsSharedServerMode() {
|
|
fmt.Println(" Mode: shared server")
|
|
if sharedDir, err := doltserver.SharedServerDir(); err == nil {
|
|
fmt.Printf(" Server: %s\n", sharedDir)
|
|
}
|
|
} else {
|
|
fmt.Println(" Mode: per-project")
|
|
}
|
|
|
|
if testConnection {
|
|
fmt.Println()
|
|
if testServerConnection(showHost, showPort) {
|
|
fmt.Printf(" %s\n", ui.RenderPass("✓ Server connection OK"))
|
|
} else {
|
|
fmt.Printf(" %s\n", ui.RenderWarn("✗ Server not reachable"))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Show remotes from both surfaces
|
|
dbName := cfg.GetDoltDatabase()
|
|
var dbDir string
|
|
if embedded {
|
|
dbDir = filepath.Join(embeddedDataDir, dbName)
|
|
} else {
|
|
dbDir = filepath.Join(doltserver.ResolveDoltDir(beadsDir), dbName)
|
|
}
|
|
fmt.Println("\nRemotes:")
|
|
ctx := context.Background()
|
|
st := getStore()
|
|
var sqlRemotes []string
|
|
if st != nil {
|
|
if remotes, err := st.ListRemotes(ctx); err == nil {
|
|
for _, r := range remotes {
|
|
sqlRemotes = append(sqlRemotes, fmt.Sprintf(" %-16s %s", r.Name, r.URL))
|
|
}
|
|
}
|
|
}
|
|
cliRemotes, cliErr := doltutil.ListCLIRemotes(dbDir)
|
|
if len(sqlRemotes) == 0 && (cliErr != nil || len(cliRemotes) == 0) {
|
|
fmt.Println(" (none)")
|
|
} else {
|
|
// Show SQL remotes
|
|
if len(sqlRemotes) > 0 {
|
|
for _, line := range sqlRemotes {
|
|
fmt.Println(line)
|
|
}
|
|
}
|
|
// Flag CLI-only remotes
|
|
if cliErr == nil {
|
|
sqlNames := map[string]bool{}
|
|
if st != nil {
|
|
if remotes, err := st.ListRemotes(ctx); err == nil {
|
|
for _, r := range remotes {
|
|
sqlNames[r.Name] = true
|
|
}
|
|
}
|
|
}
|
|
for _, r := range cliRemotes {
|
|
if !sqlNames[r.Name] {
|
|
fmt.Printf(" %-16s %s %s\n", r.Name, r.URL, ui.RenderWarn("[CLI only]"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Show config sources
|
|
fmt.Println("\nConfig sources (priority order):")
|
|
fmt.Println(" 1. Environment variables (BEADS_DOLT_*)")
|
|
fmt.Println(" 2. metadata.json (local, gitignored)")
|
|
fmt.Println(" 3. config.yaml (team defaults)")
|
|
}
|
|
|
|
func setDoltConfig(key, value string, updateConfig bool) {
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if cfg == nil {
|
|
cfg = configfile.DefaultConfig()
|
|
}
|
|
|
|
if cfg.GetBackend() != configfile.BackendDolt {
|
|
fmt.Fprintf(os.Stderr, "Error: not using Dolt backend\n")
|
|
os.Exit(1)
|
|
}
|
|
|
|
var yamlKey string
|
|
|
|
switch key {
|
|
case "mode":
|
|
// Mode will be configurable again when embedded Dolt support returns.
|
|
// For now, server mode is required (embedded driver not yet re-integrated).
|
|
fmt.Fprintf(os.Stderr, "Error: mode is not yet configurable; embedded mode is coming soon\n")
|
|
os.Exit(1)
|
|
|
|
case "database":
|
|
if value == "" {
|
|
fmt.Fprintf(os.Stderr, "Error: database name cannot be empty\n")
|
|
os.Exit(1)
|
|
}
|
|
cfg.DoltDatabase = value
|
|
yamlKey = "dolt.database"
|
|
|
|
case "host":
|
|
if value == "" {
|
|
fmt.Fprintf(os.Stderr, "Error: host cannot be empty\n")
|
|
os.Exit(1)
|
|
}
|
|
cfg.DoltServerHost = value
|
|
yamlKey = "dolt.host"
|
|
|
|
case "port":
|
|
port, err := strconv.Atoi(value)
|
|
if err != nil || port <= 0 || port > 65535 {
|
|
fmt.Fprintf(os.Stderr, "Error: port must be a valid port number (1-65535)\n")
|
|
os.Exit(1)
|
|
}
|
|
cfg.DoltServerPort = port
|
|
yamlKey = "dolt.port"
|
|
|
|
case "socket":
|
|
// Empty value clears the socket (reverts to TCP host/port).
|
|
cfg.DoltServerSocket = value
|
|
yamlKey = "dolt.socket"
|
|
|
|
case "user":
|
|
if value == "" {
|
|
fmt.Fprintf(os.Stderr, "Error: user cannot be empty\n")
|
|
os.Exit(1)
|
|
}
|
|
cfg.DoltServerUser = value
|
|
yamlKey = "dolt.user"
|
|
|
|
case "data-dir":
|
|
// GH#2438: In server mode, data-dir has no effect on which database
|
|
// the server connects to. Setting it silently switches the local
|
|
// resolution path without affecting the running server, causing
|
|
// commands to operate on the wrong (often empty) database.
|
|
if value != "" && cfg.IsDoltServerMode() {
|
|
fmt.Fprintf(os.Stderr, "Error: setting data-dir in server mode is not supported (GH#2438).\n")
|
|
fmt.Fprintf(os.Stderr, "In server mode, the database is determined by the 'database' config key,\n")
|
|
fmt.Fprintf(os.Stderr, "not the local data directory. Setting data-dir would silently disconnect\n")
|
|
fmt.Fprintf(os.Stderr, "from the configured database '%s'.\n", cfg.GetDoltDatabase())
|
|
fmt.Fprintf(os.Stderr, "\nTo change which database to use:\n")
|
|
fmt.Fprintf(os.Stderr, " bd dolt set database <name>\n")
|
|
os.Exit(1)
|
|
}
|
|
if value == "" {
|
|
// Allow clearing the custom data dir (revert to default .beads/dolt)
|
|
cfg.DoltDataDir = ""
|
|
} else {
|
|
if !filepath.IsAbs(value) {
|
|
fmt.Fprintf(os.Stderr, "Error: data-dir must be an absolute path\n")
|
|
os.Exit(1)
|
|
}
|
|
cfg.DoltDataDir = value
|
|
// Absolute paths are machine-specific and won't be persisted to
|
|
// metadata.json (which is committed to git). Use the env var for
|
|
// persistence across sessions. (GH#2251)
|
|
fmt.Fprintf(os.Stderr, "Note: absolute paths are not saved to metadata.json (it propagates via git).\n")
|
|
fmt.Fprintf(os.Stderr, "For persistence, add to your shell profile:\n")
|
|
fmt.Fprintf(os.Stderr, " export BEADS_DOLT_DATA_DIR=%s\n", value)
|
|
}
|
|
yamlKey = "dolt.data-dir"
|
|
|
|
case "shared-server":
|
|
lower := strings.ToLower(value)
|
|
if lower != "true" && lower != "false" {
|
|
fmt.Fprintf(os.Stderr, "Error: shared-server must be 'true' or 'false'\n")
|
|
os.Exit(1)
|
|
}
|
|
// shared-server is yaml-only (not stored in metadata.json)
|
|
if err := config.SetYamlConfig("dolt.shared-server", lower); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error setting shared-server: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if jsonOutput {
|
|
outputJSON(map[string]interface{}{
|
|
"key": "shared-server",
|
|
"value": lower,
|
|
"location": "config.yaml",
|
|
})
|
|
return
|
|
}
|
|
if lower == "true" {
|
|
fmt.Println("Shared server mode enabled.")
|
|
fmt.Println("All projects will use a single Dolt server at ~/.beads/shared-server/.")
|
|
fmt.Println("Each project's data remains isolated in its own database.")
|
|
} else {
|
|
fmt.Println("Shared server mode disabled. Each project will use its own Dolt server.")
|
|
}
|
|
return
|
|
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "Error: unknown key '%s'\n", key)
|
|
fmt.Fprintf(os.Stderr, "Valid keys: database, host, port, socket, user, data-dir, shared-server\n")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Audit log: record who changed what
|
|
logDoltConfigChange(beadsDir, key, value)
|
|
|
|
// Save to metadata.json
|
|
if err := cfg.Save(beadsDir); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error saving config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"key": key,
|
|
"value": value,
|
|
"location": "metadata.json",
|
|
}
|
|
if updateConfig {
|
|
result["config_yaml_updated"] = true
|
|
}
|
|
outputJSON(result)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Set %s = %s (in metadata.json)\n", key, value)
|
|
|
|
// Also update config.yaml if requested
|
|
if updateConfig && yamlKey != "" {
|
|
if err := config.SetYamlConfig(yamlKey, value); err != nil {
|
|
fmt.Printf("%s\n", ui.RenderWarn(fmt.Sprintf("Warning: failed to update config.yaml: %v", err)))
|
|
} else {
|
|
fmt.Printf("Set %s = %s (in config.yaml)\n", yamlKey, value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testDoltConnection() {
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if cfg == nil {
|
|
cfg = configfile.DefaultConfig()
|
|
}
|
|
|
|
if cfg.GetBackend() != configfile.BackendDolt {
|
|
fmt.Fprintf(os.Stderr, "Error: not using Dolt backend\n")
|
|
os.Exit(1)
|
|
}
|
|
|
|
host := cfg.GetDoltServerHost()
|
|
port := doltserver.DefaultConfig(beadsDir).Port
|
|
addr := fmt.Sprintf("%s:%d", host, port)
|
|
|
|
if jsonOutput {
|
|
ok := testServerConnection(host, port)
|
|
outputJSON(map[string]interface{}{
|
|
"host": host,
|
|
"port": port,
|
|
"connection_ok": ok,
|
|
})
|
|
if !ok {
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Testing connection to %s...\n", addr)
|
|
|
|
if testServerConnection(host, port) {
|
|
fmt.Printf("%s\n", ui.RenderPass("✓ Connection successful"))
|
|
} else {
|
|
fmt.Printf("%s\n", ui.RenderWarn("✗ Connection failed"))
|
|
fmt.Println("\nStart the server with: bd dolt start")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Test remote connectivity
|
|
st := getStore()
|
|
if st == nil {
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
remotes, err := st.ListRemotes(ctx)
|
|
if err != nil || len(remotes) == 0 {
|
|
return
|
|
}
|
|
fmt.Println("\nRemote connectivity:")
|
|
for _, r := range remotes {
|
|
if doltutil.IsSSHURL(r.URL) {
|
|
// Test SSH connectivity by parsing host from URL
|
|
sshHost := extractSSHHost(r.URL)
|
|
if sshHost != "" {
|
|
fmt.Printf(" %s (%s)... ", r.Name, r.URL)
|
|
if testSSHConnectivity(sshHost) {
|
|
fmt.Printf("%s\n", ui.RenderPass("✓ reachable"))
|
|
} else {
|
|
fmt.Printf("%s\n", ui.RenderWarn("✗ unreachable"))
|
|
}
|
|
}
|
|
} else if strings.HasPrefix(r.URL, "https://") || strings.HasPrefix(r.URL, "http://") {
|
|
fmt.Printf(" %s (%s)... ", r.Name, r.URL)
|
|
if testHTTPConnectivity(r.URL) {
|
|
fmt.Printf("%s\n", ui.RenderPass("✓ reachable"))
|
|
} else {
|
|
fmt.Printf("%s\n", ui.RenderWarn("✗ unreachable"))
|
|
}
|
|
} else {
|
|
fmt.Printf(" %s (%s)... skipped (no connectivity test for this scheme)\n", r.Name, r.URL)
|
|
}
|
|
}
|
|
}
|
|
|
|
// serverDialTimeout controls the TCP dial timeout for server connection tests.
|
|
// Tests may reduce this to avoid slow unreachable-host hangs in CI.
|
|
var serverDialTimeout = 3 * time.Second
|
|
|
|
func testServerConnection(host string, port int) bool {
|
|
addr := net.JoinHostPort(host, strconv.Itoa(port))
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, serverDialTimeout)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_ = conn.Close() // Best effort cleanup
|
|
return true
|
|
}
|
|
|
|
// extractSSHHost extracts the hostname from an SSH URL for connectivity testing.
|
|
func extractSSHHost(url string) string {
|
|
// git+ssh://git@github.com/org/repo.git → github.com
|
|
// ssh://git@github.com/org/repo.git → github.com
|
|
// git@github.com:org/repo.git → github.com
|
|
url = strings.TrimPrefix(url, "git+ssh://")
|
|
url = strings.TrimPrefix(url, "ssh://")
|
|
if idx := strings.Index(url, "@"); idx >= 0 {
|
|
url = url[idx+1:]
|
|
}
|
|
// Handle colon-separated (git@host:path) or slash-separated (ssh://host/path)
|
|
if idx := strings.Index(url, ":"); idx >= 0 && !strings.Contains(url[:idx], "/") {
|
|
return url[:idx]
|
|
}
|
|
if idx := strings.Index(url, "/"); idx >= 0 {
|
|
return url[:idx]
|
|
}
|
|
return url
|
|
}
|
|
|
|
// testSSHConnectivity tests if an SSH host is reachable on port 22.
|
|
func testSSHConnectivity(host string) bool {
|
|
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, "22"), 5*time.Second)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_ = conn.Close()
|
|
return true
|
|
}
|
|
|
|
// httpURLToTCPAddr extracts a TCP dial address (host:port) from an HTTP(S) URL.
|
|
// Handles IPv6 addresses correctly (e.g., https://[::1]:8080/path).
|
|
func httpURLToTCPAddr(url string) string {
|
|
host := url
|
|
host = strings.TrimPrefix(host, "https://")
|
|
host = strings.TrimPrefix(host, "http://")
|
|
if idx := strings.Index(host, "/"); idx >= 0 {
|
|
host = host[:idx]
|
|
}
|
|
defaultPort := "443"
|
|
if strings.HasPrefix(url, "http://") {
|
|
defaultPort = "80"
|
|
}
|
|
// Use net.SplitHostPort to correctly handle IPv6 addresses (which
|
|
// contain colons that would otherwise be confused with host:port).
|
|
if h, p, err := net.SplitHostPort(host); err == nil {
|
|
return net.JoinHostPort(h, p)
|
|
}
|
|
// No port in host string. Strip IPv6 brackets if present so
|
|
// JoinHostPort can re-add them correctly.
|
|
h := strings.TrimPrefix(host, "[")
|
|
h = strings.TrimSuffix(h, "]")
|
|
return net.JoinHostPort(h, defaultPort)
|
|
}
|
|
|
|
// testHTTPConnectivity tests if an HTTP(S) URL is reachable via TCP.
|
|
func testHTTPConnectivity(url string) bool {
|
|
addr := httpURLToTCPAddr(url)
|
|
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_ = conn.Close()
|
|
return true
|
|
}
|
|
|
|
// openDoltServerConnection opens a direct MySQL connection to the Dolt server
|
|
// using config from the beads directory. This bypasses getStore() which isn't
|
|
// initialized for dolt subcommands (beads-9vt). Connects without selecting a
|
|
// database so callers can operate on all databases (SHOW DATABASES, DROP DATABASE).
|
|
func openDoltServerConnection() (*sql.DB, func()) {
|
|
beadsDir := selectedDoltBeadsDir()
|
|
if beadsDir == "" {
|
|
FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
|
}
|
|
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if cfg == nil {
|
|
cfg = configfile.DefaultConfig()
|
|
}
|
|
|
|
host := cfg.GetDoltServerHost()
|
|
port := doltserver.DefaultConfig(beadsDir).Port
|
|
user := cfg.GetDoltServerUser()
|
|
password := os.Getenv("BEADS_DOLT_PASSWORD")
|
|
|
|
connStr := doltutil.ServerDSN{
|
|
Host: host,
|
|
Port: port,
|
|
User: user,
|
|
Password: password,
|
|
TLS: cfg.GetDoltServerTLS(),
|
|
}.String()
|
|
|
|
db, err := sql.Open("mysql", connStr)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error connecting to Dolt server: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
db.SetMaxOpenConns(2)
|
|
db.SetMaxIdleConns(1)
|
|
db.SetConnMaxLifetime(30 * time.Second)
|
|
|
|
// Verify connectivity
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := db.PingContext(ctx); err != nil {
|
|
_ = db.Close()
|
|
fmt.Fprintf(os.Stderr, "Error: cannot reach Dolt server at %s:%d: %v\n", host, port, err)
|
|
fmt.Fprintln(os.Stderr, "Start the server with: bd dolt start")
|
|
os.Exit(1)
|
|
}
|
|
|
|
return db, func() { _ = db.Close() }
|
|
}
|
|
|
|
// doltServerPidFile returns the path to the PID file for the managed dolt server.
|
|
// logDoltConfigChange appends an audit entry to .beads/dolt-config.log.
|
|
// Includes the beadsDir path for debugging worktree config pollution (bd-la2cl).
|
|
func logDoltConfigChange(beadsDir, key, value string) {
|
|
logPath := filepath.Join(beadsDir, "dolt-config.log")
|
|
actor := os.Getenv("BEADS_ACTOR")
|
|
if actor == "" {
|
|
actor = os.Getenv("BD_ACTOR") // deprecated fallback
|
|
}
|
|
if actor == "" {
|
|
actor = "unknown"
|
|
}
|
|
entry := fmt.Sprintf("%s actor=%s key=%s value=%s beads_dir=%s\n",
|
|
time.Now().UTC().Format(time.RFC3339), actor, key, value, beadsDir)
|
|
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
|
if err != nil {
|
|
return // best effort
|
|
}
|
|
defer f.Close()
|
|
_, _ = f.WriteString(entry)
|
|
}
|