version.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/cli/cmd/mobyflags"
  19. "github.com/docker/compose-cli/cli/mobycli"
  20. )
  21. // VersionCommand command to display version
  22. func VersionCommand(version string) *cobra.Command {
  23. cmd := &cobra.Command{
  24. Use: "version",
  25. Short: "Show the Docker version information",
  26. Args: cobra.MaximumNArgs(0),
  27. RunE: func(cmd *cobra.Command, _ []string) error {
  28. return runVersion(cmd, version)
  29. },
  30. }
  31. // define flags for backward compatibility with com.docker.cli
  32. flags := cmd.Flags()
  33. flags.StringP("format", "f", "", "Format the output using the given Go template")
  34. flags.String("kubeconfig", "", "Kubernetes config file")
  35. mobyflags.AddMobyFlagsForRetrocompatibility(flags)
  36. return cmd
  37. }
  38. func runVersion(cmd *cobra.Command, version string) error {
  39. displayedVersion := strings.TrimPrefix(version, "v")
  40. versionResult, _ := mobycli.ExecSilent(cmd.Context())
  41. // we don't want to fail on error, there is an error if the engine is not available but it displays client version info
  42. // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
  43. if versionResult == nil {
  44. mobycli.Exec(cmd.Root())
  45. return nil
  46. }
  47. var s string = string(versionResult)
  48. fmt.Print(strings.Replace(s, "\n Version:", "\n Cloud integration "+displayedVersion+"\n Version:", 1))
  49. return nil
  50. }