1
0

version.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "github.com/docker/compose-cli/internal"
  23. )
  24. const formatOpt = "format"
  25. // VersionCommand command to display version
  26. func VersionCommand() *cobra.Command {
  27. cmd := &cobra.Command{
  28. Use: "version",
  29. Short: "Show the Docker version information",
  30. Args: cobra.MaximumNArgs(0),
  31. Run: func(cmd *cobra.Command, _ []string) {
  32. runVersion(cmd)
  33. },
  34. }
  35. // define flags for backward compatibility with com.docker.cli
  36. flags := cmd.Flags()
  37. flags.StringP(formatOpt, "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
  38. flags.String("kubeconfig", "", "Kubernetes config file")
  39. mobyflags.AddMobyFlagsForRetrocompatibility(flags)
  40. return cmd
  41. }
  42. func runVersion(cmd *cobra.Command) {
  43. var versionString string
  44. format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.String(), " ", ""))
  45. displayedVersion := strings.TrimPrefix(internal.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, formatter.TemplateJSON: // 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. default:
  55. versionString = getOutFromMoby(cmd)
  56. }
  57. fmt.Print(versionString)
  58. }
  59. func getOutFromMoby(cmd *cobra.Command, args ...string) string {
  60. versionResult, _ := mobycli.ExecSilent(cmd.Context(), args...)
  61. // we don't want to fail on error, there is an error if the engine is not available but it displays client version info
  62. // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
  63. if versionResult == nil {
  64. mobycli.Exec(cmd.Root())
  65. return ""
  66. }
  67. return string(versionResult)
  68. }
  69. func fixedPrettyArgs(oArgs []string) []string {
  70. args := make([]string, 0)
  71. for i := 0; i < len(oArgs); i++ {
  72. if isFormatOpt(oArgs[i]) &&
  73. len(oArgs) > i &&
  74. (strings.ToLower(oArgs[i+1]) == formatter.PRETTY || oArgs[i+1] == "") {
  75. i++
  76. continue
  77. }
  78. args = append(args, oArgs[i])
  79. }
  80. return args
  81. }
  82. func fixedJSONArgs(oArgs []string) []string {
  83. args := make([]string, 0)
  84. for i := 0; i < len(oArgs); i++ {
  85. if isFormatOpt(oArgs[i]) &&
  86. len(oArgs) > i &&
  87. strings.ToLower(oArgs[i+1]) == formatter.JSON {
  88. args = append(args, oArgs[i], "{{json .}}")
  89. i++
  90. continue
  91. }
  92. args = append(args, oArgs[i])
  93. }
  94. return args
  95. }
  96. func isFormatOpt(o string) bool {
  97. return o == "--format" || o == "-f"
  98. }