From 462a8fbcbc3c770ba4a96d0946beffbe6885c64f Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Tue, 14 Jan 2025 17:28:47 +0200 Subject: [PATCH] fix(internal): Load env vars from file when specified By default it uses '.env' if found. If '--env-file' is specified then use that instead of it. Signed-off-by: Cezar Craciunoiu --- internal/cli/kraft/cloud/compose/build/build.go | 12 +++++++++++- internal/cli/kraft/cloud/compose/down/down.go | 8 +++++++- internal/cli/kraft/cloud/compose/list/list.go | 8 +++++++- internal/cli/kraft/cloud/compose/logs/logs.go | 12 +++++++++++- internal/cli/kraft/cloud/compose/ps/ps.go | 8 +++++++- internal/cli/kraft/cloud/compose/push/push.go | 12 +++++++++++- internal/cli/kraft/cloud/compose/start/start.go | 12 +++++++++++- internal/cli/kraft/cloud/compose/stop/stop.go | 12 +++++++++++- internal/cli/kraft/cloud/compose/up/up.go | 12 +++++++++++- internal/cli/kraft/compose/build/build.go | 12 +++++++++++- internal/cli/kraft/compose/create/create.go | 12 +++++++++++- internal/cli/kraft/compose/down/down.go | 12 +++++++++++- internal/cli/kraft/compose/logs/logs.go | 12 +++++++++++- internal/cli/kraft/compose/pause/pause.go | 12 +++++++++++- internal/cli/kraft/compose/ps/ps.go | 12 +++++++++++- internal/cli/kraft/compose/pull/pull.go | 12 +++++++++++- internal/cli/kraft/compose/push/push.go | 12 +++++++++++- internal/cli/kraft/compose/start/start.go | 12 +++++++++++- internal/cli/kraft/compose/stop/stop.go | 12 +++++++++++- internal/cli/kraft/compose/unpause/unpause.go | 12 +++++++++++- 20 files changed, 208 insertions(+), 20 deletions(-) diff --git a/internal/cli/kraft/cloud/compose/build/build.go b/internal/cli/kraft/cloud/compose/build/build.go index be352ca05..fdbc6b6bd 100644 --- a/internal/cli/kraft/cloud/compose/build/build.go +++ b/internal/cli/kraft/cloud/compose/build/build.go @@ -104,10 +104,16 @@ func Build(ctx context.Context, opts *BuildOptions, args ...string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err @@ -337,6 +343,10 @@ func (opts *BuildOptions) Pre(cmd *cobra.Command, args []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + return nil } diff --git a/internal/cli/kraft/cloud/compose/down/down.go b/internal/cli/kraft/cloud/compose/down/down.go index 4269faedb..3eae34066 100644 --- a/internal/cli/kraft/cloud/compose/down/down.go +++ b/internal/cli/kraft/cloud/compose/down/down.go @@ -90,10 +90,16 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/cloud/compose/list/list.go b/internal/cli/kraft/cloud/compose/list/list.go index 1fb9d5943..b21a80fa2 100644 --- a/internal/cli/kraft/cloud/compose/list/list.go +++ b/internal/cli/kraft/cloud/compose/list/list.go @@ -66,10 +66,16 @@ func (opts *ListOptions) Run(ctx context.Context, args []string) error { workdir = args[0] } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/cloud/compose/logs/logs.go b/internal/cli/kraft/cloud/compose/logs/logs.go index 728771060..4ef750a03 100644 --- a/internal/cli/kraft/cloud/compose/logs/logs.go +++ b/internal/cli/kraft/cloud/compose/logs/logs.go @@ -72,6 +72,10 @@ func (opts *LogsOptions) Pre(cmd *cobra.Command, args []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + return nil } @@ -101,10 +105,16 @@ func Logs(ctx context.Context, opts *LogsOptions, args ...string) error { } if opts.Project == nil { + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/cloud/compose/ps/ps.go b/internal/cli/kraft/cloud/compose/ps/ps.go index d634423d2..fdfec0607 100644 --- a/internal/cli/kraft/cloud/compose/ps/ps.go +++ b/internal/cli/kraft/cloud/compose/ps/ps.go @@ -86,10 +86,16 @@ func (opts *PsOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/cloud/compose/push/push.go b/internal/cli/kraft/cloud/compose/push/push.go index 45b7dbdbf..637b94d49 100644 --- a/internal/cli/kraft/cloud/compose/push/push.go +++ b/internal/cli/kraft/cloud/compose/push/push.go @@ -81,10 +81,16 @@ func Push(ctx context.Context, opts *PushOptions, args ...string) error { } if opts.Project == nil { + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err @@ -193,6 +199,10 @@ func (opts *PushOptions) Pre(cmd *cobra.Command, args []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + return nil } diff --git a/internal/cli/kraft/cloud/compose/start/start.go b/internal/cli/kraft/cloud/compose/start/start.go index 346d56403..83c4a4c60 100644 --- a/internal/cli/kraft/cloud/compose/start/start.go +++ b/internal/cli/kraft/cloud/compose/start/start.go @@ -70,6 +70,10 @@ func (opts *StartOptions) Pre(cmd *cobra.Command, args []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + return nil } @@ -95,10 +99,16 @@ func (opts *StartOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/cloud/compose/stop/stop.go b/internal/cli/kraft/cloud/compose/stop/stop.go index 33cd61961..52f4df9ca 100644 --- a/internal/cli/kraft/cloud/compose/stop/stop.go +++ b/internal/cli/kraft/cloud/compose/stop/stop.go @@ -80,6 +80,10 @@ func (opts *StopOptions) Pre(cmd *cobra.Command, args []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + return nil } @@ -105,10 +109,16 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/cloud/compose/up/up.go b/internal/cli/kraft/cloud/compose/up/up.go index feaf41fb8..f4e18280a 100644 --- a/internal/cli/kraft/cloud/compose/up/up.go +++ b/internal/cli/kraft/cloud/compose/up/up.go @@ -109,6 +109,10 @@ func (opts *UpOptions) Pre(cmd *cobra.Command, args []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + return nil } @@ -138,10 +142,16 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/build/build.go b/internal/cli/kraft/compose/build/build.go index 7165b372d..889d19c04 100644 --- a/internal/cli/kraft/compose/build/build.go +++ b/internal/cli/kraft/compose/build/build.go @@ -55,6 +55,10 @@ func (opts *BuildOptions) Pre(cmd *cobra.Command, _ []string) error { opts.composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using") return nil } @@ -65,10 +69,16 @@ func (opts *BuildOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/create/create.go b/internal/cli/kraft/compose/create/create.go index 6b17ca986..3fd8f21df 100644 --- a/internal/cli/kraft/compose/create/create.go +++ b/internal/cli/kraft/compose/create/create.go @@ -80,6 +80,10 @@ func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.Composefile).Debug("using") return nil } @@ -90,10 +94,16 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/down/down.go b/internal/cli/kraft/compose/down/down.go index c4000b10c..c08503d86 100644 --- a/internal/cli/kraft/compose/down/down.go +++ b/internal/cli/kraft/compose/down/down.go @@ -67,6 +67,10 @@ func (opts *DownOptions) Pre(cmd *cobra.Command, _ []string) error { opts.composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using") return nil } @@ -76,10 +80,16 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error { if err != nil { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/logs/logs.go b/internal/cli/kraft/compose/logs/logs.go index 2b782c2bf..6b9f51d5b 100644 --- a/internal/cli/kraft/compose/logs/logs.go +++ b/internal/cli/kraft/compose/logs/logs.go @@ -55,6 +55,10 @@ func (opts *LogsOptions) Pre(cmd *cobra.Command, _ []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.Composefile).Debug("using") return nil @@ -66,10 +70,16 @@ func (opts *LogsOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/pause/pause.go b/internal/cli/kraft/compose/pause/pause.go index 90c8b1275..72cd8a820 100644 --- a/internal/cli/kraft/compose/pause/pause.go +++ b/internal/cli/kraft/compose/pause/pause.go @@ -59,6 +59,10 @@ func (opts *PauseOptions) Pre(cmd *cobra.Command, _ []string) error { opts.composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using") return nil } @@ -69,10 +73,16 @@ func (opts *PauseOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/ps/ps.go b/internal/cli/kraft/compose/ps/ps.go index 29abebbd3..2158e38f2 100644 --- a/internal/cli/kraft/compose/ps/ps.go +++ b/internal/cli/kraft/compose/ps/ps.go @@ -67,6 +67,10 @@ func (opts *PsOptions) Pre(cmd *cobra.Command, _ []string) error { opts.composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using") return nil } @@ -76,10 +80,16 @@ func (opts *PsOptions) Run(ctx context.Context, args []string) error { if err != nil { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/pull/pull.go b/internal/cli/kraft/compose/pull/pull.go index e271c4509..a0aeaae47 100644 --- a/internal/cli/kraft/compose/pull/pull.go +++ b/internal/cli/kraft/compose/pull/pull.go @@ -59,6 +59,10 @@ func (opts *PullOptions) Pre(cmd *cobra.Command, _ []string) error { opts.composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using") return nil } @@ -69,10 +73,16 @@ func (opts *PullOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/push/push.go b/internal/cli/kraft/compose/push/push.go index a9fa75ed9..d4cc4cea1 100644 --- a/internal/cli/kraft/compose/push/push.go +++ b/internal/cli/kraft/compose/push/push.go @@ -58,6 +58,10 @@ func (opts *PushOptions) Pre(cmd *cobra.Command, _ []string) error { opts.composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using") return nil } @@ -68,10 +72,16 @@ func (opts *PushOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/start/start.go b/internal/cli/kraft/compose/start/start.go index 5f8e5968b..eeebdadd6 100644 --- a/internal/cli/kraft/compose/start/start.go +++ b/internal/cli/kraft/compose/start/start.go @@ -59,6 +59,10 @@ func (opts *StartOptions) Pre(cmd *cobra.Command, _ []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.Composefile).Debug("using") return nil } @@ -69,10 +73,16 @@ func (opts *StartOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/stop/stop.go b/internal/cli/kraft/compose/stop/stop.go index 7093953ef..f77be14ef 100644 --- a/internal/cli/kraft/compose/stop/stop.go +++ b/internal/cli/kraft/compose/stop/stop.go @@ -59,6 +59,10 @@ func (opts *StopOptions) Pre(cmd *cobra.Command, _ []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.Composefile).Debug("using") return nil } @@ -69,10 +73,16 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err diff --git a/internal/cli/kraft/compose/unpause/unpause.go b/internal/cli/kraft/compose/unpause/unpause.go index 082dfa931..301c192ef 100644 --- a/internal/cli/kraft/compose/unpause/unpause.go +++ b/internal/cli/kraft/compose/unpause/unpause.go @@ -59,6 +59,10 @@ func (opts *UnpauseOptions) Pre(cmd *cobra.Command, _ []string) error { opts.Composefile = cmd.Flag("file").Value.String() } + if cmd.Flag("env-file").Changed { + opts.EnvFile = cmd.Flag("env-file").Value.String() + } + log.G(cmd.Context()).WithField("composefile", opts.Composefile).Debug("using") return nil } @@ -69,10 +73,16 @@ func (opts *UnpauseOptions) Run(ctx context.Context, args []string) error { return err } + var envFiles []string + if opts.EnvFile != "" { + envFiles = append(envFiles, opts.EnvFile) + } + project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile, - composespec.WithEnvFiles(opts.EnvFile), + composespec.WithEnvFiles(envFiles...), + composespec.WithDotEnv, ) if err != nil { return err