From 8b9ed85928475a479264509445e7cd6581891c85 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 19 May 2021 18:49:33 +0200 Subject: [PATCH] validate and pre-process flags in PreRun Signed-off-by: Nicolas De Loof --- cli/cmd/compose/build.go | 5 ++++- cli/cmd/compose/convert.go | 5 ++++- cli/cmd/compose/cp.go | 6 ++++-- cli/cmd/compose/create.go | 5 ++++- cli/cmd/compose/down.go | 5 ++++- cli/cmd/compose/exec.go | 9 +++++---- cli/cmd/compose/port.go | 13 +++++++++---- cli/cmd/compose/pull.go | 5 ++++- cli/cmd/compose/run.go | 7 +++++-- cli/cmd/compose/up.go | 33 ++++++++++++++++++--------------- 10 files changed, 61 insertions(+), 32 deletions(-) diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index febaf4d37..c39421360 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -46,7 +46,7 @@ func buildCommand(p *projectOptions, backend compose.Service) *cobra.Command { cmd := &cobra.Command{ Use: "build [SERVICE...]", Short: "Build or rebuild services", - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { if opts.memory != "" { fmt.Println("WARNING --memory is ignored as not supported in buildkit.") } @@ -57,6 +57,9 @@ func buildCommand(p *projectOptions, backend compose.Service) *cobra.Command { } os.Stdout = devnull } + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { return runBuild(ctx, backend, opts, args) }), } diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go index 359bef47b..7c8597520 100644 --- a/cli/cmd/compose/convert.go +++ b/cli/cmd/compose/convert.go @@ -58,7 +58,7 @@ func convertCommand(p *projectOptions, backend compose.Service) *cobra.Command { Aliases: []string{"config"}, Use: "convert SERVICES", Short: "Converts the compose file to platform's canonical format", - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { if opts.quiet { devnull, err := os.Open(os.DevNull) if err != nil { @@ -66,6 +66,9 @@ func convertCommand(p *projectOptions, backend compose.Service) *cobra.Command { } os.Stdout = devnull } + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { if opts.services { return runServices(opts) } diff --git a/cli/cmd/compose/cp.go b/cli/cmd/compose/cp.go index 7ed292656..81405cbb5 100644 --- a/cli/cmd/compose/cp.go +++ b/cli/cmd/compose/cp.go @@ -46,14 +46,16 @@ func copyCommand(p *projectOptions, backend compose.Service) *cobra.Command { docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`, Short: "Copy files/folders between a service container and the local filesystem", Args: cli.ExactArgs(2), - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { if args[0] == "" { return errors.New("source can not be empty") } if args[1] == "" { return errors.New("destination can not be empty") } - + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { opts.source = args[0] opts.destination = args[1] return runCopy(ctx, backend, opts) diff --git a/cli/cmd/compose/create.go b/cli/cmd/compose/create.go index 8d790ca23..75c55124b 100644 --- a/cli/cmd/compose/create.go +++ b/cli/cmd/compose/create.go @@ -38,13 +38,16 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command { cmd := &cobra.Command{ Use: "create [SERVICE...]", Short: "Creates containers for a service.", - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { if opts.Build && opts.noBuild { return fmt.Errorf("--build and --no-build are incompatible") } if opts.forceRecreate && opts.noRecreate { return fmt.Errorf("--force-recreate and --no-recreate are incompatible") } + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { return runCreateStart(ctx, backend, upOptions{ composeOptions: &composeOptions{ projectOptions: p, diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index ba6dac25e..3a3d0ba62 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -48,12 +48,15 @@ func downCommand(p *projectOptions, contextType string, backend compose.Service) PreRun: func(cmd *cobra.Command, args []string) { opts.timeChanged = cmd.Flags().Changed("timeout") }, - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { if opts.images != "" { if opts.images != "all" && opts.images != "local" { return fmt.Errorf("invalid value for --rmi: %q", opts.images) } } + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { return runDown(ctx, backend, opts) }), } diff --git a/cli/cmd/compose/exec.go b/cli/cmd/compose/exec.go index d583e0e5f..97b7349f4 100644 --- a/cli/cmd/compose/exec.go +++ b/cli/cmd/compose/exec.go @@ -53,11 +53,12 @@ func execCommand(p *projectOptions, backend compose.Service) *cobra.Command { Use: "exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]", Short: "Execute a command in a running container.", Args: cobra.MinimumNArgs(2), - RunE: Adapt(func(ctx context.Context, args []string) error { - if len(args) > 1 { - opts.command = args[1:] - } + PreRunE: Adapt(func(ctx context.Context, args []string) error { opts.service = args[0] + opts.command = args[1:] + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { return runExec(ctx, backend, opts) }), } diff --git a/cli/cmd/compose/port.go b/cli/cmd/compose/port.go index 1d00b9c15..497c353b6 100644 --- a/cli/cmd/compose/port.go +++ b/cli/cmd/compose/port.go @@ -28,6 +28,7 @@ import ( type portOptions struct { *projectOptions + port int protocol string index int } @@ -40,12 +41,16 @@ func portCommand(p *projectOptions, backend compose.Service) *cobra.Command { Use: "port [options] [--] SERVICE PRIVATE_PORT", Short: "Print the public port for a port binding.", Args: cobra.MinimumNArgs(2), - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { port, err := strconv.Atoi(args[1]) if err != nil { return err } - return runPort(ctx, backend, opts, args[0], port) + opts.port = port + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { + return runPort(ctx, backend, opts, args[0]) }), } cmd.Flags().StringVar(&opts.protocol, "protocol", "tcp", "tcp or udp") @@ -53,12 +58,12 @@ func portCommand(p *projectOptions, backend compose.Service) *cobra.Command { return cmd } -func runPort(ctx context.Context, backend compose.Service, opts portOptions, service string, port int) error { +func runPort(ctx context.Context, backend compose.Service, opts portOptions, service string) error { projectName, err := opts.toProjectName() if err != nil { return err } - ip, port, err := backend.Port(ctx, projectName, service, port, compose.PortOptions{ + ip, port, err := backend.Port(ctx, projectName, service, opts.port, compose.PortOptions{ Protocol: opts.protocol, Index: opts.index, }) diff --git a/cli/cmd/compose/pull.go b/cli/cmd/compose/pull.go index 8917da744..b944fed55 100644 --- a/cli/cmd/compose/pull.go +++ b/cli/cmd/compose/pull.go @@ -46,10 +46,13 @@ func pullCommand(p *projectOptions, backend compose.Service) *cobra.Command { cmd := &cobra.Command{ Use: "pull [SERVICE...]", Short: "Pull service images", - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { if opts.noParallel { fmt.Fprint(os.Stderr, aec.Apply("option '--no-parallel' is DEPRECATED and will be ignored.\n", aec.RedF)) } + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { return runPull(ctx, backend, opts, args) }), } diff --git a/cli/cmd/compose/run.go b/cli/cmd/compose/run.go index ca2582cc3..4fc16062d 100644 --- a/cli/cmd/compose/run.go +++ b/cli/cmd/compose/run.go @@ -109,14 +109,17 @@ func runCommand(p *projectOptions, backend compose.Service) *cobra.Command { Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]", Short: "Run a one-off command on a service.", Args: cobra.MinimumNArgs(1), - RunE: Adapt(func(ctx context.Context, args []string) error { + PreRunE: Adapt(func(ctx context.Context, args []string) error { + opts.Service = args[0] if len(args) > 1 { opts.Command = args[1:] } - opts.Service = args[0] if len(opts.publish) > 0 && opts.servicePorts { return fmt.Errorf("--service-ports and --publish are incompatible") } + return nil + }), + RunE: Adapt(func(ctx context.Context, args []string) error { return runRun(ctx, backend, opts) }), } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index b7a4b125b..c2b2c8abe 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -151,24 +151,27 @@ func upCommand(p *projectOptions, contextType string, backend compose.Service) * PreRun: func(cmd *cobra.Command, args []string) { opts.timeChanged = cmd.Flags().Changed("timeout") }, + PreRunE: Adapt(func(ctx context.Context, args []string) error { + if opts.exitCodeFrom != "" { + opts.cascadeStop = true + } + if opts.Build && opts.noBuild { + return fmt.Errorf("--build and --no-build are incompatible") + } + if opts.Detach && (opts.attachDependencies || opts.cascadeStop) { + return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies") + } + if opts.forceRecreate && opts.noRecreate { + return fmt.Errorf("--force-recreate and --no-recreate are incompatible") + } + if opts.recreateDeps && opts.noRecreate { + return fmt.Errorf("--always-recreate-deps and --no-recreate are incompatible") + } + return nil + }), RunE: Adapt(func(ctx context.Context, args []string) error { switch contextType { case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType: - if opts.exitCodeFrom != "" { - opts.cascadeStop = true - } - if opts.Build && opts.noBuild { - return fmt.Errorf("--build and --no-build are incompatible") - } - if opts.Detach && (opts.attachDependencies || opts.cascadeStop) { - return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies") - } - if opts.forceRecreate && opts.noRecreate { - return fmt.Errorf("--force-recreate and --no-recreate are incompatible") - } - if opts.recreateDeps && opts.noRecreate { - return fmt.Errorf("--always-recreate-deps and --no-recreate are incompatible") - } return runCreateStart(ctx, backend, opts, args) default: return runUp(ctx, backend, opts, args)