| 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 by the host's naming generation (location.Writes): under legacy, |
| 4 | // the KLAUS_* spelling alone; under bridge, both spellings with one value, |
| 5 | // so a child of any build reads the value flow resolved. No other flow code |
| 6 | // spells their names. |
| 7 | package flowenv |
| 8 | |
| 9 | import ( |
| 10 | "os" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | |
| 14 | "the-valley/flow/internal/location" |
| 15 | ) |
| 16 | |
| 17 | // Var identifies a flow environment variable by its two accepted names. |
| 18 | type Var struct { |
| 19 | New string |
| 20 | Legacy string |
| 21 | } |
| 22 | |
| 23 | // Accepted flow environment variables. |
| 24 | var ( |
| 25 | SessionID = Var{New: "VALLEY_FLOW_SESSION_ID", Legacy: "KLAUS_SESSION_ID"} |
| 26 | RunID = Var{New: "VALLEY_FLOW_RUN_ID", Legacy: "KLAUS_RUN_ID"} |
| 27 | RunEvents = Var{New: "VALLEY_FLOW_RUN_EVENTS", Legacy: "KLAUS_RUN_EVENTS"} |
| 28 | Backend = Var{New: "VALLEY_FLOW_BACKEND", Legacy: "KLAUS_BACKEND"} |
| 29 | AgentBackend = Var{New: "VALLEY_FLOW_AGENT_BACKEND", Legacy: "KLAUS_AGENT_BACKEND"} |
| 30 | Scratch = Var{New: "VALLEY_FLOW_SCRATCH", Legacy: "KLAUS_SCRATCH"} |
| 31 | TmuxSocket = Var{New: "VALLEY_FLOW_TMUX_SOCKET", Legacy: "KLAUS_TMUX_SOCKET"} |
| 32 | ) |
| 33 | |
| 34 | // All lists every flow environment variable that accepts both spellings. |
| 35 | var All = []Var{ |
| 36 | SessionID, |
| 37 | RunID, |
| 38 | RunEvents, |
| 39 | Backend, |
| 40 | AgentBackend, |
| 41 | Scratch, |
| 42 | TmuxSocket, |
| 43 | } |
| 44 | |
| 45 | // Lookup returns the value of newName when set (even to empty), otherwise |
| 46 | // legacyName, and reports whether either was set. |
| 47 | func Lookup(newName, legacyName string) (string, bool) { |
| 48 | if v, ok := os.LookupEnv(newName); ok { |
| 49 | return v, true |
| 50 | } |
| 51 | return os.LookupEnv(legacyName) |
| 52 | } |
| 53 | |
| 54 | // Get returns the value of newName when set (even to empty), otherwise |
| 55 | // legacyName (or "" when neither is set). |
| 56 | func Get(newName, legacyName string) string { |
| 57 | v, _ := Lookup(newName, legacyName) |
| 58 | return v |
| 59 | } |
| 60 | |
| 61 | // Lookup returns v's value and whether either spelling is set. |
| 62 | func (v Var) Lookup() (string, bool) { return Lookup(v.New, v.Legacy) } |
| 63 | |
| 64 | // Get returns v's value: New when set (even to empty), otherwise Legacy. |
| 65 | func (v Var) Get() string { return Get(v.New, v.Legacy) } |
| 66 | |
| 67 | // bridge reports whether the host's generation gives children both |
| 68 | // spellings. |
| 69 | func bridge() bool { return location.Writes() != location.Legacy } |
| 70 | |
| 71 | // Set returns env, a child process's environment, with v set to value. |
| 72 | // Under the legacy generation that is its legacy name alone, which every |
| 73 | // reader accepts, without its new name, which readers prefer and so would |
| 74 | // override value; under bridge it is both names. |
| 75 | func (v Var) Set(env []string, value string) []string { return v.set(bridge(), env, value) } |
| 76 | |
| 77 | func (v Var) set(both bool, env []string, value string) []string { |
| 78 | out := v.without(env) |
| 79 | if both { |
| 80 | out = append(out, v.New+"="+value) |
| 81 | } |
| 82 | return append(out, v.Legacy+"="+value) |
| 83 | } |
| 84 | |
| 85 | // without is env with neither of v's names. |
| 86 | func (v Var) without(env []string) []string { |
| 87 | out := make([]string, 0, len(env)+2) |
| 88 | for _, kv := range env { |
| 89 | if name, _, _ := strings.Cut(kv, "="); name != v.New && name != v.Legacy { |
| 90 | out = append(out, kv) |
| 91 | } |
| 92 | } |
| 93 | return out |
| 94 | } |
| 95 | |
| 96 | // Export returns POSIX shell statements that set v to value, as Set does, for |
| 97 | // the commands after them. |
| 98 | func (v Var) Export(value string) string { return v.export(bridge(), value) } |
| 99 | |
| 100 | func (v Var) export(both bool, value string) string { |
| 101 | if both { |
| 102 | return "export " + v.New + "=" + shellQuote(value) + " " + v.Legacy + "=" + shellQuote(value) + "; " |
| 103 | } |
| 104 | return "unset " + v.New + "; export " + v.Legacy + "=" + shellQuote(value) + "; " |
| 105 | } |
| 106 | |
| 107 | // EnvPrefix returns an env(1) prefix that runs the command after it with v |
| 108 | // set to value, as Set does. |
| 109 | func (v Var) EnvPrefix(value string) string { return v.envPrefix(bridge(), value) } |
| 110 | |
| 111 | func (v Var) envPrefix(both bool, value string) string { |
| 112 | if both { |
| 113 | return "env " + v.New + "=" + shellQuote(value) + " " + v.Legacy + "=" + shellQuote(value) + " " |
| 114 | } |
| 115 | return "env -u " + v.New + " " + v.Legacy + "=" + shellQuote(value) + " " |
| 116 | } |
| 117 | |
| 118 | // UnsetPrefix returns an env(1) prefix that runs the command after it with |
| 119 | // neither spelling of v. |
| 120 | func (v Var) UnsetPrefix() string { |
| 121 | return "env -u " + v.New + " -u " + v.Legacy + " " |
| 122 | } |
| 123 | |
| 124 | // Name is the spelling instructions a child reads name v by: the legacy |
| 125 | // one, or the new one under bridge, where children get both. |
| 126 | func (v Var) Name() string { |
| 127 | if bridge() { |
| 128 | return v.New |
| 129 | } |
| 130 | return v.Legacy |
| 131 | } |
| 132 | |
| 133 | // Normalize returns env, a child process's environment, with each variable |
| 134 | // of All as the child would resolve it: under bridge, a variable set under |
| 135 | // either name (even to empty) is set under both to the value Lookup would |
| 136 | // give, and one under neither stays under neither; under legacy, env as it |
| 137 | // is. As for exec.Cmd, a name's last entry in env is its value. |
| 138 | func Normalize(env []string) []string { return normalize(bridge(), env) } |
| 139 | |
| 140 | func normalize(both bool, env []string) []string { |
| 141 | if !both { |
| 142 | return env |
| 143 | } |
| 144 | for _, v := range All { |
| 145 | value, ok := lookupIn(env, v.New) |
| 146 | if !ok { |
| 147 | value, ok = lookupIn(env, v.Legacy) |
| 148 | } |
| 149 | if ok { |
| 150 | env = v.set(true, env, value) |
| 151 | } else { |
| 152 | env = v.without(env) |
| 153 | } |
| 154 | } |
| 155 | return env |
| 156 | } |
| 157 | |
| 158 | func lookupIn(env []string, name string) (string, bool) { |
| 159 | for i := len(env) - 1; i >= 0; i-- { |
| 160 | if k, v, ok := strings.Cut(env[i], "="); ok && k == name { |
| 161 | return v, true |
| 162 | } |
| 163 | } |
| 164 | return "", false |
| 165 | } |
| 166 | |
| 167 | // NormalizePrefix returns POSIX shell statements that give the commands |
| 168 | // after them each variable of All as this process resolves it (Lookup), |
| 169 | // under both names, or under neither when this process has it under |
| 170 | // neither: under bridge, for a pane, whose shell starts from the tmux |
| 171 | // server's environment rather than flow's. Under legacy it is empty. |
| 172 | func NormalizePrefix() string { return normalizePrefix(bridge()) } |
| 173 | |
| 174 | func normalizePrefix(both bool) string { |
| 175 | if !both { |
| 176 | return "" |
| 177 | } |
| 178 | var b strings.Builder |
| 179 | for _, v := range All { |
| 180 | if value, ok := v.Lookup(); ok { |
| 181 | b.WriteString(v.export(true, value)) |
| 182 | } else { |
| 183 | b.WriteString(v.unset()) |
| 184 | } |
| 185 | } |
| 186 | return b.String() |
| 187 | } |
| 188 | |
| 189 | // ClearPrefix returns POSIX shell statements that unset both names of each |
| 190 | // variable of All except keep, which the commands after them set |
| 191 | // themselves: under bridge, for a command on another host, whose shell |
| 192 | // starts from that host's environment and not flow's. Under legacy it is |
| 193 | // empty. |
| 194 | func ClearPrefix(keep ...Var) string { return clearPrefix(bridge(), keep) } |
| 195 | |
| 196 | func clearPrefix(both bool, keep []Var) string { |
| 197 | if !both { |
| 198 | return "" |
| 199 | } |
| 200 | var b strings.Builder |
| 201 | for _, v := range All { |
| 202 | if !slices.Contains(keep, v) { |
| 203 | b.WriteString(v.unset()) |
| 204 | } |
| 205 | } |
| 206 | return b.String() |
| 207 | } |
| 208 | |
| 209 | // unset is the POSIX shell statement that unsets both of v's names. |
| 210 | func (v Var) unset() string { return "unset " + v.New + " " + v.Legacy + "; " } |
| 211 | |
| 212 | // NormalizeProcess sets each variable of All in this process's environment |
| 213 | // under both names to the value Lookup gives, under bridge, so every child |
| 214 | // it starts inherits one value under both. Under legacy it changes nothing. |
| 215 | func NormalizeProcess() { |
| 216 | if !bridge() { |
| 217 | return |
| 218 | } |
| 219 | for _, v := range All { |
| 220 | if value, ok := v.Lookup(); ok { |
| 221 | _ = os.Setenv(v.New, value) |
| 222 | _ = os.Setenv(v.Legacy, value) |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func shellQuote(s string) string { |
| 228 | return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" |
| 229 | } |