Move the env loadings & command running into main package.

This commit is contained in:
John Barton (joho) 2014-10-12 09:50:38 +11:00
parent 8c38c298ed
commit b2cd7d822b
2 changed files with 20 additions and 15 deletions

View File

@ -3,8 +3,8 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os" "log"
"os/exec"
"strings" "strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@ -38,23 +38,17 @@ example
} }
// load env // load env
// TODO would be nicer for an empty rawEnvFilenames to give us an empty map var envFilenames []string
// and then only call Load() once if rawEnvFilenames != "" {
// other TODO error handling on Load() envFilenames = strings.Split(rawEnvFilenames, ",")
if rawEnvFilenames == "" {
godotenv.Load()
} else {
envFilenames := strings.Split(rawEnvFilenames, ",")
godotenv.Load(envFilenames...)
} }
// take rest of args and "exec" them // take rest of args and "exec" them
cmd := args[0] cmd := args[0]
cmdArgs := args[1:len(args)] cmdArgs := args[1:len(args)]
command := exec.Command(cmd, cmdArgs...) err := godotenv.Exec(envFilenames, cmd, cmdArgs)
command.Stdin = os.Stdin if err != nil {
command.Stdout = os.Stdout log.Fatal(err)
command.Stderr = os.Stderr }
command.Start()
} }

View File

@ -19,6 +19,7 @@ import (
"bufio" "bufio"
"errors" "errors"
"os" "os"
"os/exec"
"strings" "strings"
) )
@ -65,6 +66,16 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
return return
} }
func Exec(filenames []string, cmd string, cmdArgs []string) error {
Load(filenames...)
command := exec.Command(cmd, cmdArgs...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()
}
func filenamesOrDefault(filenames []string) []string { func filenamesOrDefault(filenames []string) []string {
if len(filenames) == 0 { if len(filenames) == 0 {
return []string{".env"} return []string{".env"}