diff --git a/env/env.go b/env/env.go index 2885208..1986842 100644 --- a/env/env.go +++ b/env/env.go @@ -1,21 +1,39 @@ package main import ( - "flag" "os" - common "source.heropunch.io/tomo/go-coreutils" -) + "os/exec" -var ignoreEnv = flag.Bool( - "i", - false, - "Completely ignore the existing environment and execute cmd only with each (var, value) tuple specified.", + "gopkg.in/alecthomas/kingpin.v2" + common "source.heropunch.io/tomo/go-coreutils" ) func main() { - flag.Parse() + app := kingpin.New("env", "modify the environment, then print it or run a command") + ignoreEnv := kingpin.Flag( + "ignore-environment", + "Completely ignore the existing environment and execute cmd only with each (var, value) tuple specified.", + ).Short('i').Bool() + unset := kingpin.Flag("unset", "Unset var in the environment.").PlaceHolder("var").Short('u').Strings() + + cmd := kingpin.Arg("cmd", "").String() + args := kingpin.Arg("args", "").Strings() + + kingpin.Parse() + + for _, key := range *unset { + os.Unsetenv(key) + } + if *ignoreEnv { os.Clearenv() } - common.PrintEnv() + + if *cmd == "" { + common.PrintEnv() + os.Exit(0) + } + command := exec.Command(*cmd, *args...) + err := command.Run() + app.FatalIfError(err, "error running command:") } diff --git a/go.mod b/go.mod index 3d9512d..55339c4 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,9 @@ module source.heropunch.io/tomo/go-coreutils require ( + github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect + github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect github.com/alexflint/go-arg v1.0.0 github.com/stretchr/testify v1.3.0 + gopkg.in/alecthomas/kingpin.v2 v2.2.6 ) diff --git a/go.sum b/go.sum index 183a049..0ef130c 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alexflint/go-arg v1.0.0 h1:VWNnY3DyBHiq5lcwY2FlCE5t5qyHNV0o5i1bkCIHprU= github.com/alexflint/go-arg v1.0.0/go.mod h1:Cto8k5VtkP4pp0EXiWD4ZJMFOOinZ38ggVcQ/6CGuRI= github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70= @@ -15,3 +19,5 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=