81 lines
2.2 KiB
Go
81 lines
2.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"`
|
|
}
|
|
|
|
// Load finds the manifest next to the actual Go binary, including via a system PATH symlink.
|
|
// A standalone go-installed CLI can also select a prefix with PIPELINES_PREFIX.
|
|
func Load(executable string) (*Config, error) {
|
|
resolved, err := filepath.EvalSymlinks(executable)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
prefix := os.Getenv("PIPELINES_PREFIX")
|
|
if prefix == "" {
|
|
prefix = filepath.Dir(filepath.Dir(resolved))
|
|
}
|
|
prefix, err = filepath.Abs(prefix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(prefix, "pipelines-install.json"))
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
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")
|
|
}
|
|
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, "PATH": c.Path} {
|
|
if key != "PATH" && 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", "status", "doctor", "logs", "uninstall":
|
|
return true
|
|
}
|
|
return false
|
|
}
|