Skip to content

Commit fd7847f

Browse files
committed
parallel flag belong do top-level "compose" cobra command, not the current one
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 18a112e commit fd7847f

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

cmd/compose/compose.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ import (
4747
"github.com/docker/compose/v2/pkg/utils"
4848
)
4949

50+
const (
51+
// ComposeParallelLimit set the limit running concurrent operation on docker engine
52+
ComposeParallelLimit = "COMPOSE_PARALLEL_LIMIT"
53+
// ComposeProjectName define the project name to be used, instead of guessing from parent directory
54+
ComposeProjectName = "COMPOSE_PROJECT_NAME"
55+
// ComposeCompatibility try to mimic compose v1 as much as possible
56+
ComposeCompatibility = "COMPOSE_COMPATIBILITY"
57+
// ComposeRemoveOrphans remove “orphaned" containers, i.e. containers tagged for current project but not declared as service
58+
ComposeRemoveOrphans = "COMPOSE_REMOVE_ORPHANS"
59+
// ComposeIgnoreOrphans ignore "orphaned" containers
60+
ComposeIgnoreOrphans = "COMPOSE_IGNORE_ORPHANS"
61+
)
62+
5063
// Command defines a compose CLI command as a func with args
5164
type Command func(context.Context, []string) error
5265

@@ -145,7 +158,7 @@ func (o *ProjectOptions) projectOrName(services ...string) (*types.Project, stri
145158
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
146159
p, err := o.ToProject(services, cli.WithDiscardEnvFile)
147160
if err != nil {
148-
envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
161+
envProjectName := os.Getenv(ComposeProjectName)
149162
if envProjectName != "" {
150163
return nil, envProjectName, nil
151164
}
@@ -162,7 +175,7 @@ func (o *ProjectOptions) toProjectName() (string, error) {
162175
return o.ProjectName, nil
163176
}
164177

165-
envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
178+
envProjectName := os.Getenv("ComposeProjectName")
166179
if envProjectName != "" {
167180
return envProjectName, nil
168181
}
@@ -180,7 +193,7 @@ func (o *ProjectOptions) ToProject(services []string, po ...cli.ProjectOptionsFn
180193
return nil, compose.WrapComposeError(err)
181194
}
182195

183-
if o.Compatibility || utils.StringToBool(options.Environment["COMPOSE_COMPATIBILITY"]) {
196+
if o.Compatibility || utils.StringToBool(options.Environment[ComposeCompatibility]) {
184197
api.Separator = "_"
185198
}
186199

@@ -339,10 +352,22 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
339352
opts.EnvFiles[i] = file
340353
}
341354
}
342-
if v, ok := os.LookupEnv("COMPOSE_PARALLEL_LIMIT"); ok && !cmd.Flags().Changed("parallel") {
355+
356+
composeCmd := cmd
357+
for {
358+
if composeCmd.Name() == PluginName {
359+
break
360+
}
361+
if !composeCmd.HasParent() {
362+
return fmt.Errorf("error parsing command line, expected %q", PluginName)
363+
}
364+
composeCmd = composeCmd.Parent()
365+
}
366+
367+
if v, ok := os.LookupEnv(ComposeParallelLimit); ok && !composeCmd.Flags().Changed("parallel") {
343368
i, err := strconv.Atoi(v)
344369
if err != nil {
345-
return fmt.Errorf("COMPOSE_PARALLEL_LIMIT must be an integer (found: %q)", v)
370+
return fmt.Errorf("%s must be an integer (found: %q)", ComposeParallelLimit, v)
346371
}
347372
parallel = i
348373
}

cmd/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func downCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
6262
ValidArgsFunction: noCompletion(),
6363
}
6464
flags := downCmd.Flags()
65-
removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS"))
65+
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
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/kill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func killCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
4646
}
4747

4848
flags := cmd.Flags()
49-
removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS"))
49+
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
5050
flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
5151
flags.StringVarP(&opts.signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")
5252

cmd/compose/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ func runCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *co
146146
if err != nil {
147147
return err
148148
}
149-
opts.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])
149+
150+
opts.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
150151
return runRun(ctx, backend, project, opts, createOpts, streams)
151152
}),
152153
ValidArgsFunction: completeServiceNames(p),

cmd/compose/up.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
8282
return validateFlags(&up, &create)
8383
}),
8484
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
85-
create.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])
85+
create.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
8686
if create.ignoreOrphans && create.removeOrphans {
87-
return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined")
87+
return fmt.Errorf("%s and --remove-orphans cannot be combined", ComposeIgnoreOrphans)
8888
}
8989
return runUp(ctx, streams, backend, create, up, project, services)
9090
}),

0 commit comments

Comments
 (0)