Merge pull request #1691 from docker/pre_run

validate and pre-process flags in PreRun
This commit is contained in:
Nicolas De loof 2021-05-20 18:50:50 +02:00 committed by GitHub
commit ed11511d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 32 deletions

View File

@ -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)
}),
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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,

View File

@ -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)
}),
}

View File

@ -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)
}),
}

View File

@ -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,
})

View File

@ -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)
}),
}

View File

@ -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)
}),
}

View File

@ -149,24 +149,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)