From 2ecc6056716c48e2e46901f02e800ab693014c78 Mon Sep 17 00:00:00 2001 From: Patrik Lermon Date: Thu, 27 Nov 2025 17:17:10 +0100 Subject: [PATCH] Add -e/--env flag to specify environment files Implements a CLI flag to specify environment files, matching Honcho's behavior. This allows users to specify custom env files via command-line flag instead of only through the OVERMIND_ENV environment variable. Changes: - Add EnvFiles field to start.Handler struct - Add -e/--env flag to start command (maps to OVERMIND_ENV env var) - Add loadEnvFiles() method to parse and load comma-separated env files - Load env files from flag before processing other options The flag accepts comma-separated file paths and loads them after the default env files (~/.overmind.env, ./.overmind.env, ./.env), allowing flag-specified files to override default values. Usage: overmind start -e .env.local overmind start -e .env.local,.env.development overmind start --env .env.local,.env.development Closes #206 --- main.go | 1 + start/handler.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/main.go b/main.go index 8ad8f56..3fa429a 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,7 @@ func setupStartCmd() cli.Command { cli.StringFlag{Name: "tmux-config, F", EnvVar: "OVERMIND_TMUX_CONFIG", Usage: "Specify an alternative tmux config path to be used by Overmind", Destination: &c.TmuxConfigPath}, cli.StringFlag{Name: "ignored-processes, x", EnvVar: "OVERMIND_IGNORED_PROCESSES", Usage: "Specify process names to prevent from launching. Useful if you want to run all but one or two processes. Divide names with comma. Takes precedence over the 'processes' flag.", Destination: &c.IgnoredProcNames}, cli.StringFlag{Name: "shell, H", EnvVar: "OVERMIND_SHELL", Usage: "Specify shell to run processes with.", Value: "sh", Destination: &c.Shell}, + cli.StringFlag{Name: "env, e", EnvVar: "OVERMIND_ENV", Usage: "Environment file[,file] to load. Can specify multiple files separated by comma", Destination: &c.EnvFiles}, }, socketFlags(&c.SocketPath, &c.Network)..., ), diff --git a/start/handler.go b/start/handler.go index 4244519..f75825b 100644 --- a/start/handler.go +++ b/start/handler.go @@ -10,6 +10,7 @@ import ( "syscall" "github.com/DarthSim/overmind/v2/utils" + "github.com/Envek/godotenv" "github.com/urfave/cli" ) @@ -48,6 +49,7 @@ type Handler struct { Daemonize bool TmuxConfigPath string Shell string + EnvFiles string } // AbsRoot returns absolute path to the working directory @@ -65,6 +67,11 @@ func (h *Handler) AbsRoot() (string, error) { // Run runs the start command func (h *Handler) Run(c *cli.Context) error { + // Load env files specified via -e/--env flag + if len(h.EnvFiles) > 0 { + h.loadEnvFiles(h.EnvFiles) + } + err := h.parseColors(c.String("colors")) utils.FatalOnErr(err) @@ -166,3 +173,18 @@ func (h *Handler) parseStopSignals(signals string) error { return nil } + +func (h *Handler) loadEnvFiles(envFiles string) { + envs := strings.Split(envFiles, ",") + for _, e := range envs { + e = strings.TrimSpace(e) + if len(e) > 0 { + err := godotenv.Overload(e) + if err != nil { + if !os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "overmind: skipping %s due to error: %v\n", e, err) + } + } + } + } +}