version.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. "os"
  17. "strings"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/cli/cmd/mobyflags"
  20. "github.com/docker/compose-cli/cli/mobycli"
  21. "github.com/docker/compose-cli/formatter"
  22. )
  23. const formatOpt = "format"
  24. // VersionCommand command to display version
  25. func VersionCommand(version string) *cobra.Command {
  26. cmd := &cobra.Command{
  27. Use: "version",
  28. Short: "Show the Docker version information",
  29. Args: cobra.MaximumNArgs(0),
  30. Run: func(cmd *cobra.Command, _ []string) {
  31. runVersion(cmd, version)
  32. },
  33. }
  34. // define flags for backward compatibility with com.docker.cli
  35. flags := cmd.Flags()
  36. flags.StringP(formatOpt, "f", "", "Format the output using the given Go template")
  37. // flags.String(&opts.format, "format", "", "Format the output. Values: [pretty | json | go template]. (Default: pretty)")
  38. flags.String("kubeconfig", "", "Kubernetes config file")
  39. mobyflags.AddMobyFlagsForRetrocompatibility(flags)
  40. return cmd
  41. }
  42. func runVersion(cmd *cobra.Command, version string) {
  43. var versionString string
  44. format := strings.TrimSpace(cmd.Flag(formatOpt).Value.String())
  45. displayedVersion := strings.TrimPrefix(version, "v")
  46. // Replace is preferred in this case to keep the order.
  47. switch format {
  48. case formatter.PRETTY, "":
  49. versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
  50. "\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
  51. case formatter.JSON, "{{json .}}", "{{json . }}", "{{ json .}}", "{{ json . }}": // Try to catch full JSON formats
  52. versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
  53. `"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
  54. }
  55. fmt.Print(versionString)
  56. }
  57. func getOutFromMoby(cmd *cobra.Command, args ...string) string {
  58. versionResult, _ := mobycli.ExecSilent(cmd.Context(), args...)
  59. // we don't want to fail on error, there is an error if the engine is not available but it displays client version info
  60. // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
  61. if versionResult == nil {
  62. mobycli.Exec(cmd.Root())
  63. return ""
  64. }
  65. return string(versionResult)
  66. }
  67. func fixedPrettyArgs(oArgs []string) []string {
  68. var args []string
  69. for i := 0; i < len(oArgs); i++ {
  70. if isFormatOpt(oArgs[i]) &&
  71. len(oArgs) > i &&
  72. (strings.ToLower(oArgs[i+1]) == formatter.PRETTY || oArgs[i+1] == "") {
  73. i++
  74. continue
  75. }
  76. args = append(args, oArgs[i])
  77. }
  78. return args
  79. }
  80. func fixedJSONArgs(oArgs []string) []string {
  81. var args []string
  82. for i := 0; i < len(oArgs); i++ {
  83. if isFormatOpt(oArgs[i]) &&
  84. len(oArgs) > i &&
  85. strings.ToLower(oArgs[i+1]) == formatter.JSON {
  86. args = append(args, oArgs[i], "{{json .}}")
  87. i++
  88. continue
  89. }
  90. args = append(args, oArgs[i])
  91. }
  92. return args
  93. }
  94. func isFormatOpt(o string) bool {
  95. return o == "--format" || o == "-f"
  96. }