Skip to content

Commit

Permalink
parallel flag belong do top-level "compose" cobra command, not the …
Browse files Browse the repository at this point in the history
…current one

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed May 15, 2023
1 parent 18a112e commit fd7847f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
35 changes: 30 additions & 5 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ import (
"github.com/docker/compose/v2/pkg/utils"
)

const (
// ComposeParallelLimit set the limit running concurrent operation on docker engine
ComposeParallelLimit = "COMPOSE_PARALLEL_LIMIT"
// ComposeProjectName define the project name to be used, instead of guessing from parent directory
ComposeProjectName = "COMPOSE_PROJECT_NAME"
// ComposeCompatibility try to mimic compose v1 as much as possible
ComposeCompatibility = "COMPOSE_COMPATIBILITY"
// ComposeRemoveOrphans remove “orphaned" containers, i.e. containers tagged for current project but not declared as service
ComposeRemoveOrphans = "COMPOSE_REMOVE_ORPHANS"
// ComposeIgnoreOrphans ignore "orphaned" containers
ComposeIgnoreOrphans = "COMPOSE_IGNORE_ORPHANS"
)

// Command defines a compose CLI command as a func with args
type Command func(context.Context, []string) error

Expand Down Expand Up @@ -145,7 +158,7 @@ func (o *ProjectOptions) projectOrName(services ...string) (*types.Project, stri
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
p, err := o.ToProject(services, cli.WithDiscardEnvFile)
if err != nil {
envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
envProjectName := os.Getenv(ComposeProjectName)
if envProjectName != "" {
return nil, envProjectName, nil
}
Expand All @@ -162,7 +175,7 @@ func (o *ProjectOptions) toProjectName() (string, error) {
return o.ProjectName, nil
}

envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
envProjectName := os.Getenv("ComposeProjectName")
if envProjectName != "" {
return envProjectName, nil
}
Expand All @@ -180,7 +193,7 @@ func (o *ProjectOptions) ToProject(services []string, po ...cli.ProjectOptionsFn
return nil, compose.WrapComposeError(err)
}

if o.Compatibility || utils.StringToBool(options.Environment["COMPOSE_COMPATIBILITY"]) {
if o.Compatibility || utils.StringToBool(options.Environment[ComposeCompatibility]) {
api.Separator = "_"
}

Expand Down Expand Up @@ -339,10 +352,22 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
opts.EnvFiles[i] = file
}
}
if v, ok := os.LookupEnv("COMPOSE_PARALLEL_LIMIT"); ok && !cmd.Flags().Changed("parallel") {

composeCmd := cmd
for {
if composeCmd.Name() == PluginName {
break
}
if !composeCmd.HasParent() {
return fmt.Errorf("error parsing command line, expected %q", PluginName)
}
composeCmd = composeCmd.Parent()
}

if v, ok := os.LookupEnv(ComposeParallelLimit); ok && !composeCmd.Flags().Changed("parallel") {
i, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("COMPOSE_PARALLEL_LIMIT must be an integer (found: %q)", v)
return fmt.Errorf("%s must be an integer (found: %q)", ComposeParallelLimit, v)
}
parallel = i
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func downCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
ValidArgsFunction: noCompletion(),
}
flags := downCmd.Flags()
removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS"))
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
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.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/compose/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func killCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
}

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

Expand Down
3 changes: 2 additions & 1 deletion cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func runCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *co
if err != nil {
return err
}
opts.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])

opts.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
return runRun(ctx, backend, project, opts, createOpts, streams)
}),
ValidArgsFunction: completeServiceNames(p),
Expand Down
4 changes: 2 additions & 2 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
return validateFlags(&up, &create)
}),
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
create.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])
create.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
if create.ignoreOrphans && create.removeOrphans {
return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined")
return fmt.Errorf("%s and --remove-orphans cannot be combined", ComposeIgnoreOrphans)
}
return runUp(ctx, streams, backend, create, up, project, services)
}),
Expand Down

0 comments on commit fd7847f

Please sign in to comment.