diff --git a/cli/options.go b/cli/options.go index f20803b0..abd4e5af 100644 --- a/cli/options.go +++ b/cli/options.go @@ -194,7 +194,7 @@ func WithEnv(env []string) ProjectOptionsFn { } } -// WithDiscardEnvFiles sets discards the `env_file` section after resolving to +// WithDiscardEnvFile sets discards the `env_file` section after resolving to // the `environment` section func WithDiscardEnvFile(o *ProjectOptions) error { o.loadOptions = append(o.loadOptions, loader.WithDiscardEnvFiles) @@ -209,6 +209,15 @@ func WithLoadOptions(loadOptions ...func(*loader.Options)) ProjectOptionsFn { } } +// WithDefaultProfiles uses the provided profiles (if any), and falls back to +// profiles specified via the COMPOSE_PROFILES environment variable otherwise. +func WithDefaultProfiles(profile ...string) ProjectOptionsFn { + if len(profile) == 0 { + profile = strings.Split(os.Getenv(consts.ComposeProfiles), ",") + } + return WithProfiles(profile) +} + // WithProfiles sets profiles to be activated func WithProfiles(profiles []string) ProjectOptionsFn { return func(o *ProjectOptions) error { @@ -228,8 +237,9 @@ func WithOsEnv(o *ProjectOptions) error { return nil } -// WithEnvFile set an alternate env file -// deprecated - use WithEnvFiles +// WithEnvFile sets an alternate env file. +// +// Deprecated: use WithEnvFiles instead. func WithEnvFile(file string) ProjectOptionsFn { var files []string if file != "" { diff --git a/loader/loader.go b/loader/loader.go index 6b80ab23..74dc5c86 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -360,9 +360,6 @@ func load(ctx context.Context, configDetails types.ConfigDetails, opts *Options, } } - if profiles, ok := project.Environment[consts.ComposeProfiles]; ok && len(opts.Profiles) == 0 { - opts.Profiles = strings.Split(profiles, ",") - } project.ApplyProfiles(opts.Profiles) err := project.ResolveServicesEnvironment(opts.discardEnvFiles)