version.go 1.7 KB

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