Initial pipelines server, CLI, TUI, and Go installation
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"pipelines/internal/cli"
|
||||
"pipelines/internal/installation"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
app := cli.App{Out: os.Stdout, Err: os.Stderr, In: os.Stdin}
|
||||
args := os.Args[1:]
|
||||
executable, err := os.Executable()
|
||||
var config *installation.Config
|
||||
if err == nil {
|
||||
config, err = installation.Load(executable)
|
||||
}
|
||||
if err == nil && config != nil {
|
||||
err = config.Environment()
|
||||
if err == nil && len(args) > 0 && installation.Service(args[0]) {
|
||||
err = syscall.Exec(config.Python, append([]string{config.Python, filepath.Join(config.Prefix, "lib/manage.py"), "--prefix", config.Prefix}, args...), os.Environ())
|
||||
}
|
||||
if len(args) == 0 {
|
||||
args = []string{"tui"}
|
||||
}
|
||||
}
|
||||
if err == nil && len(args) > 0 && installation.Service(args[0]) {
|
||||
err = fmt.Errorf("%s requires a managed installation; run install.sh first", args[0])
|
||||
}
|
||||
if err == nil {
|
||||
err = app.Run(ctx, args)
|
||||
}
|
||||
if err != nil {
|
||||
jsonMode := false
|
||||
for _, arg := range os.Args[1:] {
|
||||
if arg == "--json" || arg == "-json" || arg == "--json=true" {
|
||||
jsonMode = true
|
||||
}
|
||||
}
|
||||
if jsonMode {
|
||||
_ = json.NewEncoder(os.Stderr).Encode(map[string]string{"type": "error", "error": err.Error()})
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, "pipelines:", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"pipelines/internal/pipeline"
|
||||
"pipelines/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", "127.0.0.1:8080", "HTTP/WebSocket listen address")
|
||||
db := flag.String("db", "pipelines.db", "SQLite database path")
|
||||
var roots []string
|
||||
flag.Func("workspace-root", "Allow repository runs below this directory (repeatable; default is server working directory)", func(value string) error {
|
||||
absolute, err := filepath.Abs(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(absolute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := os.Stat(resolved)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return errors.New("workspace root must be a directory")
|
||||
}
|
||||
roots = append(roots, resolved)
|
||||
return nil
|
||||
})
|
||||
flag.Parse()
|
||||
store, err := pipeline.Open(*db)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
srv := &http.Server{Addr: *addr, Handler: server.HandlerForWorkspaces(ctx, store, roots), ReadHeaderTimeout: 5 * time.Second}
|
||||
defer server.WaitRuns(srv.Handler)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Printf("shutdown: %v", err)
|
||||
}
|
||||
}()
|
||||
log.Printf("listening on %s (WebSocket endpoint: /connect)", *addr)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"pipelines/internal/tui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
url := flag.String("url", "ws://127.0.0.1:8080/connect", "server WebSocket URL")
|
||||
run := flag.Bool("run", false, "Select a registered pipeline and run it in this repository")
|
||||
name := flag.String("pipeline", "", "Registered pipeline name; omitted opens fuzzy selection")
|
||||
prompt := flag.String("prompt", "", "Task prompt; omitted opens a multiline composer")
|
||||
workspace := flag.String("workspace", "", "Repository directory (defaults to current directory with -run)")
|
||||
yes := flag.Bool("yes", false, "Start a supplied name/prompt without typing RUN (no implicit tool approval)")
|
||||
allow := flag.Bool("allow-tools", false, "Approve declared shell/edit tools with -yes")
|
||||
paths := flag.String("write-paths", "", "Comma-separated exact existing project-local Go files")
|
||||
flag.Parse()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
model := tui.New(ctx, *url)
|
||||
if *run {
|
||||
if *workspace == "" {
|
||||
var err error
|
||||
*workspace, err = os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
options := tui.RunOptions{Pipeline: *name, Prompt: *prompt, Workspace: *workspace, Yes: *yes, AllowTools: *allow}
|
||||
for _, path := range strings.Split(*paths, ",") {
|
||||
if path = strings.TrimSpace(path); path != "" {
|
||||
options.WritePaths = append(options.WritePaths, path)
|
||||
}
|
||||
}
|
||||
if *yes && (*name == "" || strings.TrimSpace(*prompt) == "") {
|
||||
fmt.Fprintln(os.Stderr, "-yes requires -pipeline and a nonempty -prompt")
|
||||
os.Exit(2)
|
||||
}
|
||||
if *yes && len(options.WritePaths) > 0 && !*allow {
|
||||
fmt.Fprintln(os.Stderr, "-write-paths with -yes requires -allow-tools")
|
||||
os.Exit(2)
|
||||
}
|
||||
model = tui.NewRun(ctx, *url, options)
|
||||
}
|
||||
if _, err := tea.NewProgram(model, tea.WithAltScreen()).Run(); err != nil {
|
||||
cancel()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user