| 1 | // Package flowenv reads flow environment variables under either their |
| 2 | // VALLEY_FLOW_* or KLAUS_* spellings, and sets them for the processes flow |
| 3 | // starts under the KLAUS_* spelling alone. No other flow code spells their |
| 4 | // names. |
| 5 | package flowenv |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // Var identifies a flow environment variable by its two accepted names. |
| 13 | type Var struct { |
| 14 | New string |
| 15 | Legacy string |
| 16 | } |
| 17 | |
| 18 | // Accepted flow environment variables. |
| 19 | var ( |
| 20 | SessionID = Var{New: "VALLEY_FLOW_SESSION_ID", Legacy: "KLAUS_SESSION_ID"} |
| 21 | RunID = Var{New: "VALLEY_FLOW_RUN_ID", Legacy: "KLAUS_RUN_ID"} |
| 22 | RunEvents = Var{New: "VALLEY_FLOW_RUN_EVENTS", Legacy: "KLAUS_RUN_EVENTS"} |
| 23 | Backend = Var{New: "VALLEY_FLOW_BACKEND", Legacy: "KLAUS_BACKEND"} |
| 24 | AgentBackend = Var{New: "VALLEY_FLOW_AGENT_BACKEND", Legacy: "KLAUS_AGENT_BACKEND"} |
| 25 | Scratch = Var{New: "VALLEY_FLOW_SCRATCH", Legacy: "KLAUS_SCRATCH"} |
| 26 | TmuxSocket = Var{New: "VALLEY_FLOW_TMUX_SOCKET", Legacy: "KLAUS_TMUX_SOCKET"} |
| 27 | ) |
| 28 | |
| 29 | // All lists every flow environment variable that accepts both spellings. |
| 30 | var All = []Var{ |
| 31 | SessionID, |
| 32 | RunID, |
| 33 | RunEvents, |
| 34 | Backend, |
| 35 | AgentBackend, |
| 36 | Scratch, |
| 37 | TmuxSocket, |
| 38 | } |
| 39 | |
| 40 | // Lookup returns the value of newName when set (even to empty), otherwise |
| 41 | // legacyName, and reports whether either was set. |
| 42 | func Lookup(newName, legacyName string) (string, bool) { |
| 43 | if v, ok := os.LookupEnv(newName); ok { |
| 44 | return v, true |
| 45 | } |
| 46 | return os.LookupEnv(legacyName) |
| 47 | } |
| 48 | |
| 49 | // Get returns the value of newName when set (even to empty), otherwise |
| 50 | // legacyName (or "" when neither is set). |
| 51 | func Get(newName, legacyName string) string { |
| 52 | v, _ := Lookup(newName, legacyName) |
| 53 | return v |
| 54 | } |
| 55 | |
| 56 | // Lookup returns v's value and whether either spelling is set. |
| 57 | func (v Var) Lookup() (string, bool) { return Lookup(v.New, v.Legacy) } |
| 58 | |
| 59 | // Get returns v's value: New when set (even to empty), otherwise Legacy. |
| 60 | func (v Var) Get() string { return Get(v.New, v.Legacy) } |
| 61 | |
| 62 | // Set returns env, a child process's environment, with v set to value under |
| 63 | // its legacy name, which every reader accepts, and without its new name, |
| 64 | // which readers prefer and so would override value. |
| 65 | func (v Var) Set(env []string, value string) []string { |
| 66 | out := make([]string, 0, len(env)+1) |
| 67 | for _, kv := range env { |
| 68 | if name, _, _ := strings.Cut(kv, "="); name != v.New && name != v.Legacy { |
| 69 | out = append(out, kv) |
| 70 | } |
| 71 | } |
| 72 | return append(out, v.Legacy+"="+value) |
| 73 | } |
| 74 | |
| 75 | // Export returns POSIX shell statements that set v to value, as Set does, for |
| 76 | // the commands after them. |
| 77 | func (v Var) Export(value string) string { |
| 78 | return "unset " + v.New + "; export " + v.Legacy + "=" + shellQuote(value) + "; " |
| 79 | } |
| 80 | |
| 81 | // EnvPrefix returns an env(1) prefix that runs the command after it with v |
| 82 | // set to value, as Set does. |
| 83 | func (v Var) EnvPrefix(value string) string { |
| 84 | return "env -u " + v.New + " " + v.Legacy + "=" + shellQuote(value) + " " |
| 85 | } |
| 86 | |
| 87 | // UnsetPrefix returns an env(1) prefix that runs the command after it with |
| 88 | // neither spelling of v. |
| 89 | func (v Var) UnsetPrefix() string { |
| 90 | return "env -u " + v.New + " -u " + v.Legacy + " " |
| 91 | } |
| 92 | |
| 93 | func shellQuote(s string) string { |
| 94 | return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" |
| 95 | } |