gunk-dev/the-valley

flow.go

source28abc5737165main's published tip history
1.8 KiB
1package main
2
3// valley flow: the agent-run client (flow/), a program of its own that
4// valley runs rather than carries, since valley imports nothing outside
5// the standard library and this repository's other modules.
6
7import (
8 "fmt"
9 "io"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "syscall"
14)
15
16// flowHelper is the program valley flow runs.
17const flowHelper = "valley-flow"
18
19// runFlow replaces this process with valley-flow, found on PATH, given the
20// arguments after flow, so the stdio, terminal, signals and exit status
21// are valley-flow's own. PATH alone is searched, never this binary's
22// directory: the packaged valley is a wrapper, and the helper is installed
23// and updated apart from valley. The helper runs in the environment valley
24// was started in (callerEnvironment), not the one valley sets up for the
25// git it runs. It returns only when the helper could not start: 127 when
26// PATH has no executable valley-flow (flowHelperPath), 126 when exec
27// fails.
28func runFlow(args []string, stderr io.Writer) int {
29 helper, found := flowHelperPath()
30 if !found {
31 fmt.Fprintf(stderr, "valley: flow runs %s, which is not on PATH\n", flowHelper)
32 return 127
33 }
34 err := syscall.Exec(helper, append([]string{flowHelper}, args...), callerEnvironment)
35 fmt.Fprintf(stderr, "valley: %s: %s\n", helper, err)
36 return 126
37}
38
39// flowHelperPath is the first executable valley-flow in PATH's absolute
40// directories. A relative or empty entry, which names a directory by
41// where valley happens to run, is skipped: exec.LookPath would stop there
42// instead, refusing it or, under GODEBUG=execerrdot=0, running it.
43func flowHelperPath() (string, bool) {
44 for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
45 if !filepath.IsAbs(dir) {
46 continue
47 }
48 if p, err := exec.LookPath(filepath.Join(dir, flowHelper)); err == nil {
49 return p, true
50 }
51 }
52 return "", false
53}