version.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "strings"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/api/cli/cmd/mobyflags"
  19. "github.com/docker/api/cli/mobycli"
  20. )
  21. const cliVersion = "0.1.1"
  22. // VersionCommand command to display version
  23. func VersionCommand() *cobra.Command {
  24. cmd := &cobra.Command{
  25. Use: "version",
  26. Short: "Show the Docker version information",
  27. Args: cobra.MaximumNArgs(0),
  28. RunE: runVersion,
  29. }
  30. // define flags for backward compatibility with com.docker.cli
  31. flags := cmd.Flags()
  32. flags.String("format", "", "Format the output using the given Go template")
  33. flags.String("kubeconfig", "", "Kubernetes config file")
  34. mobyflags.AddMobyFlagsForRetrocompatibility(flags)
  35. return cmd
  36. }
  37. func runVersion(cmd *cobra.Command, args []string) error {
  38. versionResult, _ := mobycli.ExecSilent(cmd.Context())
  39. // we don't want to fail on error, there is an error if the engine is not available but it displays client version info
  40. // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
  41. if versionResult == nil {
  42. return mobycli.ExecCmd(cmd)
  43. }
  44. var s string = string(versionResult)
  45. fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration "+cliVersion+"\n Version:", 1))
  46. return nil
  47. }