version.go 3.5 KB

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