version.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 compose
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/cli/formatter"
  19. "github.com/docker/compose-cli/internal"
  20. )
  21. type versionOptions struct {
  22. format string
  23. short bool
  24. }
  25. func versionCommand() *cobra.Command {
  26. opts := versionOptions{}
  27. cmd := &cobra.Command{
  28. Use: "version",
  29. Short: "Show the Docker Compose version information",
  30. Args: cobra.MaximumNArgs(0),
  31. Hidden: true,
  32. RunE: func(cmd *cobra.Command, _ []string) error {
  33. runVersion(opts)
  34. return nil
  35. },
  36. }
  37. // define flags for backward compatibility with com.docker.cli
  38. flags := cmd.Flags()
  39. flags.StringVarP(&opts.format, "format", "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
  40. flags.BoolVar(&opts.short, "short", false, "Shows only Compose's version number.")
  41. return cmd
  42. }
  43. func runVersion(opts versionOptions) {
  44. displayedVersion := strings.TrimPrefix(internal.ComposePluginVersion, "v")
  45. if opts.short {
  46. fmt.Println(displayedVersion)
  47. return
  48. }
  49. if opts.format == formatter.JSON {
  50. fmt.Printf(`{"version":"%s"}\n`, displayedVersion)
  51. return
  52. }
  53. fmt.Printf(`Docker Compose version %s`, displayedVersion)
  54. }