2019-10-31 14:57:29 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-19 14:39:19 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"os"
|
2019-10-31 14:57:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GitCommit ...
|
|
|
|
var GitCommit string
|
|
|
|
|
|
|
|
// GitTag ...
|
|
|
|
var GitTag string
|
|
|
|
|
2020-06-19 14:39:19 +02:00
|
|
|
// VersionCommand Returns the version of the piper binary
|
|
|
|
func VersionCommand() *cobra.Command {
|
|
|
|
const STEP_NAME = "version"
|
|
|
|
|
|
|
|
var createVersionCmd = &cobra.Command{
|
|
|
|
Use: STEP_NAME,
|
|
|
|
Short: "Returns the version of the piper binary",
|
|
|
|
Long: `Writes the commit hash and the tag (if any) to stdout and exits with 0.`,
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
log.SetStepName(STEP_NAME)
|
|
|
|
path, _ := os.Getwd()
|
|
|
|
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
|
|
|
log.RegisterHook(fatalHook)
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
version()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return createVersionCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func version() {
|
2019-10-31 14:57:29 +02:00
|
|
|
|
|
|
|
gitCommit, gitTag := "<n/a>", "<n/a>"
|
|
|
|
|
|
|
|
if len(GitCommit) > 0 {
|
|
|
|
gitCommit = GitCommit
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(GitTag) > 0 {
|
|
|
|
gitTag = GitTag
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
fmt.Printf("piper-version:\n commit: \"%s\"\n tag: \"%s\"\n", gitCommit, gitTag)
|
2019-10-31 14:57:29 +02:00
|
|
|
}
|