121 lines
4.1 KiB
Go
121 lines
4.1 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// terminalTmuxSocket is the tmux server Pipelines owns (`tmux -L pipelines`):
|
|
// its sessions never mix with the operator's own tmux server.
|
|
const terminalTmuxSocket = "pipelines"
|
|
|
|
// terminalTmux runs one Pipelines-owned tmux server. Every terminal shell is
|
|
// the only pane of its own session there, so it outlives the Pipelines
|
|
// server; the server talks to it through an ordinary attached client in a
|
|
// pseudo-terminal.
|
|
type terminalTmux struct {
|
|
program string
|
|
socket string
|
|
}
|
|
|
|
// findTerminalTmux is the tmux on PATH (or the usual package-manager
|
|
// locations a service's PATH may lack); nil when none is installed.
|
|
func findTerminalTmux(socket string) *terminalTmux {
|
|
if socket == "" {
|
|
socket = terminalTmuxSocket
|
|
}
|
|
program, err := exec.LookPath("tmux")
|
|
if err != nil {
|
|
for _, dir := range []string{"/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"} {
|
|
candidate := filepath.Join(dir, "tmux")
|
|
if info, statErr := os.Stat(candidate); statErr == nil && !info.IsDir() && info.Mode()&0o111 != 0 {
|
|
program, err = candidate, nil
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return &terminalTmux{program: program, socket: socket}
|
|
}
|
|
|
|
// terminalTmuxName is the tmux session of terminal id.
|
|
func terminalTmuxName(id string) string { return "pipelines-" + id }
|
|
|
|
// command is one tmux client invocation against the Pipelines server, with
|
|
// no configuration file and outside any tmux the server itself runs in.
|
|
func (t *terminalTmux) command(args ...string) *exec.Cmd {
|
|
cmd := exec.Command(t.program, append([]string{"-L", t.socket, "-f", os.DevNull}, args...)...)
|
|
cmd.Env = terminalEnv(os.Environ(), "")
|
|
return cmd
|
|
}
|
|
|
|
// create starts session name running shell -l in cwd at cols x rows,
|
|
// detached. The first call also starts the tmux server; the options make
|
|
// it invisible (no status line), pass the shell's window title through,
|
|
// keep a bounded scrollback reachable by the mouse wheel, and leave Esc
|
|
// prompt.
|
|
func (t *terminalTmux) create(name, cwd, shell string, cols, rows int) error {
|
|
cmd := t.command(
|
|
"start-server", ";",
|
|
"set-option", "-g", "history-limit", strconv.Itoa(protocol.TerminalTmuxHistory), ";",
|
|
"set-option", "-g", "status", "off", ";",
|
|
"set-option", "-g", "mouse", "on", ";",
|
|
"set-option", "-g", "escape-time", "10", ";",
|
|
"set-option", "-g", "set-titles", "on", ";",
|
|
"set-option", "-g", "set-titles-string", "#{pane_title}", ";",
|
|
"new-session", "-d", "-s", name, "-c", cwd, "-x", strconv.Itoa(cols), "-y", strconv.Itoa(rows), "--", shell, "-l", ";",
|
|
"select-pane", "-t", "="+name+":", "-T", "",
|
|
)
|
|
cmd.Dir = cwd
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
return fmt.Errorf("tmux new-session: %v: %s", err, strings.TrimSpace(string(out)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// alive reports whether session name still exists.
|
|
func (t *terminalTmux) alive(name string) bool {
|
|
return t.command("has-session", "-t", "="+name).Run() == nil
|
|
}
|
|
|
|
// attach is the client command showing session name in the server's
|
|
// pseudo-terminal.
|
|
func (t *terminalTmux) attach(name string) *exec.Cmd {
|
|
return t.command("attach-session", "-t", "="+name)
|
|
}
|
|
|
|
// kill ends session name and its shell.
|
|
func (t *terminalTmux) kill(name string) error {
|
|
if out, err := t.command("kill-session", "-t", "="+name).CombinedOutput(); err != nil && t.alive(name) {
|
|
return fmt.Errorf("tmux kill-session: %v: %s", err, strings.TrimSpace(string(out)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// terminalEnv is env for a terminal's process: TERM names the emulator the
|
|
// clients draw, PWD the start directory (when set), and no TMUX variables
|
|
// leak in from a tmux the server runs inside.
|
|
func terminalEnv(env []string, cwd string) []string {
|
|
out := make([]string, 0, len(env)+2)
|
|
for _, entry := range env {
|
|
if strings.HasPrefix(entry, "TERM=") || strings.HasPrefix(entry, "TMUX=") || strings.HasPrefix(entry, "TMUX_PANE=") ||
|
|
(cwd != "" && strings.HasPrefix(entry, "PWD=")) {
|
|
continue
|
|
}
|
|
out = append(out, entry)
|
|
}
|
|
out = append(out, "TERM=xterm-256color")
|
|
if cwd != "" {
|
|
out = append(out, "PWD="+cwd)
|
|
}
|
|
return out
|
|
}
|