2018-12-03 22:19:18 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
2017-05-10 01:50:19 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-08-26 00:57:33 +03:00
|
|
|
"fmt"
|
2019-02-20 21:02:11 +03:00
|
|
|
"strings"
|
2017-08-26 00:57:33 +03:00
|
|
|
|
2019-03-22 04:08:16 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-09-12 08:13:07 +03:00
|
|
|
"github.com/Azure/aks-engine-azurestack/pkg/helpers"
|
2017-08-26 00:57:33 +03:00
|
|
|
|
2017-05-10 01:50:19 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-08-26 00:57:33 +03:00
|
|
|
// BuildTag holds the `git tag` if this is a tagged build/release
|
|
|
|
BuildTag = "canary"
|
|
|
|
|
2017-05-15 06:55:55 +03:00
|
|
|
// BuildSHA holds the git commit SHA at `make build` time.
|
|
|
|
BuildSHA = "unset"
|
2017-08-17 02:18:13 +03:00
|
|
|
|
2017-08-26 00:57:33 +03:00
|
|
|
// GitTreeState is the state of the git tree, either clean or dirty
|
|
|
|
GitTreeState = "unset"
|
|
|
|
|
|
|
|
outputFormatOptions = []string{"human", "json"}
|
|
|
|
outputFormat string
|
|
|
|
version versionInfo
|
2017-05-10 01:50:19 +03:00
|
|
|
)
|
|
|
|
|
2018-05-07 21:36:22 +03:00
|
|
|
const (
|
|
|
|
versionName = "version"
|
2020-05-04 20:59:22 +03:00
|
|
|
versionShortDescription = "Print the version of aks-engine"
|
2019-03-12 02:01:44 +03:00
|
|
|
versionLongDescription = versionShortDescription
|
2018-05-07 21:36:22 +03:00
|
|
|
)
|
|
|
|
|
2017-08-26 00:57:33 +03:00
|
|
|
type versionInfo struct {
|
|
|
|
GitTag string
|
|
|
|
GitCommit string
|
|
|
|
GitTreeState string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
version = versionInfo{
|
|
|
|
GitTag: BuildTag,
|
|
|
|
GitCommit: BuildSHA,
|
|
|
|
GitTreeState: GitTreeState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHumanVersion() string {
|
2019-03-12 02:01:44 +03:00
|
|
|
return fmt.Sprintf("Version: %s\nGitCommit: %s\nGitTreeState: %s",
|
2017-08-26 00:57:33 +03:00
|
|
|
version.GitTag,
|
|
|
|
version.GitCommit,
|
|
|
|
version.GitTreeState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getJSONVersion() string {
|
2017-12-05 03:53:21 +03:00
|
|
|
jsonVersion, _ := helpers.JSONMarshalIndent(version, "", " ", false)
|
2017-08-26 00:57:33 +03:00
|
|
|
return string(jsonVersion)
|
|
|
|
}
|
|
|
|
|
2019-03-12 02:01:44 +03:00
|
|
|
func getVersion(outputType string) (string, error) {
|
|
|
|
switch outputType {
|
|
|
|
case "human":
|
|
|
|
return getHumanVersion(), nil
|
|
|
|
case "json":
|
|
|
|
return getJSONVersion(), nil
|
|
|
|
default:
|
2019-03-22 04:08:16 +03:00
|
|
|
return "", errors.Errorf(`output format "%s" is not supported`, outputType)
|
2017-08-26 00:57:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 06:55:55 +03:00
|
|
|
func newVersionCmd() *cobra.Command {
|
2017-05-10 01:50:19 +03:00
|
|
|
versionCmd := &cobra.Command{
|
2018-05-07 21:36:22 +03:00
|
|
|
Use: versionName,
|
|
|
|
Short: versionShortDescription,
|
|
|
|
Long: versionLongDescription,
|
2017-05-10 01:50:19 +03:00
|
|
|
|
2019-03-12 02:01:44 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
output, err := getVersion(outputFormat)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println(output)
|
|
|
|
}
|
|
|
|
return err
|
2017-05-10 01:50:19 +03:00
|
|
|
},
|
|
|
|
}
|
2017-08-26 00:57:33 +03:00
|
|
|
|
2019-02-20 21:02:11 +03:00
|
|
|
versionCmdDescription := fmt.Sprintf("Output format. Allowed values: %s",
|
|
|
|
strings.Join(outputFormatOptions, ", "))
|
2017-08-26 00:57:33 +03:00
|
|
|
|
|
|
|
versionCmd.Flags().StringVarP(&outputFormat, "output", "o", "human", versionCmdDescription)
|
|
|
|
|
2017-05-10 01:50:19 +03:00
|
|
|
return versionCmd
|
|
|
|
}
|