139 lines
3.7 KiB
Go
139 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
)
|
|
|
|
type exitError struct {
|
|
Code int
|
|
}
|
|
|
|
func (e *exitError) Error() string {
|
|
return fmt.Sprintf("exit code %d", e.Code)
|
|
}
|
|
|
|
func exitCodeFromError(err error) (int, bool) {
|
|
var ee *exitError
|
|
if errors.As(err, &ee) {
|
|
return ee.Code, true
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
func activeWorkspaceNotFoundError() string {
|
|
return "no active beads workspace found"
|
|
}
|
|
|
|
func activeWorkspaceNotFoundMessage() string {
|
|
return "No active beads workspace found."
|
|
}
|
|
|
|
func diagHint() string {
|
|
return workspaceDiagHint(true)
|
|
}
|
|
|
|
func whereDiagHint() string {
|
|
return workspaceDiagHint(false)
|
|
}
|
|
|
|
func workspaceDiagHint(includeWhere bool) string {
|
|
if includeWhere {
|
|
if !usesSQLServer() {
|
|
return "run 'bd where' to inspect the resolved workspace, or 'bd init' to create a new database"
|
|
}
|
|
return "run 'bd where' to inspect the resolved workspace, run 'bd doctor' to diagnose, or 'bd init' to create a new database"
|
|
}
|
|
if !usesSQLServer() {
|
|
return "check BEADS_DIR/worktree setup, or run 'bd init' to create a new database"
|
|
}
|
|
return "check BEADS_DIR/worktree setup, run 'bd doctor' to diagnose, or run 'bd init' to create a new database"
|
|
}
|
|
|
|
func buildJSONError(message, hint string) interface{} {
|
|
inner := map[string]interface{}{
|
|
"error": message,
|
|
}
|
|
if hint != "" {
|
|
inner["hint"] = hint
|
|
}
|
|
if jsonEnvelopeEnabled() {
|
|
return map[string]interface{}{
|
|
"schema_version": JSONSchemaVersion,
|
|
"data": inner,
|
|
}
|
|
}
|
|
inner["schema_version"] = JSONSchemaVersion
|
|
return inner
|
|
}
|
|
|
|
func jsonStderrError(message, hint string) {
|
|
encoder := json.NewEncoder(os.Stderr)
|
|
encoder.SetIndent("", " ")
|
|
_ = encoder.Encode(buildJSONError(message, hint))
|
|
}
|
|
|
|
func jsonStdoutError(message, hint string) {
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
_ = encoder.Encode(buildJSONError(message, hint))
|
|
}
|
|
|
|
func HandleError(format string, args ...interface{}) error {
|
|
fmt.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
|
|
return &exitError{Code: 1}
|
|
}
|
|
|
|
func HandleErrorRespectJSON(format string, args ...interface{}) error {
|
|
if jsonOutput {
|
|
jsonStdoutError(fmt.Sprintf(format, args...), "")
|
|
return &exitError{Code: 1}
|
|
}
|
|
return HandleError(format, args...)
|
|
}
|
|
|
|
func HandleErrorWithHint(message, hint string) error {
|
|
if jsonOutput {
|
|
jsonStderrError(message, hint)
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Error: %s\n", message) //nolint:gosec // G705: stderr, not a browser context
|
|
fmt.Fprintf(os.Stderr, "Hint: %s\n", hint) //nolint:gosec // G705: stderr, not a browser context
|
|
}
|
|
return &exitError{Code: 1}
|
|
}
|
|
|
|
func HandleErrorWithHintRespectJSON(message, hint string) error {
|
|
if jsonOutput {
|
|
jsonStdoutError(message, hint)
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Error: %s\n", message)
|
|
fmt.Fprintf(os.Stderr, "Hint: %s\n", hint)
|
|
}
|
|
return &exitError{Code: 1}
|
|
}
|
|
|
|
func SilentExit() error {
|
|
return &exitError{Code: 1}
|
|
}
|
|
|
|
func WarnError(format string, args ...interface{}) {
|
|
fmt.Fprintf(os.Stderr, "Warning: "+format+"\n", args...)
|
|
}
|
|
|
|
// CheckReadonly aborts the command when bd is running in read-only mode (the
|
|
// worker-sandbox posture, see readonlyMode). It exits via os.Exit and so cannot
|
|
// run the per-command deferred CloseEventAndAdd — a command blocked here records
|
|
// no cli_command event of its own (it never actually ran). It does flush metrics
|
|
// first, so events already queued earlier in this run are still written and
|
|
// scheduled for upload rather than stranded until the next clean exit.
|
|
func CheckReadonly(operation string) {
|
|
if readonlyMode {
|
|
fmt.Fprintf(os.Stderr, "Error: operation '%s' is not allowed in read-only mode\n", operation)
|
|
metrics.CloseAndFlush()
|
|
os.Exit(1)
|
|
}
|
|
}
|