`bd context` and GET /v0/beads/context both answer "which workspace is this", and they shared nothing: each copied the same seven values out of domain.ContextInfo field by field, so they were free to drift into naming the same workspace differently and nothing would have noticed. domain.PublishedContext is now that projection, once. The HTTP handler fills its wire struct from it and names only the three members that are the SERVER's rather than the workspace's — api version, schema version, capabilities. contextInfoView fills the CLI's view from it and names only the members that are local-only. The exclusions move down with it, and that is the point of putting the projection here rather than beside either surface. PublishedContextFields has no SyncRemote member AT ALL — it is populated unconditionally from the workspace's sync.remote config and remote URLs routinely embed credentials — so a surface reaching identity through this type cannot publish one by forgetting to leave it out. Same for the database bind endpoint and the absolute host paths. The local CLI still prints those, because it prints them to the terminal of whoever owns the workspace, and it reads them off the snapshot beside the projection where that choice is visible. Both of `bd context`'s own routes converge here too: the direct route reads config files itself, because it has to answer in degraded states where no database opens, but it now assembles the same domain.ContextInfo the provider hands the proxied route and passes it to the same view. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/steveyegge/beads/internal/storage/contextinfo"
|
|
"github.com/steveyegge/beads/internal/storage/domain"
|
|
)
|
|
|
|
func runContextProxiedServer(cmd *cobra.Command, ctx context.Context) error {
|
|
if selected := selectedNoDBBeadsDir(cmd); selected != "" {
|
|
prepareSelectedNoDBContext(selected)
|
|
}
|
|
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return contextProxiedError("cannot resolve working directory: %v", err)
|
|
}
|
|
|
|
info, err := contextinfo.NewContextProvider(cwd, Version).ContextUseCase().GetContextInfo(ctx)
|
|
if err != nil {
|
|
return contextProxiedError("cannot resolve context: %v", err)
|
|
}
|
|
|
|
view := contextInfoView(info)
|
|
if jsonOutput {
|
|
return outputJSON(view)
|
|
}
|
|
printContextText(view)
|
|
return nil
|
|
}
|
|
|
|
func contextProxiedError(format string, args ...any) error {
|
|
if jsonOutput {
|
|
if jerr := outputJSON(map[string]string{"error": fmt.Sprintf(format, args...)}); jerr != nil {
|
|
return jerr
|
|
}
|
|
return SilentExit()
|
|
}
|
|
return HandleError(format, args...)
|
|
}
|
|
|
|
// contextInfoView projects a workspace snapshot onto what `bd context` prints
|
|
// and marshals. BOTH routes reach it — the proxied one above and the direct
|
|
// one in context_cmd.go — so there is one answer to "what does bd context
|
|
// say", assembled once.
|
|
//
|
|
// The workspace identity half comes from domain.PublishedContext, the same
|
|
// projection GET /v0/beads/context serves, so the two surfaces cannot name the
|
|
// same workspace differently.
|
|
//
|
|
// Everything below that call is LOCAL-ONLY and deliberately so, which is why
|
|
// it is written out here rather than added to the shared projection. These are
|
|
// operator diagnostics printed to the terminal of whoever owns the workspace:
|
|
// the redirect and worktree flags, the absolute host paths, the database bind
|
|
// endpoint, the role, and the sync remote — which is exactly the member the
|
|
// shared projection has no room for, because a remote URL routinely embeds a
|
|
// credential and an HTTP client is not the person who configured it.
|
|
func contextInfoView(d domain.ContextInfo) ContextInfo {
|
|
published := domain.PublishedContext(d)
|
|
return ContextInfo{
|
|
BdVersion: published.BdVersion,
|
|
Backend: published.Backend,
|
|
DoltMode: published.DoltMode,
|
|
Database: published.Database,
|
|
BeadsDir: published.BeadsDir,
|
|
RepoRoot: published.RepoRoot,
|
|
ProjectID: published.ProjectID,
|
|
|
|
CWDRepoRoot: d.CWDRepoRoot,
|
|
IsRedirected: d.IsRedirected,
|
|
IsWorktree: d.IsWorktree,
|
|
ServerHost: d.ServerHost,
|
|
ServerPort: d.ServerPort,
|
|
ProxiedDir: d.ProxiedDir,
|
|
DataDir: d.DataDir,
|
|
Role: d.Role,
|
|
SyncRemote: d.SyncRemote,
|
|
SyncGitRemote: d.SyncRemote,
|
|
}
|
|
}
|