From c1b1e1e1631cccf48f48eab591457b7253d8b988 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 17 Aug 2022 00:15:44 +0200 Subject: [PATCH] allow variable reference in same env file --- main.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 6fe0aa8..5407cfa 100644 --- a/main.go +++ b/main.go @@ -27,20 +27,18 @@ func main() { } defer envFile.Close() - envVars := parseEnvFile(envFile) + parseEnvFile(envFile) args := flag.Args() - newEnv := append(os.Environ(), envVars...) - if len(args) == 0 { - for _, envVar := range newEnv { + for _, envVar := range os.Environ() { fmt.Println(envVar) } return } cmd := exec.Command(args[0], args[1:]...) - cmd.Env = newEnv + cmd.Env = os.Environ() cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -56,21 +54,19 @@ func main() { } } -func parseEnvFile(r io.Reader) []string { - env := make([]string, 0) +func parseEnvFile(r io.Reader) { s := bufio.NewScanner(r) l := 0 + for s.Scan() { l++ matches := EnvVarRegex.FindStringSubmatch(s.Text()) if len(matches) != 0 { key, value := matches[1], os.ExpandEnv(matches[2]) - env = append(env, fmt.Sprintf("%s=%s", key, value)) + os.Setenv(key, value) } else if s.Text() != "" && !EnvCommentRegex.MatchString(s.Text()) { panic(fmt.Sprintf("invalid env syntax on line %d", l)) } } - - return env }