Files
loom-cli/internal/config/config.go
T
jeffryandClaude Opus 5 1543df0a0c loomctl external: list, add, check — and its first run found two changed documents
Go, standard library only, with git shelled out for list alone. list enumerates a
publisher's .loom/published by partial clone and ls-tree; add fetches one document,
writes it under .loom/externals and records the resolved origin and the publisher's
ETag in .loom/externals/.locks; check asks conditionally and reports.

The first real run did what the tool exists for. All eight documents adopted by
hand before it existed reported unlocked — the tool refuses to invent a lock by
adopting whatever the remote currently serves, since that would assert the local
copy is the one being served, which is the thing it was about to check. Locking
them fetched two that had moved: bedrock/starting.md, which now says the worked
example is private and will not link to something you cannot fetch, and cart.md,
which went to v1.

cart v1 changes a role we cast: a cart is not committed, because a committed cart
grows a third file by itself — version control does not require anybody to ask, so
the two-file rule is never invoked — and because ignored, gone means gone. Adds
.loom/cart/ to .gitignore and supersedes the isolation entry rather than editing
it. osprey and marmalade are already in history and are left there: rewriting to
honour a rule adopted afterwards costs more than it buys.

Records the conflict this creates rather than settling it: the annotation protocol
here says commit before dissolving because git is the only archive, and an ignored
cart has no archive, so dissolving would destroy the annotations outright.

Credentials are read-only, per host, and passed to git through the environment
rather than argv, because argv is visible to every process on the machine.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018UTxuSizozEA8yDitPuris
2026-09-07 14:48:29 -04:00

85 lines
2.2 KiB
Go

// Package config reads the per-host settings loomctl needs to talk to a git host.
//
// The config is not only a secret. It is how you talk to a host at all, which is
// why it is keyed by host rather than being a single token. It lives in the
// user's home directory and never in a repository — see .loom/event-log.md,
// "decided by fallback: where the credential lives".
package config
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
// Host is what we know about one git host.
type Host struct {
// Token is a read-only personal access token. It must not carry write
// scope: loomctl never writes over the network.
Token string `json:"token,omitempty"`
}
type Config struct {
Hosts map[string]Host `json:"hosts"`
}
// Path is where the config lives. Never inside a repository.
func Path() string {
if p := os.Getenv("LOOMCTL_CONFIG"); p != "" {
return p
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(home, ".config", "loomctl", "config.json")
}
// Load reads the config. A missing file is not an error: everything loomctl does
// against a public repository works with no credential at all.
func Load() (*Config, error) {
c := &Config{Hosts: map[string]Host{}}
p := Path()
if p == "" {
return c, nil
}
b, err := os.ReadFile(p)
if errors.Is(err, fs.ErrNotExist) {
return c, nil
}
if err != nil {
return nil, fmt.Errorf("reading %s: %w", p, err)
}
if err := json.Unmarshal(b, c); err != nil {
return nil, fmt.Errorf("parsing %s: %w", p, err)
}
if c.Hosts == nil {
c.Hosts = map[string]Host{}
}
return c, nil
}
// TokenFor returns the token for a host, or "" if we have none.
//
// An environment variable wins over the file, so a token can be supplied for one
// invocation without ever being written to disk.
func (c *Config) TokenFor(host string) string {
if t := os.Getenv("LOOMCTL_TOKEN_" + envKey(host)); t != "" {
return t
}
if t := os.Getenv("LOOMCTL_TOKEN"); t != "" {
return t
}
return c.Hosts[host].Token
}
// envKey turns a hostname into the shape an environment variable can carry.
func envKey(host string) string {
r := strings.NewReplacer(".", "_", "-", "_", ":", "_")
return strings.ToUpper(r.Replace(host))
}