diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 4acabd85..f76ae7ac 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -25,15 +25,14 @@ import ( "strings" "syscall" - "github.com/docker/compose/v2/cmd/formatter" - - "github.com/sirupsen/logrus" - "github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/types" dockercli "github.com/docker/cli/cli" + "github.com/docker/cli/cli-plugins/manager" + "github.com/docker/compose/v2/cmd/formatter" "github.com/morikuni/aec" "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -106,7 +105,7 @@ type ProjectFunc func(ctx context.Context, project *types.Project) error // ProjectServicesFunc does stuff within a types.Project and a selection of services type ProjectServicesFunc func(ctx context.Context, project *types.Project, services []string) error -// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services +// WithProject creates a cobra run command from a ProjectFunc based on configured project options and selected services func (o *projectOptions) WithProject(fn ProjectFunc) func(cmd *cobra.Command, args []string) error { return o.WithServices(func(ctx context.Context, project *types.Project, services []string) error { return fn(ctx, project) @@ -209,6 +208,13 @@ func (o *projectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.Proj cli.WithName(o.ProjectName))...) } +const pluginName = "compose" + +// RunningAsStandalone detects when running as a standalone program +func RunningAsStandalone() bool { + return len(os.Args) < 2 || os.Args[1] != manager.MetadataSubcommandName && os.Args[1] != pluginName +} + // RootCommand returns the compose command with its child commands func RootCommand(backend api.Service) *cobra.Command { opts := projectOptions{} @@ -219,7 +225,7 @@ func RootCommand(backend api.Service) *cobra.Command { ) command := &cobra.Command{ Short: "Docker Compose", - Use: "compose", + Use: pluginName, TraverseChildren: true, // By default (no Run/RunE in parent command) for typos in subcommands, cobra displays the help of parent command but exit(0) ! RunE: func(cmd *cobra.Command, args []string) error { @@ -234,11 +240,13 @@ func RootCommand(backend api.Service) *cobra.Command { }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { parent := cmd.Root() - parentPrerun := parent.PersistentPreRunE - if parentPrerun != nil { - err := parentPrerun(cmd, args) - if err != nil { - return err + if parent != nil { + parentPrerun := parent.PersistentPreRunE + if parentPrerun != nil { + err := parentPrerun(cmd, args) + if err != nil { + return err + } } } if noAnsi { diff --git a/cmd/main.go b/cmd/main.go index 00756fed..16ca5aaf 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,10 +17,13 @@ package main import ( + "os" + dockercli "github.com/docker/cli/cli" "github.com/docker/cli/cli-plugins/manager" "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" + "github.com/docker/compose-switch/redirect" "github.com/spf13/cobra" commands "github.com/docker/compose/v2/cmd/compose" @@ -34,7 +37,7 @@ func init() { "To provide feedback or request new features please open issues at https://github.com/docker/compose" } -func main() { +func pluginMain() { plugin.Run(func(dockerCli command.Cli) *cobra.Command { lazyInit := api.NewServiceProxy() cmd := commands.RootCommand(lazyInit) @@ -63,3 +66,10 @@ func main() { Version: internal.Version, }) } + +func main() { + if commands.RunningAsStandalone() { + os.Args = append([]string{"docker"}, redirect.Convert(os.Args[1:])...) + } + pluginMain() +} diff --git a/go.mod b/go.mod index 1dce6a2b..15a908a5 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/docker/buildx v0.5.2-0.20210422185057-908a856079fc github.com/docker/cli v20.10.7+incompatible github.com/docker/cli-docs-tool v0.1.1 + github.com/docker/compose-switch v1.0.2 github.com/docker/docker v20.10.7+incompatible github.com/docker/go-connections v0.4.0 github.com/docker/go-units v0.4.0 diff --git a/go.sum b/go.sum index 23133249..6f8da62e 100644 --- a/go.sum +++ b/go.sum @@ -351,6 +351,8 @@ github.com/docker/cli v20.10.7+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHv github.com/docker/cli-docs-tool v0.1.1 h1:c6vuTMvogCkSFQCXIr6Mb4gFgUpdZ+28YMbCBfaQLik= github.com/docker/cli-docs-tool v0.1.1/go.mod h1:oMzPNt1wC3TcxuY22GMnOODNOxkwGH51gV3AhqAjFQ4= github.com/docker/compose-on-kubernetes v0.4.19-0.20190128150448-356b2919c496/go.mod h1:iT2pYfi580XlpaV4KmK0T6+4/9+XoKmk/fhoDod1emE= +github.com/docker/compose-switch v1.0.2 h1:chXFNNcnRvmtQYzwTaVsv/KSLRt8riSRAiSav89mLfk= +github.com/docker/compose-switch v1.0.2/go.mod h1:uyPj8S3oH1O9rSZ5QVozw28OIjdNIflSSYElC2P0plQ= github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY= github.com/docker/distribution v2.6.0-rc.1.0.20180327202408-83389a148052+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/distribution v2.7.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= @@ -960,6 +962,7 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3 github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI= +github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw= github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 16bb53ae..7c31218c 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -32,6 +32,7 @@ import ( "github.com/sanathkr/go-yaml" ) +// Separator is used for naming components var Separator = "-" // NewComposeService create a local implementation of the compose.Service API