feat(cli): Slice 2 — memoria init + ~/.claude.json writer#2
Merged
Conversation
Adds `memoria init <token>` which validates a Memoria API token, stores it securely (keychain / env-var mode / file), writes the MCP entry into ~/.claude.json, and records the bound tenant + brain in ~/.config/memoria/state.json. New packages: - internal/api: thin HTTP client; Whoami() calls GET /v1/whoami and returns a typed *AuthError (wrapping ErrInvalidToken) on 401. - internal/config/claude_json.go: merge-safe ~/.claude.json writer that preserves all existing top-level keys and sibling MCP servers. Atomic write (tempfile + rename), 0600 perms, error on malformed JSON. - cmd/memoria/init.go: cobra command; uses httptest servers in tests. New tests (8): - internal/api: 200 parse, 401 AuthError, 500 generic, header shape - internal/config: missing file, other keys preserved, sibling servers, overwrite, malformed JSON error, 0600 perms - cmd/memoria: success path (stdout, ~/.claude.json, state.json), 401 path (non-zero exit, stderr, no ~/.claude.json written)
- Drop duplicate config_pkg alias import in init.go - Use errors.Is(err, os.ErrNotExist) instead of deprecated os.IsNotExist - Set User-Agent: memoria-cli/<version> on whoami requests - Bound whoami response body read to 64KB - Tell the user the token was stored when claude.json write fails - Assert Authorization header in init_test fake server - Use 0o700 for parent-dir create to match Slice 1 convention Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
memoria init <token>command that validates a token, stores it, writes~/.claude.json, and records stateinternal/apipackage with a minimal HTTP client (Whoami→GET /v1/whoami)internal/config/claude_json.gowith merge-safe~/.claude.jsoneditingNew commands
Flow:
GET /v1/whoamito validate the token (server PR: premex-ab/memoria#220)Bound to tenant=<id> brain=<id> scopes=<...>to stdoutauth.Store(keychain / env-var / file)mcpServers.memoriaentry into~/.claude.json(merge-safe, 0600 perms)~/.config/memoria/state.jsonwithbound_tenant,bound_brain,token_sourceTest plan
go vet ./...— cleango test ./...— 8 new tests across 3 packages all passinternal/api: 200 parse, 401 →*AuthError+ErrInvalidToken, 500 generic error, correct headerinternal/config: missing file created, other keys preserved, sibling servers preserved, existing entry overwritten, malformed JSON → error, 0600 permscmd/memoria: success path (stdout contains "Bound to tenant=t",~/.claude.jsonwritten,state.jsonhas correct fields), 401 path (non-zero exit, stderr message, no~/.claude.json)go build -o /tmp/memoria ./cmd/memoria— binary builds and--helpoutput is correct