forked from coop-cloud-mirrors/godotenv
Move the env loadings & command running into main package.
This commit is contained in:
parent
8c38c298ed
commit
b2cd7d822b
24
cmd/cmd.go
24
cmd/cmd.go
@ -3,8 +3,8 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"log"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
@ -38,23 +38,17 @@ example
|
||||
}
|
||||
|
||||
// load env
|
||||
// TODO would be nicer for an empty rawEnvFilenames to give us an empty map
|
||||
// and then only call Load() once
|
||||
// other TODO error handling on Load()
|
||||
if rawEnvFilenames == "" {
|
||||
godotenv.Load()
|
||||
} else {
|
||||
envFilenames := strings.Split(rawEnvFilenames, ",")
|
||||
godotenv.Load(envFilenames...)
|
||||
var envFilenames []string
|
||||
if rawEnvFilenames != "" {
|
||||
envFilenames = strings.Split(rawEnvFilenames, ",")
|
||||
}
|
||||
|
||||
// take rest of args and "exec" them
|
||||
cmd := args[0]
|
||||
cmdArgs := args[1:len(args)]
|
||||
|
||||
command := exec.Command(cmd, cmdArgs...)
|
||||
command.Stdin = os.Stdin
|
||||
command.Stdout = os.Stdout
|
||||
command.Stderr = os.Stderr
|
||||
command.Start()
|
||||
err := godotenv.Exec(envFilenames, cmd, cmdArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
11
godotenv.go
11
godotenv.go
@ -19,6 +19,7 @@ import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -65,6 +66,16 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
|
||||
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 {
|
||||
if len(filenames) == 0 {
|
||||
return []string{".env"}
|
||||
|
Loading…
x
Reference in New Issue
Block a user