Skip to content

Commit

Permalink
feat: Introduce support for supplying --env-file in compose
Browse files Browse the repository at this point in the history
This commit introduces support to all compose subcommands (both
in the local and cloud) for supplying an alternative environmental
variables file (i.e. other than `.env`) via the flag `--env-file`.
This increases compatibility with existing compose-based CLI tools
like `docker`.

Signed-off-by: Alexander Jung <[email protected]>
  • Loading branch information
nderjung committed Dec 23, 2024
1 parent 95e2a12 commit f543e1a
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 35 deletions.
3 changes: 2 additions & 1 deletion compose/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var DefaultFileNames = []string{

// NewProjectFromComposeFile loads a compose file and returns a project. If no
// compose file is specified, it will look for one in the current directory.
func NewProjectFromComposeFile(ctx context.Context, workdir, composefile string) (*Project, error) {
func NewProjectFromComposeFile(ctx context.Context, workdir, composefile string, opts ...cli.ProjectOptionsFn) (*Project, error) {
if composefile == "" {
for _, file := range DefaultFileNames {
fullpath := filepath.Join(workdir, file)
Expand All @@ -62,6 +62,7 @@ func NewProjectFromComposeFile(ctx context.Context, workdir, composefile string)

options, err := cli.NewProjectOptions(
[]string{fullpath},
opts...,
)
if err != nil {
return nil, err
Expand Down
10 changes: 8 additions & 2 deletions internal/cli/kraft/cloud/compose/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
Expand All @@ -33,8 +34,9 @@ import (

type BuildOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Composefile string `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Metro string `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Push bool `long:"push" usage:"Push the built service images"`
Expand Down Expand Up @@ -102,7 +104,11 @@ func Build(ctx context.Context, opts *BuildOptions, args ...string) error {
return err
}

opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/cli/kraft/cloud/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

type ComposeOptions struct {
Composefile string `long:"file" usage:"Set the Compose file."`
EnvFile string `long:"env-file" usage:"Set the environment file."`
}

func NewCmd() *cobra.Command {
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 @@ -13,6 +13,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
Expand All @@ -28,6 +29,7 @@ type DownOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Metro string `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Token string `noattribute:"true"`
Expand Down Expand Up @@ -88,7 +90,11 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error {
return err
}

opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
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 @@ -12,6 +12,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

"kraftkit.sh/cmdfactory"
Expand All @@ -23,6 +24,7 @@ import (

type ListOptions struct {
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list,raw" default:"table"`
Token string `noattribute:"true"`
}
Expand Down Expand Up @@ -64,7 +66,11 @@ func (opts *ListOptions) Run(ctx context.Context, args []string) error {
workdir = args[0]
}

project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
project, err := compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
Expand All @@ -27,6 +28,7 @@ type LogsOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Follow bool `long:"follow" short:"f" usage:"Follow log output"`
Metro string `noattribute:"true"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list,raw" default:"table"`
Expand Down Expand Up @@ -99,7 +101,11 @@ func Logs(ctx context.Context, opts *LogsOptions, args ...string) error {
}

if opts.Project == nil {
opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
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 @@ -12,6 +12,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
Expand All @@ -27,6 +28,7 @@ type PsOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Metro string `noattribute:"true"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list,raw" default:"table"`
Project *compose.Project `noattribute:"true"`
Expand Down Expand Up @@ -84,7 +86,11 @@ func (opts *PsOptions) Run(ctx context.Context, args []string) error {
return err
}

opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"

Expand All @@ -31,6 +32,7 @@ type PushOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kcinstances.InstancesService `noattribute:"true"`
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Metro string `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Token string `noattribute:"true"`
Expand Down Expand Up @@ -79,7 +81,11 @@ func Push(ctx context.Context, opts *PushOptions, args ...string) error {
}

if opts.Project == nil {
opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
Expand All @@ -28,6 +29,7 @@ type StartOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
Metro string `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Token string `noattribute:"true"`
Expand Down Expand Up @@ -93,7 +95,11 @@ func (opts *StartOptions) Run(ctx context.Context, args []string) error {
return err
}

opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
Expand All @@ -30,6 +31,7 @@ type StopOptions struct {
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
DrainTimeout time.Duration `long:"drain-timeout" short:"d" usage:"Timeout for the instance to stop (ms/s/m/h)"`
EnvFile string `noattribute:"true"`
Force bool `long:"force" short:"f" usage:"Force stop the instance(s)"`
Metro string `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Expand Down Expand Up @@ -103,7 +105,11 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error {
return err
}

opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/cloud/compose/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -41,6 +42,7 @@ type UpOptions struct {
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
Detach bool `local:"true" long:"detach" short:"d" usage:"Run the services in the background"`
EnvFile string `noattribute:"true"`
Metro string `noattribute:"true"`
NoStart bool `noattribute:"true"`
NoBuild bool `local:"true" long:"no-build" usage:"Do not build the services before starting them"`
Expand Down Expand Up @@ -136,7 +138,11 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {
return err
}

opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
opts.Project, err = compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/compose/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"

composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/types"
"github.com/spf13/cobra"

Expand All @@ -24,6 +25,7 @@ import (

type BuildOptions struct {
composefile string
EnvFile string `noattribute:"true"`
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -63,7 +65,11 @@ func (opts *BuildOptions) Run(ctx context.Context, args []string) error {
return err
}

project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile)
project, err := compose.NewProjectFromComposeFile(ctx,
workdir,
opts.composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/cli/kraft/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

type ComposeOptions struct {
Composefile string `long:"file" short:"f" usage:"Set the Compose file."`
EnvFile string `long:"env-file" usage:"Set the environment file."`
}

func NewCmd() *cobra.Command {
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/kraft/compose/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/types"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -42,6 +43,7 @@ import (

type CreateOptions struct {
Composefile string `noattribute:"true"`
EnvFile string `noattribute:"true"`
RemoveOrphans bool `long:"remove-orphans" usage:"Remove machines for services not defined in the Compose file"`
}

Expand Down Expand Up @@ -88,7 +90,11 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error {
return err
}

project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
project, err := compose.NewProjectFromComposeFile(ctx,
workdir,
opts.Composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions internal/cli/kraft/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"

"github.com/MakeNowJust/heredoc"
composespec "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/types"

"github.com/spf13/cobra"
Expand All @@ -29,7 +30,8 @@ import (

type DownOptions struct {
composefile string
RemoveOrphans bool `long:"remove-orphans" usage:"Remove machines for services not defined in the Compose file."`
EnvFile string `noattribute:"true"`
RemoveOrphans bool `long:"remove-orphans" usage:"Remove machines for services not defined in the Compose file."`
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -74,7 +76,11 @@ func (opts *DownOptions) Run(ctx context.Context, args []string) error {
if err != nil {
return err
}
project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile)
project, err := compose.NewProjectFromComposeFile(ctx,
workdir,
opts.composefile,
composespec.WithEnvFiles(opts.EnvFile),
)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit f543e1a

Please sign in to comment.