ref: Replace x/devtools/internal/envutil with x/lib/envvar
A while ago I wrote envutil in the devtools repo, to make it
easier to deal with environment variables, and to make it easy
for the v23 tool to keep track of the mutations it had made.
Later on I noticed that there was similar code in x/ref in a few
places.
This CL removes envutil, and adds a new package envvar that may
be used by all other packages. It behaves similarly to envutil,
and also to the code scattered around x/ref.
In a subsequent CL I'll add cmdline support for environment
variables using envvar.
MultiPart: 2/2
Change-Id: Idd5c06ab79badf678cd5ce7e87493c04ea16204a
diff --git a/lib/exec/parent.go b/lib/exec/parent.go
index e8747a1..230e5d0 100644
--- a/lib/exec/parent.go
+++ b/lib/exec/parent.go
@@ -18,9 +18,8 @@
"time"
"v.io/v23/verror"
-
+ "v.io/x/lib/envvar"
"v.io/x/lib/vlog"
-
"v.io/x/ref/lib/timekeeper"
)
@@ -126,22 +125,16 @@
// Start starts the child process, sharing a secret with it and
// setting up a communication channel over which to read its status.
func (p *ParentHandle) Start() error {
- // Make sure that there are no instances of the ExecVersionVariable
- // already in the environment (which can happen when a subprocess
- // creates a subprocess etc)
- nenv := make([]string, 0, len(p.c.Env)+1)
- for _, e := range p.c.Env {
- if strings.HasPrefix(e, ExecVersionVariable+"=") {
- continue
- }
- nenv = append(nenv, e)
- }
-
+ env := envvar.SliceToMap(p.c.Env)
if !p.protocol {
+ // Ensure ExecVersionVariable is not set if we're not using the protocol.
+ delete(env, ExecVersionVariable)
+ p.c.Env = envvar.MapToSlice(env)
return p.c.Start()
}
-
- p.c.Env = append(nenv, ExecVersionVariable+"="+version1)
+ // Ensure ExecVersionVariable is always set if we are using the protocol.
+ env[ExecVersionVariable] = version1
+ p.c.Env = envvar.MapToSlice(env)
// Create anonymous pipe for communicating data between the child
// and the parent.