Files
pipelines/internal/tui/create.go
T

131 lines
3.8 KiB
Go

package tui
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"git.a-wan.net/alan/pipelines/internal/author"
"git.a-wan.net/alan/pipelines/internal/client"
"git.a-wan.net/alan/pipelines/internal/draft"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type editedMsg struct {
name, path, source string
err error
confirm bool
unchanged bool
createOnly bool
baseVersion int
}
func (m Model) send(request protocol.Message) tea.Cmd {
return func() tea.Msg {
ctx, cancel := context.WithTimeout(m.ctx, 10*time.Second)
defer cancel()
if err := client.Send(ctx, m.conn, request); err != nil {
return errorMsg{err: err, conn: m.conn}
}
return sentMsg{request.ID}
}
}
func editDraft(name, source string, confirm ...bool) tea.Cmd {
needsConfirm := len(confirm) > 0 && confirm[0]
return editDraftMode(name, source, needsConfirm, false)
}
func editDraftMode(name, source string, needsConfirm, fresh bool, metadata ...draft.Draft) tea.Cmd {
var meta draft.Draft
if len(metadata) > 0 {
meta = metadata[0]
}
d := meta
var err error
if d.Path == "" {
d, err = draft.Create(name, source, meta.Server, fresh, meta.BaseVersion)
}
path := d.Path
if err != nil {
return func() tea.Msg { return editedMsg{name: name, path: path, err: err} }
}
var command *exec.Cmd
if name != "" && (strings.TrimSpace(os.Getenv("VISUAL")) != "" || strings.TrimSpace(os.Getenv("EDITOR")) != "") {
editor, args, editorErr := draftEditor()
if editorErr != nil {
return func() tea.Msg { return editedMsg{name: name, path: path, err: editorErr} }
}
command = exec.Command(editor, append(args, path)...)
} else {
executable, exeErr := os.Executable()
if exeErr != nil {
return func() tea.Msg { return editedMsg{name: name, path: path, err: exeErr} }
}
cli := quoteCLI(executable) + " --url " + quoteCLI(meta.Server)
command, err = author.Command(context.Background(), opencode.Executable(), d, "", cli)
if err != nil {
return func() tea.Msg { return editedMsg{name: name, path: path, err: err} }
}
}
return tea.ExecProcess(command, func(err error) tea.Msg {
result := readEditedDraft(name, path, source, needsConfirm, err)
if name == "" {
if updated, loadErr := draft.Load(path); loadErr == nil {
result.name = updated.Name
}
}
result.baseVersion = meta.BaseVersion
if fresh {
result.unchanged = false
result.createOnly = true
}
return result
})
}
func quoteCLI(value string) string { return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'" }
func draftEditor() (string, []string, error) {
for _, name := range []string{"VISUAL", "EDITOR"} {
if value := strings.TrimSpace(os.Getenv(name)); value != "" {
shell, err := exec.LookPath("sh")
return shell, []string{"-c", value + ` "$1"`, "pipelines-editor"}, err
}
}
for _, name := range []string{"nvim", "vim", "vi", "nano"} {
if path, err := exec.LookPath(name); err == nil {
return path, nil, nil
}
}
return "", nil, fmt.Errorf("no editor found; set VISUAL or EDITOR, or install vim/nano")
}
func readEditedDraft(name, path, original string, confirm bool, editorErr error) editedMsg {
result := editedMsg{name: name, path: path, err: editorErr, confirm: confirm}
if editorErr != nil {
return result
}
info, err := os.Stat(path)
if err != nil {
result.err = err
return result
}
if info.Size() > 1<<20 {
result.err = fmt.Errorf("file exceeds 1 MiB")
return result
}
data, err := os.ReadFile(path)
result.source, result.err = string(data), err
// A new pipeline still needs its initial submission even if its template
// was left untouched. Existing edits require actual byte-for-byte changes.
result.unchanged = err == nil && confirm && result.source == original
return result
}