Add the catalogue and a Platform client
The catalogue lists Welcome and Scratchpad. The Platform client sets env, creates instances (409 counts as success) and deletes them, with errors that carry the call name and status but never the response body. A Fake records calls and can be told to fail a named call.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
8 files changed,
+428
−0
docs/copy-pending.md+2 −0internal/catalog/catalog.go+46 −0internal/catalog/catalog.json+18 −0internal/catalog/catalog_test.go+36 −0internal/platform/client.go+15 −0internal/platform/fake.go+78 −0internal/platform/http.go+107 −0internal/platform/http_test.go+126 −0
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index f08a51c..dceaf8a 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -58,3 +58,5 @@ People and invitations: |
| | internal/web/people.go | Invitation email, body | You've been invited to {Base name}'s Base, where the team's apps live.<br><br>Sign in with this email address to get started:<br>{Base origin}/signin | |
| | internal/web/people.go | Member tries to invite (403) | forbidden | |
| | internal/web/people.go | People list or invite failed (503) | unavailable | |
| +| internal/catalog/catalog.json | Catalogue, Welcome summary | A short tour of your team's Base. | |
| +| internal/catalog/catalog.json | Catalogue, Scratchpad summary | A shared page for quick notes. | |
diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go| new file mode 100644 |
| index 0000000..6444d45 |
| --- /dev/null |
| +++ b/internal/catalog/catalog.go |
| @@ -0,0 +1,46 @@ |
| +// Package catalog lists the apps Base can install. The list is fixed at |
| +// build time; an operator enables each app on the Platform account once. |
| +package catalog |
| + |
| +import ( |
| + _ "embed" |
| + "encoding/json" |
| +) |
| + |
| +// Entry describes one installable app. ID doubles as a DNS label part, so it |
| +// is lowercase letters, digits and hyphens. Tint is one of "amber", "teal", |
| +// "violet", "mist" or "rose". Icon is inline SVG markup. |
| +type Entry struct { |
| + ID string `json:"id"` |
| + Name string `json:"name"` |
| + Summary string `json:"summary"` |
| + Tint string `json:"tint"` |
| + Icon string `json:"icon"` |
| + Default bool `json:"default"` |
| +} |
| + |
| +//go:embed catalog.json |
| +var raw []byte |
| + |
| +var entries = mustLoad() |
| + |
| +func mustLoad() []Entry { |
| + var e []Entry |
| + if err := json.Unmarshal(raw, &e); err != nil { |
| + panic("catalog: bad catalog.json: " + err.Error()) |
| + } |
| + return e |
| +} |
| + |
| +// All returns the catalogue in display order. The caller may modify the copy. |
| +func All() []Entry { return append([]Entry(nil), entries...) } |
| + |
| +// Get returns the entry with the given ID. |
| +func Get(id string) (Entry, bool) { |
| + for _, e := range entries { |
| + if e.ID == id { |
| + return e, true |
| + } |
| + } |
| + return Entry{}, false |
| +} |
diff --git a/internal/catalog/catalog.json b/internal/catalog/catalog.json| new file mode 100644 |
| index 0000000..2900c15 |
| --- /dev/null |
| +++ b/internal/catalog/catalog.json |
| @@ -0,0 +1,18 @@ |
| +[ |
| + { |
| + "id": "welcome", |
| + "name": "Welcome", |
| + "summary": "A short tour of your team's Base.", |
| + "tint": "amber", |
| + "icon": "<svg viewBox=\"0 0 24 24\"><path d=\"M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2.5 2.5M15.5 15.5 18 18M6 18l2.5-2.5M15.5 8.5 18 6\"/></svg>", |
| + "default": true |
| + }, |
| + { |
| + "id": "scratchpad", |
| + "name": "Scratchpad", |
| + "summary": "A shared page for quick notes.", |
| + "tint": "rose", |
| + "icon": "<svg viewBox=\"0 0 24 24\"><path d=\"M6 3h9l4 4v13a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1z\"/><path d=\"M15 3v4h4M8.5 12h7M8.5 15.5h5\"/></svg>", |
| + "default": false |
| + } |
| +] |
diff --git a/internal/catalog/catalog_test.go b/internal/catalog/catalog_test.go| new file mode 100644 |
| index 0000000..9e7f8f5 |
| --- /dev/null |
| +++ b/internal/catalog/catalog_test.go |
| @@ -0,0 +1,36 @@ |
| +package catalog |
| + |
| +import ( |
| + "regexp" |
| + "testing" |
| +) |
| + |
| +var label = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`) |
| + |
| +func TestCatalogIDsAreLabels(t *testing.T) { |
| + if len(All()) == 0 { |
| + t.Fatal("empty catalogue") |
| + } |
| + for _, e := range All() { |
| + if !label.MatchString(e.ID) || len(e.ID) > 20 { |
| + t.Errorf("bad id %q", e.ID) |
| + } |
| + if e.Name == "" || e.Summary == "" || e.Icon == "" { |
| + t.Errorf("%s incomplete", e.ID) |
| + } |
| + } |
| +} |
| + |
| +func TestGet(t *testing.T) { |
| + w, ok := Get("welcome") |
| + if !ok || !w.Default || w.Tint != "amber" { |
| + t.Fatalf("welcome: %+v %v", w, ok) |
| + } |
| + s, ok := Get("scratchpad") |
| + if !ok || s.Default || s.Tint != "rose" { |
| + t.Fatalf("scratchpad: %+v %v", s, ok) |
| + } |
| + if _, ok := Get("nope"); ok { |
| + t.Fatal("unexpected") |
| + } |
| +} |
diff --git a/internal/platform/client.go b/internal/platform/client.go| new file mode 100644 |
| index 0000000..7071bf3 |
| --- /dev/null |
| +++ b/internal/platform/client.go |
| @@ -0,0 +1,15 @@ |
| +// Package platform talks to the CARLOS Platform account Base runs on. |
| +package platform |
| + |
| +import "context" |
| + |
| +// Client is the part of Platform's CLI API that Base uses. |
| +type Client interface { |
| + // SetEnv sets plain environment variables in an app's config environment. |
| + SetEnv(ctx context.Context, app, environment string, vars map[string]string) error |
| + // CreateInstance creates an instance of app at host, bound to the config |
| + // environment. An instance that already exists (409) counts as success. |
| + CreateInstance(ctx context.Context, app, host, environment string) error |
| + // DeleteInstance retires the instance's route. Its data is kept. |
| + DeleteInstance(ctx context.Context, app, host string) error |
| +} |
diff --git a/internal/platform/fake.go b/internal/platform/fake.go| new file mode 100644 |
| index 0000000..894f732 |
| --- /dev/null |
| +++ b/internal/platform/fake.go |
| @@ -0,0 +1,78 @@ |
| +package platform |
| + |
| +import ( |
| + "context" |
| + "maps" |
| + "sync" |
| +) |
| + |
| +// Call is one recorded call to a Fake. Method is "SetEnv", "CreateInstance" |
| +// or "DeleteInstance"; unused fields are zero. |
| +type Call struct { |
| + Method string |
| + App string |
| + Host string |
| + Environment string |
| + Vars map[string]string |
| +} |
| + |
| +// Fake is an in-memory Client for tests. It is safe for concurrent use. |
| +// |
| +// f := platform.NewFake() |
| +// f.Fail("CreateInstance", errBoom) // that call now returns errBoom |
| +// f.Fail("CreateInstance", nil) // clears it |
| +// f.Calls() // every call so far, in order |
| +// |
| +// A failing call is still recorded. |
| +type Fake struct { |
| + mu sync.Mutex |
| + calls []Call |
| + fail map[string]error |
| +} |
| + |
| +var _ Client = (*Fake)(nil) |
| + |
| +// NewFake returns an empty Fake. |
| +func NewFake() *Fake { return &Fake{fail: map[string]error{}} } |
| + |
| +// Fail makes the named method return err from now on. A nil err clears it. |
| +func (f *Fake) Fail(method string, err error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if err == nil { |
| + delete(f.fail, method) |
| + return |
| + } |
| + f.fail[method] = err |
| +} |
| + |
| +// Calls returns a copy of the recorded calls, oldest first. |
| +func (f *Fake) Calls() []Call { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + out := make([]Call, len(f.calls)) |
| + for i, c := range f.calls { |
| + c.Vars = maps.Clone(c.Vars) |
| + out[i] = c |
| + } |
| + return out |
| +} |
| + |
| +func (f *Fake) record(c Call) error { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls = append(f.calls, c) |
| + return f.fail[c.Method] |
| +} |
| + |
| +func (f *Fake) SetEnv(_ context.Context, app, environment string, vars map[string]string) error { |
| + return f.record(Call{Method: "SetEnv", App: app, Environment: environment, Vars: maps.Clone(vars)}) |
| +} |
| + |
| +func (f *Fake) CreateInstance(_ context.Context, app, host, environment string) error { |
| + return f.record(Call{Method: "CreateInstance", App: app, Host: host, Environment: environment}) |
| +} |
| + |
| +func (f *Fake) DeleteInstance(_ context.Context, app, host string) error { |
| + return f.record(Call{Method: "DeleteInstance", App: app, Host: host}) |
| +} |
diff --git a/internal/platform/http.go b/internal/platform/http.go| new file mode 100644 |
| index 0000000..bead839 |
| --- /dev/null |
| +++ b/internal/platform/http.go |
| @@ -0,0 +1,107 @@ |
| +package platform |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "fmt" |
| + "io" |
| + "net/http" |
| + "net/url" |
| + "strings" |
| +) |
| + |
| +type httpClient struct { |
| + console, account string |
| + instanceToken, opToken string |
| + hc *http.Client |
| +} |
| + |
| +// NewHTTP returns a Client for the Platform console at console (a base URL), |
| +// acting on the account with the given sqid. CreateInstance and |
| +// DeleteInstance use instanceToken; SetEnv uses operateToken. If hc is nil, |
| +// http.DefaultClient is used. |
| +func NewHTTP(console, account, instanceToken, operateToken string, hc *http.Client) Client { |
| + if hc == nil { |
| + hc = http.DefaultClient |
| + } |
| + return &httpClient{strings.TrimRight(console, "/"), account, instanceToken, operateToken, hc} |
| +} |
| + |
| +func (c *httpClient) appURL(app, rest string) string { |
| + return c.console + "/api/cli/apps/" + url.PathEscape(c.account) + "/" + url.PathEscape(app) + rest |
| +} |
| + |
| +// do sends one request. Errors name the call and the status only: a response |
| +// body can echo secrets. |
| +func (c *httpClient) do(ctx context.Context, call, method, u, token, message string, body any, okStatus ...int) error { |
| + var rd io.Reader |
| + if body != nil { |
| + b, err := json.Marshal(body) |
| + if err != nil { |
| + return fmt.Errorf("platform %s: %w", call, err) |
| + } |
| + rd = bytes.NewReader(b) |
| + } |
| + req, err := http.NewRequestWithContext(ctx, method, u, rd) |
| + if err != nil { |
| + return fmt.Errorf("platform %s: bad request", call) |
| + } |
| + req.Header.Set("Authorization", "Bearer "+token) |
| + if body != nil { |
| + req.Header.Set("Content-Type", "application/json") |
| + } |
| + if message != "" { |
| + req.Header.Set("X-Carlos-Message", message) |
| + } |
| + resp, err := c.hc.Do(req) |
| + if err != nil { |
| + // url.Error repeats the URL, which holds no secret; drop it anyway. |
| + if ue, ok := err.(*url.Error); ok { |
| + err = ue.Err |
| + } |
| + return fmt.Errorf("platform %s: %w", call, err) |
| + } |
| + defer resp.Body.Close() |
| + io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<20)) |
| + if resp.StatusCode/100 == 2 { |
| + return nil |
| + } |
| + for _, s := range okStatus { |
| + if resp.StatusCode == s { |
| + return nil |
| + } |
| + } |
| + return fmt.Errorf("platform %s: status %d", call, resp.StatusCode) |
| +} |
| + |
| +type envVar struct { |
| + Kind string `json:"kind"` |
| + Value string `json:"value"` |
| +} |
| + |
| +func (c *httpClient) SetEnv(ctx context.Context, app, environment string, vars map[string]string) error { |
| + set := make(map[string]envVar, len(vars)) |
| + for k, v := range vars { |
| + set[k] = envVar{"env", v} |
| + } |
| + body := struct { |
| + Environment string `json:"environment"` |
| + Message string `json:"message"` |
| + Set map[string]envVar `json:"set"` |
| + }{environment, "Base: install", set} |
| + return c.do(ctx, "SetEnv", http.MethodPut, c.appURL(app, "/config"), c.opToken, "", body) |
| +} |
| + |
| +func (c *httpClient) CreateInstance(ctx context.Context, app, host, environment string) error { |
| + body := struct { |
| + Host string `json:"host"` |
| + Environment string `json:"environment"` |
| + Message string `json:"message"` |
| + }{host, environment, "Base: install"} |
| + return c.do(ctx, "CreateInstance", http.MethodPost, c.appURL(app, "/instances"), c.instanceToken, "", body, http.StatusConflict) |
| +} |
| + |
| +func (c *httpClient) DeleteInstance(ctx context.Context, app, host string) error { |
| + return c.do(ctx, "DeleteInstance", http.MethodDelete, c.appURL(app, "/instances/"+url.PathEscape(host)), c.instanceToken, "Base: uninstall", nil) |
| +} |
diff --git a/internal/platform/http_test.go b/internal/platform/http_test.go| new file mode 100644 |
| index 0000000..f95d691 |
| --- /dev/null |
| +++ b/internal/platform/http_test.go |
| @@ -0,0 +1,126 @@ |
| +package platform |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "io" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +type seen struct { |
| + method, path, auth, message string |
| + body map[string]any |
| +} |
| + |
| +func server(t *testing.T, status int, body string) (*httptest.Server, *seen) { |
| + t.Helper() |
| + s := &seen{} |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + s.method, s.path = r.Method, r.URL.Path |
| + s.auth, s.message = r.Header.Get("Authorization"), r.Header.Get("X-Carlos-Message") |
| + if b, _ := io.ReadAll(r.Body); len(b) > 0 { |
| + if err := json.Unmarshal(b, &s.body); err != nil { |
| + t.Errorf("body not JSON: %v", err) |
| + } |
| + } |
| + w.WriteHeader(status) |
| + io.WriteString(w, body) |
| + })) |
| + t.Cleanup(ts.Close) |
| + return ts, s |
| +} |
| + |
| +func TestSetEnvShape(t *testing.T) { |
| + ts, s := server(t, 200, `{}`) |
| + c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| + if err := c.SetEnv(context.Background(), "scratchpad", "base-scratchpad", map[string]string{"K": "V"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if s.method != "PUT" || s.path != "/api/cli/apps/acct1/scratchpad/config" { |
| + t.Fatalf("got %s %s", s.method, s.path) |
| + } |
| + if s.auth != "Bearer op-tok" { |
| + t.Fatalf("auth %q", s.auth) |
| + } |
| + got, _ := json.Marshal(s.body) |
| + want := `{"environment":"base-scratchpad","message":"Base: install","set":{"K":{"kind":"env","value":"V"}}}` |
| + if string(got) != want { |
| + t.Fatalf("body %s", got) |
| + } |
| +} |
| + |
| +func TestCreateInstanceShape(t *testing.T) { |
| + for _, status := range []int{201, 409} { |
| + ts, s := server(t, status, `{}`) |
| + c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| + if err := c.CreateInstance(context.Background(), "scratchpad", "b-scratchpad.example.com", "b-scratchpad"); err != nil { |
| + t.Fatalf("status %d: %v", status, err) |
| + } |
| + if s.method != "POST" || s.path != "/api/cli/apps/acct1/scratchpad/instances" { |
| + t.Fatalf("got %s %s", s.method, s.path) |
| + } |
| + if s.auth != "Bearer inst-tok" { |
| + t.Fatalf("auth %q", s.auth) |
| + } |
| + got, _ := json.Marshal(s.body) |
| + want := `{"environment":"b-scratchpad","host":"b-scratchpad.example.com","message":"Base: install"}` |
| + if string(got) != want { |
| + t.Fatalf("body %s", got) |
| + } |
| + } |
| +} |
| + |
| +func TestDeleteInstanceShape(t *testing.T) { |
| + ts, s := server(t, 200, `{"result":"deleted"}`) |
| + c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| + if err := c.DeleteInstance(context.Background(), "scratchpad", "b-scratchpad.example.com"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if s.method != "DELETE" || s.path != "/api/cli/apps/acct1/scratchpad/instances/b-scratchpad.example.com" { |
| + t.Fatalf("got %s %s", s.method, s.path) |
| + } |
| + if s.auth != "Bearer inst-tok" || s.message != "Base: uninstall" { |
| + t.Fatalf("auth %q message %q", s.auth, s.message) |
| + } |
| +} |
| + |
| +func TestErrorsCarryStatusNotBody(t *testing.T) { |
| + ts, _ := server(t, 403, "SECRET-BODY") |
| + c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| + ctx := context.Background() |
| + errs := map[string]error{ |
| + "CreateInstance": c.CreateInstance(ctx, "a", "h", "e"), |
| + "DeleteInstance": c.DeleteInstance(ctx, "a", "h"), |
| + "SetEnv": c.SetEnv(ctx, "a", "e", map[string]string{"K": "V"}), |
| + } |
| + for name, err := range errs { |
| + if err == nil { |
| + t.Fatalf("%s: want error", name) |
| + } |
| + msg := err.Error() |
| + if !strings.Contains(msg, name) || !strings.Contains(msg, "403") || strings.Contains(msg, "SECRET") { |
| + t.Errorf("%s: error %q", name, msg) |
| + } |
| + } |
| +} |
| + |
| +func TestFakeRecordsAndFails(t *testing.T) { |
| + f := NewFake() |
| + ctx := context.Background() |
| + f.CreateInstance(ctx, "a", "h", "e") |
| + f.Fail("DeleteInstance", io.ErrUnexpectedEOF) |
| + if err := f.DeleteInstance(ctx, "a", "h"); err != io.ErrUnexpectedEOF { |
| + t.Fatalf("got %v", err) |
| + } |
| + f.SetEnv(ctx, "a", "e", map[string]string{"K": "V"}) |
| + calls := f.Calls() |
| + if len(calls) != 3 || calls[0].Method != "CreateInstance" || calls[1].Method != "DeleteInstance" || calls[2].Method != "SetEnv" { |
| + t.Fatalf("calls %+v", calls) |
| + } |
| + if calls[2].Vars["K"] != "V" || calls[0].Host != "h" || calls[0].Environment != "e" { |
| + t.Fatalf("args %+v", calls) |
| + } |
| +} |