Files
pipelines/internal/installation/installation.go
T

108 lines
3.2 KiB
Go

// Package installation binds the compiled CLI to its per-user managed services.
package installation
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
)
type Config struct {
Format int `json:"format"`
Prefix string `json:"prefix"`
Python string `json:"python"`
ServerPort int `json:"server_port"`
OpenCode string `json:"opencode"`
OpenCodeEnv map[string]string `json:"opencode_env"`
Path string `json:"path"`
Ntfy struct {
Mode string `json:"mode"`
} `json:"ntfy"`
}
// Load selects an explicit prefix, or a native installation containing this exact
// executable. It never guesses a default prefix for an unrelated Go-installed CLI.
func Load(executable string) (*Config, error) {
prefix := os.Getenv("PIPELINES_PREFIX")
inferred := prefix == ""
if prefix == "" {
resolved, err := filepath.EvalSymlinks(executable)
if err != nil || filepath.Base(resolved) != "pipelines" || filepath.Base(filepath.Dir(resolved)) != "bin" {
return nil, nil
}
prefix = filepath.Dir(filepath.Dir(resolved))
}
prefix, err := filepath.Abs(prefix)
if err != nil {
return nil, err
}
// The native installer and scripts/manage.py record the symlink-resolved
// prefix, so resolve the same way here instead of rejecting a symlinked
// HOME or prefix as "invalid".
if resolved, err := filepath.EvalSymlinks(prefix); err == nil {
prefix = resolved
}
manifest := filepath.Join(prefix, "pipelines-install.json")
info, err := os.Lstat(manifest)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
if !info.Mode().IsRegular() || !ownedByUser(info) {
return nil, fmt.Errorf("refusing installation manifest not a regular file owned by this user")
}
data, err := os.ReadFile(manifest)
if err != nil {
return nil, err
}
var config Config
if err = json.Unmarshal(data, &config); err != nil {
return nil, err
}
if config.Format != 1 || config.Prefix != prefix || config.ServerPort < 1024 || config.ServerPort > 65535 {
return nil, fmt.Errorf("invalid managed installation manifest")
}
if inferred && config.Ntfy.Mode != "native" {
return nil, nil
}
if inferred {
binary, err := os.Stat(filepath.Join(prefix, "bin", "pipelines"))
if err != nil || !binary.Mode().IsRegular() || binary.Mode().Perm()&0111 == 0 || !ownedByUser(binary) {
return nil, fmt.Errorf("native installation executable must be owned by this user")
}
}
return &config, nil
}
func (c Config) Environment() error {
for key, value := range c.OpenCodeEnv {
switch key {
case "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "OPENCODE_SERVER", "OPENCODE_DB":
if err := os.Setenv(key, value); err != nil {
return err
}
}
}
for key, value := range map[string]string{"PIPELINES_URL": "ws://127.0.0.1:" + strconv.Itoa(c.ServerPort) + "/connect", "PIPELINES_OPENCODE": c.OpenCode} {
if os.Getenv(key) != "" {
continue
}
if err := os.Setenv(key, value); err != nil {
return err
}
}
return nil
}
func Service(action string) bool {
switch action {
case "start", "stop", "restart", "doctor", "logs", "uninstall", "update":
return true
}
return false
}