go build -o readenv cmd/readenv/main.gomv readenv /usr/local/bin/readenv
eval (readenv .env)
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "strings" | |
| ) | |
| func main() { | |
| path := os.Args[1:] | |
| if len(path) == 0 { | |
| os.Stderr.Write([]byte("file path is a required arg\n")) | |
| os.Exit(1) | |
| } | |
| if len(path) > 1 { | |
| os.Stderr.Write([]byte("only 1 path is allowed\n")) | |
| os.Exit(1) | |
| } | |
| fp, err := os.Open(path[0]) | |
| if err != nil { | |
| os.Stderr.Write([]byte(fmt.Sprintf("unable to open file %s: %s\n", path[0], err))) | |
| os.Exit(1) | |
| } | |
| s := bufio.NewScanner(fp) | |
| for s.Scan() { | |
| t := strings.TrimSpace(s.Text()) | |
| if len(t) == 0 || t[0] == '#' { | |
| continue | |
| } | |
| tokens := strings.Split(t, "=") | |
| if len(tokens) != 2 { | |
| os.Stdout.Write([]byte(fmt.Sprintf("# invalid token: %s;\n", t))) | |
| continue | |
| } | |
| os.Stdout.Write([]byte(fmt.Sprintf("set -x %s %s;\n", tokens[0], tokens[1]))) | |
| } | |
| } |