Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit ed11511

Browse files
authored
Merge pull request #1691 from docker/pre_run
validate and pre-process flags in PreRun
2 parents d70b50b + 8b9ed85 commit ed11511

File tree

10 files changed

+61
-32
lines changed

10 files changed

+61
-32
lines changed

cli/cmd/compose/build.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func buildCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4646
cmd := &cobra.Command{
4747
Use: "build [SERVICE...]",
4848
Short: "Build or rebuild services",
49-
RunE: Adapt(func(ctx context.Context, args []string) error {
49+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
5050
if opts.memory != "" {
5151
fmt.Println("WARNING --memory is ignored as not supported in buildkit.")
5252
}
@@ -57,6 +57,9 @@ func buildCommand(p *projectOptions, backend compose.Service) *cobra.Command {
5757
}
5858
os.Stdout = devnull
5959
}
60+
return nil
61+
}),
62+
RunE: Adapt(func(ctx context.Context, args []string) error {
6063
return runBuild(ctx, backend, opts, args)
6164
}),
6265
}

cli/cmd/compose/convert.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ func convertCommand(p *projectOptions, backend compose.Service) *cobra.Command {
5858
Aliases: []string{"config"},
5959
Use: "convert SERVICES",
6060
Short: "Converts the compose file to platform's canonical format",
61-
RunE: Adapt(func(ctx context.Context, args []string) error {
61+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
6262
if opts.quiet {
6363
devnull, err := os.Open(os.DevNull)
6464
if err != nil {
6565
return err
6666
}
6767
os.Stdout = devnull
6868
}
69+
return nil
70+
}),
71+
RunE: Adapt(func(ctx context.Context, args []string) error {
6972
if opts.services {
7073
return runServices(opts)
7174
}

cli/cmd/compose/cp.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ func copyCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4646
docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`,
4747
Short: "Copy files/folders between a service container and the local filesystem",
4848
Args: cli.ExactArgs(2),
49-
RunE: Adapt(func(ctx context.Context, args []string) error {
49+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
5050
if args[0] == "" {
5151
return errors.New("source can not be empty")
5252
}
5353
if args[1] == "" {
5454
return errors.New("destination can not be empty")
5555
}
56-
56+
return nil
57+
}),
58+
RunE: Adapt(func(ctx context.Context, args []string) error {
5759
opts.source = args[0]
5860
opts.destination = args[1]
5961
return runCopy(ctx, backend, opts)

cli/cmd/compose/create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
3838
cmd := &cobra.Command{
3939
Use: "create [SERVICE...]",
4040
Short: "Creates containers for a service.",
41-
RunE: Adapt(func(ctx context.Context, args []string) error {
41+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
4242
if opts.Build && opts.noBuild {
4343
return fmt.Errorf("--build and --no-build are incompatible")
4444
}
4545
if opts.forceRecreate && opts.noRecreate {
4646
return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
4747
}
48+
return nil
49+
}),
50+
RunE: Adapt(func(ctx context.Context, args []string) error {
4851
return runCreateStart(ctx, backend, upOptions{
4952
composeOptions: &composeOptions{
5053
projectOptions: p,

cli/cmd/compose/down.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ func downCommand(p *projectOptions, contextType string, backend compose.Service)
4848
PreRun: func(cmd *cobra.Command, args []string) {
4949
opts.timeChanged = cmd.Flags().Changed("timeout")
5050
},
51-
RunE: Adapt(func(ctx context.Context, args []string) error {
51+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
5252
if opts.images != "" {
5353
if opts.images != "all" && opts.images != "local" {
5454
return fmt.Errorf("invalid value for --rmi: %q", opts.images)
5555
}
5656
}
57+
return nil
58+
}),
59+
RunE: Adapt(func(ctx context.Context, args []string) error {
5760
return runDown(ctx, backend, opts)
5861
}),
5962
}

cli/cmd/compose/exec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ func execCommand(p *projectOptions, backend compose.Service) *cobra.Command {
5353
Use: "exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]",
5454
Short: "Execute a command in a running container.",
5555
Args: cobra.MinimumNArgs(2),
56-
RunE: Adapt(func(ctx context.Context, args []string) error {
57-
if len(args) > 1 {
58-
opts.command = args[1:]
59-
}
56+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
6057
opts.service = args[0]
58+
opts.command = args[1:]
59+
return nil
60+
}),
61+
RunE: Adapt(func(ctx context.Context, args []string) error {
6162
return runExec(ctx, backend, opts)
6263
}),
6364
}

cli/cmd/compose/port.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
type portOptions struct {
3030
*projectOptions
31+
port int
3132
protocol string
3233
index int
3334
}
@@ -40,25 +41,29 @@ func portCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4041
Use: "port [options] [--] SERVICE PRIVATE_PORT",
4142
Short: "Print the public port for a port binding.",
4243
Args: cobra.MinimumNArgs(2),
43-
RunE: Adapt(func(ctx context.Context, args []string) error {
44+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
4445
port, err := strconv.Atoi(args[1])
4546
if err != nil {
4647
return err
4748
}
48-
return runPort(ctx, backend, opts, args[0], port)
49+
opts.port = port
50+
return nil
51+
}),
52+
RunE: Adapt(func(ctx context.Context, args []string) error {
53+
return runPort(ctx, backend, opts, args[0])
4954
}),
5055
}
5156
cmd.Flags().StringVar(&opts.protocol, "protocol", "tcp", "tcp or udp")
5257
cmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if service has multiple replicas")
5358
return cmd
5459
}
5560

56-
func runPort(ctx context.Context, backend compose.Service, opts portOptions, service string, port int) error {
61+
func runPort(ctx context.Context, backend compose.Service, opts portOptions, service string) error {
5762
projectName, err := opts.toProjectName()
5863
if err != nil {
5964
return err
6065
}
61-
ip, port, err := backend.Port(ctx, projectName, service, port, compose.PortOptions{
66+
ip, port, err := backend.Port(ctx, projectName, service, opts.port, compose.PortOptions{
6267
Protocol: opts.protocol,
6368
Index: opts.index,
6469
})

cli/cmd/compose/pull.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ func pullCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4646
cmd := &cobra.Command{
4747
Use: "pull [SERVICE...]",
4848
Short: "Pull service images",
49-
RunE: Adapt(func(ctx context.Context, args []string) error {
49+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
5050
if opts.noParallel {
5151
fmt.Fprint(os.Stderr, aec.Apply("option '--no-parallel' is DEPRECATED and will be ignored.\n", aec.RedF))
5252
}
53+
return nil
54+
}),
55+
RunE: Adapt(func(ctx context.Context, args []string) error {
5356
return runPull(ctx, backend, opts, args)
5457
}),
5558
}

cli/cmd/compose/run.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,17 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command {
109109
Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]",
110110
Short: "Run a one-off command on a service.",
111111
Args: cobra.MinimumNArgs(1),
112-
RunE: Adapt(func(ctx context.Context, args []string) error {
112+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
113+
opts.Service = args[0]
113114
if len(args) > 1 {
114115
opts.Command = args[1:]
115116
}
116-
opts.Service = args[0]
117117
if len(opts.publish) > 0 && opts.servicePorts {
118118
return fmt.Errorf("--service-ports and --publish are incompatible")
119119
}
120+
return nil
121+
}),
122+
RunE: Adapt(func(ctx context.Context, args []string) error {
120123
return runRun(ctx, backend, opts)
121124
}),
122125
}

cli/cmd/compose/up.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,27 @@ func upCommand(p *projectOptions, contextType string, backend compose.Service) *
149149
PreRun: func(cmd *cobra.Command, args []string) {
150150
opts.timeChanged = cmd.Flags().Changed("timeout")
151151
},
152+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
153+
if opts.exitCodeFrom != "" {
154+
opts.cascadeStop = true
155+
}
156+
if opts.Build && opts.noBuild {
157+
return fmt.Errorf("--build and --no-build are incompatible")
158+
}
159+
if opts.Detach && (opts.attachDependencies || opts.cascadeStop) {
160+
return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies")
161+
}
162+
if opts.forceRecreate && opts.noRecreate {
163+
return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
164+
}
165+
if opts.recreateDeps && opts.noRecreate {
166+
return fmt.Errorf("--always-recreate-deps and --no-recreate are incompatible")
167+
}
168+
return nil
169+
}),
152170
RunE: Adapt(func(ctx context.Context, args []string) error {
153171
switch contextType {
154172
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
155-
if opts.exitCodeFrom != "" {
156-
opts.cascadeStop = true
157-
}
158-
if opts.Build && opts.noBuild {
159-
return fmt.Errorf("--build and --no-build are incompatible")
160-
}
161-
if opts.Detach && (opts.attachDependencies || opts.cascadeStop) {
162-
return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies")
163-
}
164-
if opts.forceRecreate && opts.noRecreate {
165-
return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
166-
}
167-
if opts.recreateDeps && opts.noRecreate {
168-
return fmt.Errorf("--always-recreate-deps and --no-recreate are incompatible")
169-
}
170173
return runCreateStart(ctx, backend, opts, args)
171174
default:
172175
return runUp(ctx, backend, opts, args)

0 commit comments

Comments
 (0)