浏览代码

support --entrypoint="" as override

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 年之前
父节点
当前提交
2ceb176500
共有 3 个文件被更改,包括 39 次插入30 次删除
  1. 12 3
      cmd/compose/compose.go
  2. 26 26
      cmd/compose/run.go
  3. 1 1
      pkg/compose/run.go

+ 12 - 3
cmd/compose/compose.go

@@ -44,8 +44,10 @@ import (
 // Command defines a compose CLI command as a func with args
 type Command func(context.Context, []string) error
 
-// Adapt a Command func to cobra library
-func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
+type CobraCommand func(context.Context, *cobra.Command, []string) error
+
+// AdaptCmd adapt a CobraCommand func to cobra library
+func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
 	return func(cmd *cobra.Command, args []string) error {
 		ctx := cmd.Context()
 		contextString := fmt.Sprintf("%s", ctx)
@@ -59,7 +61,7 @@ func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
 				cancel()
 			}()
 		}
-		err := fn(ctx, args)
+		err := fn(ctx, cmd, args)
 		var composeErr compose.Error
 		if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {
 			err = dockercli.StatusError{
@@ -77,6 +79,13 @@ func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
 	}
 }
 
+// Adapt a Command func to cobra library
+func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
+	return AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
+		return fn(ctx, args)
+	})
+}
+
 // Warning is a global warning to be displayed to user on command failure
 var Warning string
 

+ 26 - 26
cmd/compose/run.go

@@ -36,22 +36,23 @@ import (
 
 type runOptions struct {
 	*composeOptions
-	Service      string
-	Command      []string
-	environment  []string
-	Detach       bool
-	Remove       bool
-	noTty        bool
-	user         string
-	workdir      string
-	entrypoint   string
-	labels       []string
-	volumes      []string
-	publish      []string
-	useAliases   bool
-	servicePorts bool
-	name         string
-	noDeps       bool
+	Service       string
+	Command       []string
+	environment   []string
+	Detach        bool
+	Remove        bool
+	noTty         bool
+	user          string
+	workdir       string
+	entrypoint    string
+	entrypointCmd []string
+	labels        []string
+	volumes       []string
+	publish       []string
+	useAliases    bool
+	servicePorts  bool
+	name          string
+	noDeps        bool
 }
 
 func (opts runOptions) apply(project *types.Project) error {
@@ -110,7 +111,7 @@ func runCommand(p *projectOptions, backend api.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),
-		PreRunE: Adapt(func(ctx context.Context, args []string) error {
+		PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
 			opts.Service = args[0]
 			if len(args) > 1 {
 				opts.Command = args[1:]
@@ -118,6 +119,13 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
 			if len(opts.publish) > 0 && opts.servicePorts {
 				return fmt.Errorf("--service-ports and --publish are incompatible")
 			}
+			if cmd.Flags().Changed("entrypoint") {
+				command, err := shellwords.Parse(opts.entrypoint)
+				if err != nil {
+					return err
+				}
+				opts.entrypointCmd = command
+			}
 			return nil
 		}),
 		RunE: Adapt(func(ctx context.Context, args []string) error {
@@ -166,14 +174,6 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
 		return err
 	}
 
-	var entrypoint []string
-	if opts.entrypoint != "" {
-		entrypoint, err = shellwords.Parse(opts.entrypoint)
-		if err != nil {
-			return err
-		}
-	}
-
 	labels := types.Labels{}
 	for _, s := range opts.labels {
 		parts := strings.SplitN(s, "=", 2)
@@ -197,7 +197,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
 		WorkingDir:        opts.workdir,
 		User:              opts.user,
 		Environment:       opts.environment,
-		Entrypoint:        entrypoint,
+		Entrypoint:        opts.entrypointCmd,
 		Labels:            labels,
 		UseNetworkAliases: opts.useAliases,
 		Index:             0,

+ 1 - 1
pkg/compose/run.go

@@ -196,7 +196,7 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts
 	if len(opts.WorkingDir) > 0 {
 		service.WorkingDir = opts.WorkingDir
 	}
-	if len(opts.Entrypoint) > 0 {
+	if opts.Entrypoint != nil {
 		service.Entrypoint = opts.Entrypoint
 	}
 	if len(opts.Environment) > 0 {