diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 89002f062..0ee5bd0cd 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -30,6 +30,7 @@ import ( dockercli "github.com/docker/cli/cli" "github.com/docker/cli/cli-plugins/manager" "github.com/docker/compose/v2/cmd/formatter" + "github.com/docker/compose/v2/pkg/utils" "github.com/morikuni/aec" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -158,7 +159,7 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn return nil, compose.WrapComposeError(err) } - if o.Compatibility || project.Environment["COMPOSE_COMPATIBILITY"] == "true" { + if o.Compatibility || utils.StringToBool(project.Environment["COMPOSE_COMPATIBILITY"]) { compose.Separator = "_" } diff --git a/cmd/compose/down.go b/cmd/compose/down.go index 4c0369de9..daca5164f 100644 --- a/cmd/compose/down.go +++ b/cmd/compose/down.go @@ -20,10 +20,10 @@ import ( "context" "fmt" "os" - "strings" "time" "github.com/compose-spec/compose-go/types" + "github.com/docker/compose/v2/pkg/utils" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -62,7 +62,7 @@ func downCommand(p *projectOptions, backend api.Service) *cobra.Command { ValidArgsFunction: noCompletion(), } flags := downCmd.Flags() - removeOrphans := strings.ToLower(os.Getenv("COMPOSE_REMOVE_ORPHANS ")) == "true" + removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS ")) flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.") flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds") flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.") diff --git a/cmd/compose/up.go b/cmd/compose/up.go index f889d3b8e..aebf53289 100644 --- a/cmd/compose/up.go +++ b/cmd/compose/up.go @@ -103,8 +103,7 @@ func upCommand(p *projectOptions, backend api.Service) *cobra.Command { return validateFlags(&up, &create) }), RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error { - ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"] - create.ignoreOrphans = strings.ToLower(ignore) == "true" + create.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"]) if create.ignoreOrphans && create.removeOrphans { return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined") } diff --git a/pkg/utils/stringutils.go b/pkg/utils/stringutils.go index fd2dd477d..01dd42387 100644 --- a/pkg/utils/stringutils.go +++ b/pkg/utils/stringutils.go @@ -16,6 +16,11 @@ package utils +import ( + "strconv" + "strings" +) + // StringContains check if an array contains a specific value func StringContains(array []string, needle string) bool { for _, val := range array { @@ -25,3 +30,9 @@ func StringContains(array []string, needle string) bool { } return false } + +// StringToBool converts a string to a boolean ignoring errors +func StringToBool(s string) bool { + b, _ := strconv.ParseBool(strings.ToLower(strings.TrimSpace(s))) + return b +}