forked from joho/godotenv
/
cmd.go
54 lines (42 loc) · 1008 Bytes
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/joho/godotenv"
)
func main() {
var showHelp bool
flag.BoolVar(&showHelp, "h", false, "show help")
var rawEnvFilenames string
flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files")
flag.Parse()
usage := `
Run a process with a env setup from a .env file
godotenv [-f ENV_FILE_PATHS] COMMAND_ARGS
ENV_FILE_PATHS: comma separated paths to .env files
COMMAND_ARGS: command and args you want to run
example
godotenv -f /path/to/something/.env,/another/path/.env fortune
`
// if no args or -h flag
// print usage and return
args := flag.Args()
if showHelp || len(args) == 0 {
fmt.Println(usage)
return
}
// load env
var envFilenames []string
if rawEnvFilenames != "" {
envFilenames = strings.Split(rawEnvFilenames, ",")
}
// take rest of args and "exec" them
cmd := args[0]
cmdArgs := args[1:]
err := godotenv.Exec(envFilenames, cmd, cmdArgs)
if err != nil {
log.Fatal(err)
}
}