2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-06-12 17:54:14 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-28 17:08:27 +02:00
|
|
|
"os"
|
2020-06-12 17:54:14 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-04-07 11:39:31 +02:00
|
|
|
"github.com/docker/cli/cli"
|
2021-01-15 16:45:27 +01:00
|
|
|
"github.com/docker/compose-cli/cli/formatter"
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/cli/mobycli"
|
2020-10-07 15:33:27 +02:00
|
|
|
"github.com/docker/compose-cli/internal"
|
2020-06-12 17:54:14 +02:00
|
|
|
)
|
|
|
|
|
2020-09-28 17:08:27 +02:00
|
|
|
const formatOpt = "format"
|
|
|
|
|
2020-06-12 17:54:14 +02:00
|
|
|
// VersionCommand command to display version
|
2020-10-07 15:33:27 +02:00
|
|
|
func VersionCommand() *cobra.Command {
|
2020-06-12 17:54:14 +02:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Show the Docker version information",
|
|
|
|
Args: cobra.MaximumNArgs(0),
|
2021-03-05 17:45:04 +01:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
err := runVersion(cmd)
|
|
|
|
if err != nil {
|
2021-04-07 11:39:31 +02:00
|
|
|
return cli.StatusError{StatusCode: 1, Status: err.Error()}
|
2021-03-05 17:45:04 +01:00
|
|
|
}
|
|
|
|
return nil
|
2020-06-23 17:14:59 +02:00
|
|
|
},
|
2020-06-12 17:54:14 +02:00
|
|
|
}
|
2020-06-17 17:57:44 +02:00
|
|
|
// define flags for backward compatibility with com.docker.cli
|
2020-06-12 17:54:14 +02:00
|
|
|
flags := cmd.Flags()
|
2020-09-30 11:25:08 +02:00
|
|
|
flags.StringP(formatOpt, "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
|
2020-06-12 17:54:14 +02:00
|
|
|
flags.String("kubeconfig", "", "Kubernetes config file")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2021-03-05 17:45:04 +01:00
|
|
|
func runVersion(cmd *cobra.Command) error {
|
2020-09-28 17:08:27 +02:00
|
|
|
var versionString string
|
2021-03-05 17:45:04 +01:00
|
|
|
var err error
|
2020-09-30 10:32:26 +02:00
|
|
|
format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.String(), " ", ""))
|
2020-09-28 17:08:27 +02:00
|
|
|
// Replace is preferred in this case to keep the order.
|
|
|
|
switch format {
|
|
|
|
case formatter.PRETTY, "":
|
2021-03-05 17:45:04 +01:00
|
|
|
versionString, err = getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...)
|
|
|
|
versionString = strings.Replace(versionString,
|
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
|
|
|
"\n Version:", "\n Cloud integration: "+internal.Version+"\n Version:", 1)
|
2020-10-30 10:34:18 +01:00
|
|
|
case formatter.JSON, formatter.TemplateLegacyJSON: // Try to catch full JSON formats
|
2021-03-05 17:45:04 +01:00
|
|
|
versionString, err = getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...)
|
|
|
|
versionString = strings.Replace(versionString,
|
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":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, internal.Version), 1)
|
2020-10-02 19:04:43 +02:00
|
|
|
default:
|
2021-03-05 17:45:04 +01:00
|
|
|
versionString, err = getOutFromMoby(cmd)
|
2020-09-28 17:08:27 +02:00
|
|
|
}
|
2020-10-02 19:04:43 +02:00
|
|
|
|
2020-09-28 17:08:27 +02:00
|
|
|
fmt.Print(versionString)
|
2021-03-05 17:45:04 +01:00
|
|
|
return err
|
2020-09-28 17:08:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 17:45:04 +01:00
|
|
|
func getOutFromMoby(cmd *cobra.Command, args ...string) (string, error) {
|
|
|
|
versionResult, err := mobycli.ExecSilent(cmd.Context(), args...)
|
2020-06-12 17:54:14 +02:00
|
|
|
// we don't want to fail on error, there is an error if the engine is not available but it displays client version info
|
|
|
|
// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
|
|
|
|
if versionResult == nil {
|
2020-09-16 17:22:21 +02:00
|
|
|
mobycli.Exec(cmd.Root())
|
2021-03-05 17:45:04 +01:00
|
|
|
return "", nil
|
2020-09-28 17:08:27 +02:00
|
|
|
}
|
2021-03-05 17:45:04 +01:00
|
|
|
return string(versionResult), err
|
2020-09-28 17:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func fixedPrettyArgs(oArgs []string) []string {
|
2020-09-30 10:32:26 +02:00
|
|
|
args := make([]string, 0)
|
2020-09-28 17:08:27 +02:00
|
|
|
for i := 0; i < len(oArgs); i++ {
|
|
|
|
if isFormatOpt(oArgs[i]) &&
|
|
|
|
len(oArgs) > i &&
|
|
|
|
(strings.ToLower(oArgs[i+1]) == formatter.PRETTY || oArgs[i+1] == "") {
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, oArgs[i])
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixedJSONArgs(oArgs []string) []string {
|
2020-09-30 10:32:26 +02:00
|
|
|
args := make([]string, 0)
|
2020-09-28 17:08:27 +02:00
|
|
|
for i := 0; i < len(oArgs); i++ {
|
|
|
|
if isFormatOpt(oArgs[i]) &&
|
|
|
|
len(oArgs) > i &&
|
|
|
|
strings.ToLower(oArgs[i+1]) == formatter.JSON {
|
|
|
|
args = append(args, oArgs[i], "{{json .}}")
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, oArgs[i])
|
2020-06-12 17:54:14 +02:00
|
|
|
}
|
2020-09-28 17:08:27 +02:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFormatOpt(o string) bool {
|
|
|
|
return o == "--format" || o == "-f"
|
2020-06-12 17:54:14 +02:00
|
|
|
}
|