Phase 0: turn Supermarket into a Dockerized local web app
Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// Package core reads the Federated Core connection that CoreDesktop's
|
||||
// `configure core` saved, and works out the Core's service addresses.
|
||||
package core
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Core is the main Core from CoreDesktop's core.conf.
|
||||
type Core struct {
|
||||
Domain string // e.g. example.com
|
||||
Email string // the user's Core email
|
||||
Name string // the user's name
|
||||
}
|
||||
|
||||
// Load reads a core.conf (CORE_DOMAIN=..., CORE_EMAIL=..., CORE_NAME=...,
|
||||
// written with bash's printf %q). It returns nil, nil if the file doesn't
|
||||
// exist: running without a Core is normal.
|
||||
func Load(path string) (*Core, error) {
|
||||
f, err := os.Open(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
c := &Core{}
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
key, val, ok := strings.Cut(strings.TrimSpace(sc.Text()), "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
val = unquote(val)
|
||||
switch key {
|
||||
case "CORE_DOMAIN":
|
||||
c.Domain = val
|
||||
case "CORE_EMAIL":
|
||||
c.Email = val
|
||||
case "CORE_NAME":
|
||||
c.Name = val
|
||||
}
|
||||
}
|
||||
if err := sc.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.Domain == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// unquote undoes the quoting bash's printf %q produces for ordinary values:
|
||||
// backslash escapes, and $'...' or plain single/double quotes.
|
||||
func unquote(s string) string {
|
||||
switch {
|
||||
case strings.HasPrefix(s, "$'") && strings.HasSuffix(s, "'") && len(s) >= 3:
|
||||
s = s[2 : len(s)-1]
|
||||
case len(s) >= 2 && (s[0] == '\'' || s[0] == '"') && s[len(s)-1] == s[0]:
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
var b strings.Builder
|
||||
escaped := false
|
||||
for _, r := range s {
|
||||
if escaped {
|
||||
b.WriteRune(r)
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
if r == '\\' {
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
b.WriteRune(r)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// User is the part of the email before the @, the Core's user name.
|
||||
func (c *Core) User() string {
|
||||
user, _, _ := strings.Cut(c.Email, "@")
|
||||
return user
|
||||
}
|
||||
|
||||
// Service is one of the Core's web services.
|
||||
type Service struct {
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
// Services are the Core services Supermarket uses, by their usual
|
||||
// addresses on a Federated Core.
|
||||
func (c *Core) Services() []Service {
|
||||
return []Service{
|
||||
{"Gitea", "https://gitea." + c.Domain},
|
||||
{"Plausible", "https://plausible." + c.Domain},
|
||||
{"EspoCRM", "https://espocrm." + c.Domain},
|
||||
{"MCP hub", c.MCPHubURL()},
|
||||
}
|
||||
}
|
||||
|
||||
// MCPHubURL is the Core MCP hub's SSE endpoint for this user, which Hermes
|
||||
// connects to.
|
||||
func (c *Core) MCPHubURL() string {
|
||||
return "https://mcphub." + c.Domain + "/sse?as_user=" + c.User()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "core.conf")
|
||||
// As `configure core` writes it, with printf %q.
|
||||
os.WriteFile(path, []byte("CORE_DOMAIN=example.com\nCORE_EMAIL=pat@example.com\nCORE_NAME=Pat\\ O\\'Brien\n"), 0600)
|
||||
c, err := Load(path)
|
||||
if err != nil || c == nil {
|
||||
t.Fatal(c, err)
|
||||
}
|
||||
if c.Domain != "example.com" || c.User() != "pat" || c.Name != "Pat O'Brien" {
|
||||
t.Errorf("%+v user=%s", c, c.User())
|
||||
}
|
||||
if got := c.MCPHubURL(); got != "https://mcphub.example.com/sse?as_user=pat" {
|
||||
t.Errorf("hub = %s", got)
|
||||
}
|
||||
|
||||
if c, err := Load(filepath.Join(dir, "missing")); c != nil || err != nil {
|
||||
t.Errorf("missing file: %v %v", c, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnquote(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
`plain`: "plain",
|
||||
`two\ words`: "two words",
|
||||
`'single quoted'`: "single quoted",
|
||||
`$'ansi'`: "ansi",
|
||||
`""`: "",
|
||||
} {
|
||||
if got := unquote(in); got != want {
|
||||
t.Errorf("unquote(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user