Skip to content

Commit c7abdac

Browse files
committed
Add an --env flag to specify env files to load
By default `godotenv.Load` will load the file `.env`. This commit adds an option to override that. You can, for example use `--env=.env,.env.local` to allow team members to override environment variables or `--env=""` to avoid loading any environment file at all.
1 parent ebb9736 commit c7abdac

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ var exitOnStop = flag.Bool("exit-on-stop", true, "Exit goreman if all subprocess
9898
// show timestamp in log
9999
var logTime = flag.Bool("logtime", true, "show timestamp in log")
100100

101+
var envFileOption = flag.String("env", ".env", "Environment files to load, comma separated")
102+
101103
var maxProcNameLength = 0
102104

103105
var re = regexp.MustCompile(`\$([a-zA-Z]+[a-zA-Z0-9_]+)`)
@@ -109,6 +111,7 @@ type config struct {
109111
BaseDir string `yaml:"basedir"`
110112
BasePort uint `yaml:"baseport"`
111113
Args []string
114+
EnvFiles []string
112115
// If true, exit the supervisor process if a subprocess exits with an error.
113116
ExitOnError bool `yaml:"exit_on_error"`
114117
}
@@ -125,6 +128,7 @@ func readConfig() *config {
125128
cfg.Port = *port
126129
cfg.BaseDir = *basedir
127130
cfg.BasePort = *baseport
131+
cfg.EnvFiles = strings.FieldsFunc(*envFileOption, func(char rune) bool { return char == ',' })
128132
cfg.ExitOnError = *exitOnError
129133
cfg.Args = flag.Args()
130134

@@ -263,7 +267,9 @@ func start(ctx context.Context, sig <-chan os.Signal, cfg *config) error {
263267
procs = tmp
264268
mu.Unlock()
265269
}
266-
godotenv.Load()
270+
if len(cfg.EnvFiles) > 0 {
271+
godotenv.Load(cfg.EnvFiles...)
272+
}
267273
rpcChan := make(chan *rpcMessage, 10)
268274
if *startRPCServer {
269275
go startServer(ctx, rpcChan, cfg.Port)

0 commit comments

Comments
 (0)