-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.go
42 lines (38 loc) · 931 Bytes
/
bin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package chainlib
import (
"flag"
"fmt"
"os"
"strings"
)
func assert(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// Convenience function for writing utilities which use Bernstein chaining to
// pass environment variables. Function f should perform logic to retrieve data
// to be serialized to environment variables and return them as a map of strings,
// or error if something went wrong.
func Main(prefix string, f func() (map[string]string, error)) {
var upcase bool
var rprefix string
flag.StringVar(&rprefix, "prefix", prefix, "Prefix")
flag.BoolVar(&upcase, "upcase", true, "Upcase variables")
flag.Parse()
vars, err := f()
assert(err)
env := NewEnvironment()
for k, v := range vars {
key := SafeEnv(k)
if upcase {
key = strings.ToUpper(key)
}
if rprefix != "" {
key = fmt.Sprintf("%s_%s", rprefix, key)
}
env[key] = v
}
assert(Exec(flag.Args(), env))
}