Compare commits

...

4 Commits

Author SHA1 Message Date
dtluna
0bdffc7571 Implement running commands 2019-03-26 17:53:51 +03:00
dtluna
40803ced8b Move error.go to the coreutils package 2019-03-26 15:54:28 +03:00
dtluna
0bd7c47381 Merge branch 'master' into feature/env 2019-03-26 15:53:22 +03:00
dtluna
593adb4cdf Start making env 2019-03-26 14:13:21 +03:00
6 changed files with 71 additions and 10 deletions

18
common.go Normal file
View File

@ -0,0 +1,18 @@
package coreutils
import (
"fmt"
"os"
)
// PrintStrings prints each string in strings on a new line
func PrintStrings(strings []string) {
for _, str := range strings {
fmt.Println(str)
}
}
// PrintEnv prints all environment variables
func PrintEnv() {
PrintStrings(os.Environ())
}

39
env/env.go vendored Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"os"
"os/exec"
"gopkg.in/alecthomas/kingpin.v2"
common "source.heropunch.io/tomo/go-coreutils"
)
func main() {
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()
}
if *cmd == "" {
common.PrintEnv()
os.Exit(0)
}
command := exec.Command(*cmd, *args...)
err := command.Run()
app.FatalIfError(err, "error running command:")
}

View File

@ -1,4 +1,4 @@
package common
package coreutils
import (
"fmt"

3
go.mod
View File

@ -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
)

7
go.sum
View File

@ -1,9 +1,14 @@
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=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -14,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=

View File

@ -1,19 +1,13 @@
package main
import (
"fmt"
"os"
common "source.heropunch.io/tomo/go-coreutils"
)
func printStrings(strings []string) {
for _, str := range strings {
fmt.Println(str)
}
}
func main() {
if len(os.Args) == 1 {
printStrings(os.Environ())
common.PrintEnv()
os.Exit(0)
}
@ -27,6 +21,6 @@ func main() {
exitCode = 1
}
}
printStrings(envVars)
common.PrintStrings(envVars)
os.Exit(exitCode)
}