Skip to content

Commit 1a4e6c2

Browse files
authored
Merge pull request docker#9230 from ulyssessouza/treat-env-bool-vars
Add function to convert strings to bool
2 parents c12a948 + 67b4669 commit 1a4e6c2

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

cmd/compose/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
dockercli "github.com/docker/cli/cli"
3131
"github.com/docker/cli/cli-plugins/manager"
3232
"github.com/docker/compose/v2/cmd/formatter"
33+
"github.com/docker/compose/v2/pkg/utils"
3334
"github.com/morikuni/aec"
3435
"github.com/pkg/errors"
3536
"github.com/sirupsen/logrus"
@@ -158,7 +159,7 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn
158159
return nil, compose.WrapComposeError(err)
159160
}
160161

161-
if o.Compatibility || project.Environment["COMPOSE_COMPATIBILITY"] == "true" {
162+
if o.Compatibility || utils.StringToBool(project.Environment["COMPOSE_COMPATIBILITY"]) {
162163
compose.Separator = "_"
163164
}
164165

cmd/compose/down.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23-
"strings"
2423
"time"
2524

2625
"github.com/compose-spec/compose-go/types"
26+
"github.com/docker/compose/v2/pkg/utils"
2727
"github.com/sirupsen/logrus"
2828
"github.com/spf13/cobra"
2929
"github.com/spf13/pflag"
@@ -62,7 +62,7 @@ func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
6262
ValidArgsFunction: noCompletion(),
6363
}
6464
flags := downCmd.Flags()
65-
removeOrphans := strings.ToLower(os.Getenv("COMPOSE_REMOVE_ORPHANS ")) == "true"
65+
removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS "))
6666
flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
6767
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
6868
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.")

cmd/compose/up.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ func upCommand(p *projectOptions, backend api.Service) *cobra.Command {
103103
return validateFlags(&up, &create)
104104
}),
105105
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
106-
ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"]
107-
create.ignoreOrphans = strings.ToLower(ignore) == "true"
106+
create.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])
108107
if create.ignoreOrphans && create.removeOrphans {
109108
return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined")
110109
}

pkg/utils/stringutils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package utils
1818

19+
import (
20+
"strconv"
21+
"strings"
22+
)
23+
1924
// StringContains check if an array contains a specific value
2025
func StringContains(array []string, needle string) bool {
2126
for _, val := range array {
@@ -25,3 +30,9 @@ func StringContains(array []string, needle string) bool {
2530
}
2631
return false
2732
}
33+
34+
// StringToBool converts a string to a boolean ignoring errors
35+
func StringToBool(s string) bool {
36+
b, _ := strconv.ParseBool(strings.ToLower(strings.TrimSpace(s)))
37+
return b
38+
}

0 commit comments

Comments
 (0)