Files
pad/internal/cli/server.go
T
xarmian 7cda0d7896 feat: rebrand to Perpetual Software + new tagline (IDEA-832) (#273)
Migrates from xarmian/pad to PerpetualSoftware/pad across the entire
repo and updates the product subtitle to "Collaborate with your AI
agents".

Go module rename
- go.mod: github.com/xarmian/pad → github.com/PerpetualSoftware/pad
- All Go imports updated across cmd/pad, internal/{cli,server,store,
  models,collections,items,events,metrics,webhooks} (~130 files)
- Test fixtures with the literal repo slug ("xarmian/pad" in JSON
  shapes, SSH/HTTPS git URL strings, workspace_context fixtures)
  also updated, including the secondary repo entry
  (xarmian/pad-web → PerpetualSoftware/pad-web — pad-web was also
  moved to the org per branch context)

Docs / config
- README badges, install instructions, brew tap, Docker image, source
  build path, sponsor link (sponsor link kept as personal @xarmian)
- Subtitle: "Project management for developers and AI agents." →
  "Collaborate with your AI agents." (README, manifests, web layout
  meta, .goreleaser homebrew description)
- CONTRIBUTING.md, SECURITY.md, skills/INSTALL.md
- .goreleaser.yaml: homebrew_casks owner, GHCR image, release github
  owner, cosign cert-identity regex, comments
- .github/workflows/release.yml: tap/release comments
- deploy/k8s/deployment.yaml: container image
- docs/deployment.md: clone URL
- web/static/{site.webmanifest,manifest.json}: description
- web/src/routes/+layout.svelte: meta description + og:description

Brew tap path is PerpetualSoftware/tap/pad (CamelCase, matches
GitHub user case). GHCR image is ghcr.io/perpetualsoftware/pad
(lowercased per GHCR's URL normalization). CODEOWNERS @xarmian and
FUNDING.yml github: xarmian intentionally retained — those are the
personal maintainer / sponsor account, separate from the org repo.

Verification: go build ./..., go test ./... (all pkgs pass), web
build, and make install all clean (TASK-844, TASK-845).
2026-04-28 12:26:39 -04:00

122 lines
2.9 KiB
Go

package cli
import (
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/PerpetualSoftware/pad/internal/config"
)
// EnsureServer checks if the pad server is running; if not, starts it in the background.
func EnsureServer(cfg *config.Config) error {
// Only an explicitly configured local client should auto-manage a local
// background process. Unconfigured or external clients should connect only
// to their configured target.
if !cfg.ManagesLocalServer() {
return nil
}
if isServerHealthy(cfg.Host, cfg.Port) {
return nil
}
// Start server as background process
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("find executable: %w", err)
}
cmd := exec.Command(exePath, "server", "start")
setSysProcAttr(cmd)
// Redirect stdout/stderr to log file
logFile, err := os.OpenFile(cfg.LogFile(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return fmt.Errorf("open log file: %w", err)
}
cmd.Stdout = logFile
cmd.Stderr = logFile
if err := cmd.Start(); err != nil {
logFile.Close()
return fmt.Errorf("start server: %w", err)
}
// Write PID file
if err := os.WriteFile(cfg.PIDFile(), []byte(strconv.Itoa(cmd.Process.Pid)), 0644); err != nil {
logFile.Close()
return fmt.Errorf("write PID file: %w", err)
}
// Release the process so it doesn't become a zombie
cmd.Process.Release()
logFile.Close()
// Wait for server to become healthy
for i := 0; i < 30; i++ {
time.Sleep(100 * time.Millisecond)
if isServerHealthy(cfg.Host, cfg.Port) {
return nil
}
}
return fmt.Errorf("server failed to start within 3 seconds. Check %s for errors", cfg.LogFile())
}
// StopServer sends a stop signal to the background server process.
func StopServer(cfg *config.Config) error {
pidData, err := os.ReadFile(cfg.PIDFile())
if err != nil {
return fmt.Errorf("server not running (no PID file)")
}
pid, err := strconv.Atoi(strings.TrimSpace(string(pidData)))
if err != nil {
os.Remove(cfg.PIDFile())
return fmt.Errorf("invalid PID file")
}
process, err := os.FindProcess(pid)
if err != nil {
os.Remove(cfg.PIDFile())
return fmt.Errorf("process not found")
}
if err := stopProcess(process); err != nil {
os.Remove(cfg.PIDFile())
return fmt.Errorf("failed to stop server: %w", err)
}
os.Remove(cfg.PIDFile())
// Wait for it to actually stop
for i := 0; i < 20; i++ {
time.Sleep(100 * time.Millisecond)
if !isServerHealthy(cfg.Host, cfg.Port) {
return nil
}
}
return nil
}
// IsServerRunning checks if the server is currently running.
func IsServerRunning(cfg *config.Config) bool {
return isServerHealthy(cfg.Host, cfg.Port)
}
func isServerHealthy(host string, port int) bool {
client := &http.Client{Timeout: 1 * time.Second}
resp, err := client.Get(fmt.Sprintf("http://%s:%d/api/v1/health", host, port))
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}