Skip to content

Commit

Permalink
fix(internal): Load env vars from file when specified
Browse files Browse the repository at this point in the history
By default it uses '.env' if found. If '--env-file' is specified
then use that instead of it.

Signed-off-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
craciunoiuc committed Jan 14, 2025
1 parent b32c7b1 commit 462a8fb
Show file tree
Hide file tree
Showing 20 changed files with 208 additions and 20 deletions.
12 changes: 11 additions & 1 deletion internal/cli/kraft/cloud/compose/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/cloud/compose/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/cloud/compose/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/cloud/compose/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/cloud/compose/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/cloud/compose/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/compose/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/compose/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/compose/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/cli/kraft/compose/pause/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
Loading

0 comments on commit 462a8fb

Please sign in to comment.