Debian Codegraph @

pkg/envars/envars.go

raw symbols SCIP
1 // Copyright (c) Bartłomiej Płotka @bwplotka
2 // Licensed under the Apache License 2.0.
3
4 package envars
5
6 import (
7 "strings"
8
9 "mvdan.cc/sh/v3/expand"
10 )
11
12 type EnvSlice []string
13
14 func (e EnvSlice) Lookup(k string) (string, bool) {
15 for _, ev := range e {
16 sp := strings.SplitN(ev, "=", 2)
17 if sp[0] == k {
18 return sp[1], true
19 }
20 }
21 return "", false
22 }
23
24 func (e *EnvSlice) Set(kvs ...string) {
25 *e = MergeEnvSlices(*e, kvs...)
26 }
27
28 // Get retrieves a variable by its name. To check if the variable is
29 // set, use Variable.IsSet.
30 func (e *EnvSlice) Get(name string) expand.Variable {
31 return expand.ListEnviron(*e...).Get(name)
32 }
33
34 // Each iterates over all the currently set variables, calling the
35 // supplied function on each variable. Iteration is stopped if the
36 // function returns false.
37 //
38 // The names used in the calls aren't required to be unique or sorted.
39 // If a variable name appears twice, the latest occurrence takes
40 // priority.
41 //
42 // Each is required to forward exported variables when executing
43 // programs.
44 func (e *EnvSlice) Each(f func(name string, vr expand.Variable) bool) {
45 expand.ListEnviron(*e...).Each(f)
46 }