Skip to content

Commit

Permalink
env:done
Browse files Browse the repository at this point in the history
update #8
  • Loading branch information
guonaihong committed Jun 13, 2019
1 parent d9005e3 commit fe8e3a8
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package env

import (
"fmt"
"github.com/guonaihong/flag"
"os"
"os/exec"
"strings"
)

type Env struct {
IgnoreEnvironment bool `opt:"i, ignore-environment" usage:"start with an empty environment"`
Unset string `opt:"u, unset" usage:"remove variable from the environment"`
Chdir string `opt:"C, chdir" usage:"change working directory to DIR"`
Null byte `opt:"0, null" usage:"end each output line with NUL, not newline"`
//Chdir string `opt:"C, chdir" usage:"change working directory to DIR"`
Null bool `opt:"0, null" usage:"end each output line with NUL, not newline"`
}

func Main(argv []string) {
var env Env
delimiter := byte('\n')

command := flag.NewFlagSet(argv[0], flag.ExitOnError)
command.ParseStruct(argv[1:], &env)
Expand All @@ -23,18 +27,34 @@ func Main(argv []string) {
os.Clearenv()
}

for _, v := range args {
kv, err := strings.Split(v, "=")
if err != nil {
//todo
if env.Null {
delimiter = byte(0)
}

for k, v := range args {
if strings.IndexByte(v, '=') > 0 {
kv := strings.SplitN(v, "=", 2)
if err := os.Setenv(kv[0], kv[1]); err != nil {
fmt.Printf("%s\n", err)
}
continue
}
if err = os.Setenv(kv[0], kv[1]); err != nil {
//todo

cmd := exec.Command(args[k], args[k+1:]...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}

os.Exit(0)
}

allArgs := os.Environ()
for _, v := range allArgs {
fmt.Fprintf(os.Stdout, "%s\n", v)
fmt.Fprintf(os.Stdout, "%s%c", v, delimiter)
}
}

0 comments on commit fe8e3a8

Please sign in to comment.