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

View File

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

View File

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