contexts, and adopting by name: loomctl external add loom/cart cart

The config becomes kubectl-shaped — named contexts with one current, each holding a
host, its flavor and a read-only token — and a bare owner/repo resolves against it.
The point is where the details live: a host's raw-file route belongs to the host so
it sits in the context, the published directory belongs to the convention so it
sits in the code, and what is left is which repository and which document, which is
the only part a person knows.

Measured rather than assumed, because the three hosts differ. Gitea redirects its
short raw form to the resolved branch, so the lock records a branch without anybody
naming one. GitHub and GitLab accept HEAD and do not redirect, which would put a
moving ref in the lock — the hazard already recorded and nearly built anyway — so
those resolve the default branch with git ls-remote --symref first, one round trip
and no clone.

That supersedes the claim that list is the only command needing git, which is now
wrong for two of three flavors and would otherwise read as still true.

Adds loomctl config, which says which context is current and where the credential
came from without printing it, so that "my token is not being used" is answerable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018UTxuSizozEA8yDitPuris
This commit is contained in:
2026-09-07 16:57:39 -04:00
co-authored by Claude Opus 5
parent 104cbd050c
commit 73b3a97dce
4 changed files with 295 additions and 29 deletions
+94 -11
View File
@@ -9,15 +9,21 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"sort"
"strings"
"git.hypertheory-labs.dev/loom/loom-cli/internal/config"
"git.hypertheory-labs.dev/loom/loom-cli/internal/external"
)
const usage = `loomctl — fetch what you depend on, and find out when it changed.
loomctl external list <repo-url> what a repository publishes
loomctl external add <url> [--path p] adopt one document and lock it
loomctl external list <owner/repo> what a repository publishes
loomctl external add <owner/repo> <doc> adopt one document and lock it
loomctl external add <url> [--path p] ... or by its full raw URL
loomctl external check ask every publisher whether theirs moved
loomctl external apply [path...] move a staged polad into place, lock and all
@@ -41,9 +47,24 @@ Adopted documents live in .loom/externals/<host>/<owner>/<repo>/<name>.md, and
their origins in .loom/externals/.locks. The path is for a person to read; the
lock is what a machine uses, because the path does not round-trip to a URL.
Credentials are read-only and per host, in ~/.config/loomctl/config.json or in
LOOMCTL_TOKEN_<HOST>. loomctl never writes over the network, so a token it is
given should never carry write scope.
loomctl config which context is current, and from where
A bare owner/repo is resolved against the current context in
~/.config/loomctl/config.json, which holds a host, its flavor and a read-only
token. The host's raw-file route lives there so that nobody has to type it, and
LOOMCTL_TOKEN_<HOST> overrides the file for one invocation. loomctl never writes
over the network, so a token it is given should never carry write scope.
{
"current-context": "hypertheory",
"contexts": {
"hypertheory": {
"host": "git.hypertheory-labs.dev",
"flavor": "gitea",
"token": "..."
}
}
}
Requires git on PATH, for list only.
`
@@ -63,6 +84,8 @@ func run(args []string) error {
switch args[0] {
case "external":
return runExternal(args[1:])
case "config":
return showConfig(os.Stdout)
default:
return fmt.Errorf("unknown command %q\n\n%s", args[0], usage)
}
@@ -75,9 +98,12 @@ func runExternal(args []string) error {
switch args[0] {
case "list":
if len(args) != 2 {
return fmt.Errorf("usage: loomctl external list <repo-url>")
return fmt.Errorf("usage: loomctl external list <owner/repo>")
}
return external.List(args[1], os.Stdout)
if strings.Contains(args[1], "://") {
return external.List(args[1], os.Stdout)
}
return external.ListByName(args[1], os.Stdout)
case "add":
fs := flag.NewFlagSet("add", flag.ContinueOnError)
@@ -85,14 +111,23 @@ func runExternal(args []string) error {
if err := fs.Parse(args[1:]); err != nil {
return err
}
if fs.NArg() != 1 {
return fmt.Errorf("usage: loomctl external add <url> [--path p]")
}
root, err := root()
if err != nil {
return err
}
return external.Add(root, fs.Arg(0), *path, os.Stdout)
switch fs.NArg() {
case 1:
if !strings.Contains(fs.Arg(0), "://") {
return fmt.Errorf("adopting by name needs the document too: " +
"loomctl external add <owner/repo> <doc>")
}
return external.Add(root, fs.Arg(0), *path, os.Stdout)
case 2:
return external.AddByName(root, fs.Arg(0), fs.Arg(1), os.Stdout)
default:
return fmt.Errorf("usage: loomctl external add <owner/repo> <doc>, " +
"or loomctl external add <url> [--path p]")
}
case "check":
root, err := root()
@@ -113,6 +148,54 @@ func runExternal(args []string) error {
}
}
// showConfig says which context is current and where it came from, so that "my
// token is not being used" is answerable without printing the token.
func showConfig(out io.Writer) error {
cfg, err := config.Load()
if err != nil {
return err
}
fmt.Fprintf(out, "config %s\n", config.Path())
if len(cfg.Contexts) == 0 {
fmt.Fprintln(out, " (none — public repositories still work by full URL)")
return nil
}
names := make([]string, 0, len(cfg.Contexts))
for n := range cfg.Contexts {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
c := cfg.Contexts[n]
marker := " "
if n == cfg.CurrentContext {
marker = "*"
}
flavor := c.Flavor
if flavor == "" {
flavor = "gitea (default)"
}
fmt.Fprintf(out, "%s %-14s %-32s %-16s %s\n", marker, n, c.Host, flavor, credential(cfg, c))
}
if _, err := cfg.Current(); err != nil {
fmt.Fprintf(out, "\n%v\n", err)
}
return nil
}
func credential(cfg *config.Config, c config.Context) string {
if os.Getenv("LOOMCTL_TOKEN") != "" {
return "token from LOOMCTL_TOKEN"
}
if cfg.TokenFor(c.Host) != "" {
if c.Token == "" {
return "token from the environment"
}
return "token set"
}
return "no token (anonymous)"
}
func root() (string, error) {
wd, err := os.Getwd()
if err != nil {