github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/version.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "github.com/SAP/jenkins-library/pkg/log" 6 "github.com/spf13/cobra" 7 "os" 8 ) 9 10 // GitCommit ... 11 var GitCommit string 12 13 // GitTag ... 14 var GitTag string 15 16 // VersionCommand Returns the version of the piper binary 17 func VersionCommand() *cobra.Command { 18 const STEP_NAME = "version" 19 20 var createVersionCmd = &cobra.Command{ 21 Use: STEP_NAME, 22 Short: "Returns the version of the piper binary", 23 Long: `Writes the commit hash and the tag (if any) to stdout and exits with 0.`, 24 PreRun: func(cmd *cobra.Command, args []string) { 25 log.SetStepName(STEP_NAME) 26 path, _ := os.Getwd() 27 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 28 log.RegisterHook(fatalHook) 29 }, 30 Run: func(cmd *cobra.Command, args []string) { 31 version() 32 }, 33 } 34 35 return createVersionCmd 36 } 37 38 func version() { 39 40 gitCommit, gitTag := "<n/a>", "<n/a>" 41 42 if len(GitCommit) > 0 { 43 gitCommit = GitCommit 44 } 45 46 if len(GitTag) > 0 { 47 gitTag = GitTag 48 } 49 50 fmt.Printf("piper-version:\n commit: \"%s\"\n tag: \"%s\"\n", gitCommit, gitTag) 51 }