mirror of
https://github.com/docker/compose.git
synced 2025-07-04 20:34:25 +02:00
Merge pull request #1691 from docker/pre_run
validate and pre-process flags in PreRun
This commit is contained in:
commit
ed11511d29
@ -46,7 +46,7 @@ func buildCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "build [SERVICE...]",
|
Use: "build [SERVICE...]",
|
||||||
Short: "Build or rebuild services",
|
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 != "" {
|
if opts.memory != "" {
|
||||||
fmt.Println("WARNING --memory is ignored as not supported in buildkit.")
|
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
|
os.Stdout = devnull
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||||
return runBuild(ctx, backend, opts, args)
|
return runBuild(ctx, backend, opts, args)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ func convertCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
Aliases: []string{"config"},
|
Aliases: []string{"config"},
|
||||||
Use: "convert SERVICES",
|
Use: "convert SERVICES",
|
||||||
Short: "Converts the compose file to platform's canonical format",
|
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 {
|
if opts.quiet {
|
||||||
devnull, err := os.Open(os.DevNull)
|
devnull, err := os.Open(os.DevNull)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,6 +66,9 @@ func convertCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
}
|
}
|
||||||
os.Stdout = devnull
|
os.Stdout = devnull
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||||
if opts.services {
|
if opts.services {
|
||||||
return runServices(opts)
|
return runServices(opts)
|
||||||
}
|
}
|
||||||
|
@ -46,14 +46,16 @@ func copyCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`,
|
docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`,
|
||||||
Short: "Copy files/folders between a service container and the local filesystem",
|
Short: "Copy files/folders between a service container and the local filesystem",
|
||||||
Args: cli.ExactArgs(2),
|
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] == "" {
|
if args[0] == "" {
|
||||||
return errors.New("source can not be empty")
|
return errors.New("source can not be empty")
|
||||||
}
|
}
|
||||||
if args[1] == "" {
|
if args[1] == "" {
|
||||||
return errors.New("destination can not be empty")
|
return errors.New("destination can not be empty")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||||
opts.source = args[0]
|
opts.source = args[0]
|
||||||
opts.destination = args[1]
|
opts.destination = args[1]
|
||||||
return runCopy(ctx, backend, opts)
|
return runCopy(ctx, backend, opts)
|
||||||
|
@ -38,13 +38,16 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "create [SERVICE...]",
|
Use: "create [SERVICE...]",
|
||||||
Short: "Creates containers for a 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 {
|
if opts.Build && opts.noBuild {
|
||||||
return fmt.Errorf("--build and --no-build are incompatible")
|
return fmt.Errorf("--build and --no-build are incompatible")
|
||||||
}
|
}
|
||||||
if opts.forceRecreate && opts.noRecreate {
|
if opts.forceRecreate && opts.noRecreate {
|
||||||
return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
|
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{
|
return runCreateStart(ctx, backend, upOptions{
|
||||||
composeOptions: &composeOptions{
|
composeOptions: &composeOptions{
|
||||||
projectOptions: p,
|
projectOptions: p,
|
||||||
|
@ -48,12 +48,15 @@ func downCommand(p *projectOptions, contextType string, backend compose.Service)
|
|||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
opts.timeChanged = cmd.Flags().Changed("timeout")
|
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 != "" {
|
||||||
if opts.images != "all" && opts.images != "local" {
|
if opts.images != "all" && opts.images != "local" {
|
||||||
return fmt.Errorf("invalid value for --rmi: %q", opts.images)
|
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)
|
return runDown(ctx, backend, opts)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
@ -53,11 +53,12 @@ func execCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
Use: "exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]",
|
Use: "exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]",
|
||||||
Short: "Execute a command in a running container.",
|
Short: "Execute a command in a running container.",
|
||||||
Args: cobra.MinimumNArgs(2),
|
Args: cobra.MinimumNArgs(2),
|
||||||
RunE: Adapt(func(ctx context.Context, args []string) error {
|
PreRunE: Adapt(func(ctx context.Context, args []string) error {
|
||||||
if len(args) > 1 {
|
|
||||||
opts.command = args[1:]
|
|
||||||
}
|
|
||||||
opts.service = args[0]
|
opts.service = args[0]
|
||||||
|
opts.command = args[1:]
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||||
return runExec(ctx, backend, opts)
|
return runExec(ctx, backend, opts)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
|
|
||||||
type portOptions struct {
|
type portOptions struct {
|
||||||
*projectOptions
|
*projectOptions
|
||||||
|
port int
|
||||||
protocol string
|
protocol string
|
||||||
index int
|
index int
|
||||||
}
|
}
|
||||||
@ -40,12 +41,16 @@ func portCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
Use: "port [options] [--] SERVICE PRIVATE_PORT",
|
Use: "port [options] [--] SERVICE PRIVATE_PORT",
|
||||||
Short: "Print the public port for a port binding.",
|
Short: "Print the public port for a port binding.",
|
||||||
Args: cobra.MinimumNArgs(2),
|
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])
|
port, err := strconv.Atoi(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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")
|
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
|
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()
|
projectName, err := opts.toProjectName()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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,
|
Protocol: opts.protocol,
|
||||||
Index: opts.index,
|
Index: opts.index,
|
||||||
})
|
})
|
||||||
|
@ -46,10 +46,13 @@ func pullCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "pull [SERVICE...]",
|
Use: "pull [SERVICE...]",
|
||||||
Short: "Pull service images",
|
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 {
|
if opts.noParallel {
|
||||||
fmt.Fprint(os.Stderr, aec.Apply("option '--no-parallel' is DEPRECATED and will be ignored.\n", aec.RedF))
|
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)
|
return runPull(ctx, backend, opts, args)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
@ -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...]",
|
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.",
|
Short: "Run a one-off command on a service.",
|
||||||
Args: cobra.MinimumNArgs(1),
|
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 {
|
if len(args) > 1 {
|
||||||
opts.Command = args[1:]
|
opts.Command = args[1:]
|
||||||
}
|
}
|
||||||
opts.Service = args[0]
|
|
||||||
if len(opts.publish) > 0 && opts.servicePorts {
|
if len(opts.publish) > 0 && opts.servicePorts {
|
||||||
return fmt.Errorf("--service-ports and --publish are incompatible")
|
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)
|
return runRun(ctx, backend, opts)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
@ -149,24 +149,27 @@ func upCommand(p *projectOptions, contextType string, backend compose.Service) *
|
|||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
opts.timeChanged = cmd.Flags().Changed("timeout")
|
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 {
|
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||||
switch contextType {
|
switch contextType {
|
||||||
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
|
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)
|
return runCreateStart(ctx, backend, opts, args)
|
||||||
default:
|
default:
|
||||||
return runUp(ctx, backend, opts, args)
|
return runUp(ctx, backend, opts, args)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user