parent
40803ced8b
commit
0bdffc7571
3 changed files with 36 additions and 9 deletions
@ -1,21 +1,39 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"flag" |
|
||||||
"os" |
"os" |
||||||
|
"os/exec" |
||||||
|
|
||||||
|
"gopkg.in/alecthomas/kingpin.v2" |
||||||
common "source.heropunch.io/tomo/go-coreutils" |
common "source.heropunch.io/tomo/go-coreutils" |
||||||
) |
) |
||||||
|
|
||||||
var ignoreEnv = flag.Bool( |
func main() { |
||||||
"i", |
app := kingpin.New("env", "modify the environment, then print it or run a command") |
||||||
false, |
ignoreEnv := kingpin.Flag( |
||||||
|
"ignore-environment", |
||||||
"Completely ignore the existing environment and execute cmd only with each (var, value) tuple specified.", |
"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) |
||||||
|
} |
||||||
|
|
||||||
func main() { |
|
||||||
flag.Parse() |
|
||||||
if *ignoreEnv { |
if *ignoreEnv { |
||||||
os.Clearenv() |
os.Clearenv() |
||||||
} |
} |
||||||
|
|
||||||
|
if *cmd == "" { |
||||||
common.PrintEnv() |
common.PrintEnv() |
||||||
|
os.Exit(0) |
||||||
|
} |
||||||
|
command := exec.Command(*cmd, *args...) |
||||||
|
err := command.Run() |
||||||
|
app.FatalIfError(err, "error running command:") |
||||||
} |
} |
||||||
|
@ -1,6 +1,9 @@ |
|||||||
module source.heropunch.io/tomo/go-coreutils |
module source.heropunch.io/tomo/go-coreutils |
||||||
|
|
||||||
require ( |
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/alexflint/go-arg v1.0.0 |
||||||
github.com/stretchr/testify v1.3.0 |
github.com/stretchr/testify v1.3.0 |
||||||
|
gopkg.in/alecthomas/kingpin.v2 v2.2.6 |
||||||
) |
) |
||||||
|
Loading…
Reference in new issue