2021-03-04 19:21:42 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-29 23:45:11 +02:00
|
|
|
"os"
|
|
|
|
|
2021-04-15 12:43:18 +02:00
|
|
|
dockercli "github.com/docker/cli/cli"
|
2021-03-04 19:21:42 +01:00
|
|
|
"github.com/docker/cli/cli-plugins/manager"
|
|
|
|
"github.com/docker/cli/cli-plugins/plugin"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2023-06-15 18:05:57 +02:00
|
|
|
"github.com/docker/compose/v2/cmd/cmdtrace"
|
2024-01-10 19:30:20 +01:00
|
|
|
"github.com/docker/docker/client"
|
2024-03-12 14:47:41 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-04-15 12:43:18 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-12-08 17:59:48 +01:00
|
|
|
"github.com/docker/compose/v2/cmd/compatibility"
|
2021-08-31 18:53:24 +02:00
|
|
|
commands "github.com/docker/compose/v2/cmd/compose"
|
|
|
|
"github.com/docker/compose/v2/internal"
|
|
|
|
"github.com/docker/compose/v2/pkg/compose"
|
2021-03-04 19:21:42 +01:00
|
|
|
)
|
|
|
|
|
2021-09-29 23:45:11 +02:00
|
|
|
func pluginMain() {
|
2021-03-04 19:21:42 +01:00
|
|
|
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
|
2024-03-20 14:44:27 +01:00
|
|
|
// TODO(milas): this cast is safe but we should not need to do this,
|
|
|
|
// we should expose the concrete service type so that we do not need
|
|
|
|
// to rely on the `api.Service` interface internally
|
|
|
|
backend := compose.NewComposeService(dockerCli).(commands.Backend)
|
2023-12-22 14:02:21 +01:00
|
|
|
cmd := commands.RootCommand(dockerCli, backend)
|
2024-03-12 14:47:41 +01:00
|
|
|
originalPreRunE := cmd.PersistentPreRunE
|
2021-03-04 19:21:42 +01:00
|
|
|
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
2023-06-15 18:05:57 +02:00
|
|
|
// initialize the dockerCli instance
|
2021-03-04 19:21:42 +01:00
|
|
|
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-10 19:30:20 +01:00
|
|
|
// compose-specific initialization
|
|
|
|
dockerCliPostInitialize(dockerCli)
|
|
|
|
|
2024-03-12 14:47:41 +01:00
|
|
|
if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
|
|
|
|
logrus.Debugf("failed to enable tracing: %v", err)
|
|
|
|
}
|
2023-06-08 22:46:07 +02:00
|
|
|
|
2024-03-12 14:47:41 +01:00
|
|
|
if originalPreRunE != nil {
|
|
|
|
return originalPreRunE(cmd, args)
|
2021-03-04 19:21:42 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-08 22:46:07 +02:00
|
|
|
|
2021-04-15 12:43:18 +02:00
|
|
|
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
|
|
|
|
return dockercli.StatusError{
|
2021-06-15 09:57:38 +02:00
|
|
|
StatusCode: compose.CommandSyntaxFailure.ExitCode,
|
2021-04-15 12:43:18 +02:00
|
|
|
Status: err.Error(),
|
|
|
|
}
|
|
|
|
})
|
2021-03-04 19:21:42 +01:00
|
|
|
return cmd
|
|
|
|
},
|
|
|
|
manager.Metadata{
|
|
|
|
SchemaVersion: "0.1.0",
|
|
|
|
Vendor: "Docker Inc.",
|
Do not strip "v" prefix from version when printing
I noticed this when building the binary; `internal.Version` is set to `v2.0.0-beta.4`,
to match the tag.
```bash
GIT_TAG=v2.0.0-beta.4
make COMPOSE_BINARY=bin/docker-compose -f builder.Makefile compose-plugin \
GOOS=linux \
GOARCH=amd64 \
CGO_ENABLED=0 \
go build \
-trimpath \
-ldflags="-s -w -X github.com/docker/compose-cli/internal.Version=v2.0.0-beta.4" \
-o bin/docker-compose \
./cmd
```
However, the binary has the `v` prefix stripped (which caused a check to fail
when packaging):
```bash
/root/rpmbuild/BUILDROOT/docker-compose-plugin-2.0.0.beta.4-0.fc34.x86_64/usr/libexec/docker/cli-plugins/docker-compose docker-cli-plugin-metadata
++ awk '{ gsub(/[",:]/,"")}; $1 == "Version" { print $2 }'
+ ver=2.0.0-beta.4
+ test 2.0.0-beta.4 = v2.0.0-beta.4
FAIL: docker-compose version (2.0.0-beta.4) did not match
```
This also looks inconsistent with other binaries and plugins we ship:
```bash
docker info --format '{{json .ClientInfo.Plugins}}' | jq .
[
{
"SchemaVersion": "0.1.0",
"Vendor": "Docker Inc.",
"Version": "v0.5.1-docker",
"ShortDescription": "Build with BuildKit",
"Name": "buildx",
"Path": "/usr/libexec/docker/cli-plugins/docker-buildx"
},
{
"SchemaVersion": "0.1.0",
"Vendor": "Docker Inc.",
"Version": "2.0.0-beta.4",
"ShortDescription": "Docker Compose",
"Name": "compose",
"Path": "/usr/libexec/docker/cli-plugins/docker-compose"
},
{
"SchemaVersion": "0.1.0",
"Vendor": "Docker Inc.",
"Version": "v0.8.0",
"ShortDescription": "Docker Scan",
"Name": "scan",
"Path": "/usr/libexec/docker/cli-plugins/docker-scan"
}
]
```
Perhaps there was a specific reason for this, but thought I'd open this PR for
discussion (if the v-prefix should not be there, perhaps we should isntead strip
it when setting the version).
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-03 15:50:46 +02:00
|
|
|
Version: internal.Version,
|
2021-03-04 19:21:42 +01:00
|
|
|
})
|
|
|
|
}
|
2021-09-29 23:45:11 +02:00
|
|
|
|
2024-01-10 19:30:20 +01:00
|
|
|
// dockerCliPostInitialize performs Compose-specific configuration for the
|
|
|
|
// command.Cli instance provided by the plugin.Run() initialization.
|
|
|
|
//
|
|
|
|
// NOTE: This must be called AFTER plugin.PersistentPreRunE.
|
|
|
|
func dockerCliPostInitialize(dockerCli command.Cli) {
|
|
|
|
// HACK(milas): remove once docker/cli#4574 is merged; for now,
|
|
|
|
// set it in a rather roundabout way by grabbing the underlying
|
|
|
|
// concrete client and manually invoking an option on it
|
|
|
|
_ = dockerCli.Apply(func(cli *command.DockerCli) error {
|
|
|
|
if mobyClient, ok := cli.Client().(*client.Client); ok {
|
|
|
|
_ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-29 23:45:11 +02:00
|
|
|
func main() {
|
2022-03-09 17:52:30 +01:00
|
|
|
if plugin.RunningStandalone() {
|
2021-12-08 17:59:48 +01:00
|
|
|
os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
|
2021-09-29 23:45:11 +02:00
|
|
|
}
|
|
|
|
pluginMain()
|
|
|
|
}
|