version.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Values: [pretty | json]. (Default: pretty)")
  37. flags.String("kubeconfig", "", "Kubernetes config file")
  38. mobyflags.AddMobyFlagsForRetrocompatibility(flags)
  39. return cmd
  40. }
  41. func runVersion(cmd *cobra.Command, version string) {
  42. var versionString string
  43. format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.String(), " ", ""))
  44. displayedVersion := strings.TrimPrefix(version, "v")
  45. // Replace is preferred in this case to keep the order.
  46. switch format {
  47. case formatter.PRETTY, "":
  48. versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
  49. "\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
  50. case formatter.JSON, "{{json.}}": // Try to catch full JSON formats
  51. versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
  52. `"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
  53. }
  54. fmt.Print(versionString)
  55. }
  56. func getOutFromMoby(cmd *cobra.Command, args ...string) string {
  57. versionResult, _ := mobycli.ExecSilent(cmd.Context(), args...)
  58. // we don't want to fail on error, there is an error if the engine is not available but it displays client version info
  59. // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
  60. if versionResult == nil {
  61. mobycli.Exec(cmd.Root())
  62. return ""
  63. }
  64. return string(versionResult)
  65. }
  66. func fixedPrettyArgs(oArgs []string) []string {
  67. args := make([]string, 0)
  68. for i := 0; i < len(oArgs); i++ {
  69. if isFormatOpt(oArgs[i]) &&
  70. len(oArgs) > i &&
  71. (strings.ToLower(oArgs[i+1]) == formatter.PRETTY || oArgs[i+1] == "") {
  72. i++
  73. continue
  74. }
  75. args = append(args, oArgs[i])
  76. }
  77. return args
  78. }
  79. func fixedJSONArgs(oArgs []string) []string {
  80. args := make([]string, 0)
  81. for i := 0; i < len(oArgs); i++ {
  82. if isFormatOpt(oArgs[i]) &&
  83. len(oArgs) > i &&
  84. strings.ToLower(oArgs[i+1]) == formatter.JSON {
  85. args = append(args, oArgs[i], "{{json .}}")
  86. i++
  87. continue
  88. }
  89. args = append(args, oArgs[i])
  90. }
  91. return args
  92. }
  93. func isFormatOpt(o string) bool {
  94. return o == "--format" || o == "-f"
  95. }