Initial pipelines server, CLI, TUI, and Go installation
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package installation
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompiledCommandFindsManifestThroughSystemLink(t *testing.T) {
|
||||
t.Setenv("PIPELINES_PREFIX", "")
|
||||
root, _ := filepath.EvalSymlinks(t.TempDir())
|
||||
os.Mkdir(filepath.Join(root, "bin"), 0700)
|
||||
binary := filepath.Join(root, "bin/pipelines")
|
||||
os.WriteFile(binary, []byte("binary"), 0700)
|
||||
config := Config{Format: 1, Prefix: root, ServerPort: 18080, OpenCode: "/host/opencode", Path: "/usr/bin"}
|
||||
data, _ := json.Marshal(config)
|
||||
os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600)
|
||||
link := filepath.Join(t.TempDir(), "pipelines")
|
||||
os.Symlink(binary, link)
|
||||
loaded, err := Load(link)
|
||||
if err != nil || loaded == nil || loaded.Prefix != root {
|
||||
t.Fatal(loaded, err)
|
||||
}
|
||||
t.Setenv("PIPELINES_URL", "")
|
||||
t.Setenv("PIPELINES_OPENCODE", "")
|
||||
t.Setenv("PATH", os.Getenv("PATH"))
|
||||
if err = loaded.Environment(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if os.Getenv("PIPELINES_URL") != "ws://127.0.0.1:18080/connect" {
|
||||
t.Fatal("wrong installation selected")
|
||||
}
|
||||
t.Setenv("PIPELINES_URL", "ws://explicit/connect")
|
||||
loaded.Environment()
|
||||
if os.Getenv("PIPELINES_URL") != "ws://explicit/connect" {
|
||||
t.Fatal("explicit server overridden")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStandaloneGoInstalledCommandHasNoServiceDependency(t *testing.T) {
|
||||
t.Setenv("PIPELINES_PREFIX", "")
|
||||
binary := filepath.Join(t.TempDir(), "pipelines")
|
||||
os.WriteFile(binary, []byte("binary"), 0700)
|
||||
config, err := Load(binary)
|
||||
if err != nil || config != nil {
|
||||
t.Fatal(config, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user