support --entrypoint="" as override

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-08 09:07:18 +02:00 committed by Nicolas De loof
parent 1458beea84
commit 2ceb176500
3 changed files with 39 additions and 30 deletions

View File

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

View File

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

View File

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