mirror of
http://git.dtluna.net/tomo/go-coreutils.git
synced 2025-01-07 06:19:54 +00:00
Add printenv
This commit is contained in:
parent
73e7ba9c1c
commit
b7178c3a2c
@ -14,3 +14,4 @@ Implemeted utilities:
|
||||
* `dirname`
|
||||
* `basename`
|
||||
* `echo`
|
||||
* `printenv`
|
||||
|
32
printenv/printenv.go
Normal file
32
printenv/printenv.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func printStrings(strings []string) {
|
||||
for _, str := range strings {
|
||||
fmt.Println(str)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
printStrings(os.Environ())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
exitCode := 0
|
||||
envVars := []string{}
|
||||
for _, key := range os.Args[1:] {
|
||||
val, present := os.LookupEnv(key)
|
||||
if present {
|
||||
envVars = append(envVars, val)
|
||||
} else {
|
||||
exitCode = 1
|
||||
}
|
||||
}
|
||||
printStrings(envVars)
|
||||
os.Exit(exitCode)
|
||||
}
|
Loading…
Reference in New Issue
Block a user