You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Make version command a high level command like get-config without resolving the configuration (#1689)
This commit is contained in:
		| @@ -2,8 +2,9 @@ package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/spf13/cobra" | ||||
| 	"os" | ||||
| ) | ||||
|  | ||||
| // GitCommit ... | ||||
| @@ -12,7 +13,29 @@ var GitCommit string | ||||
| // GitTag ... | ||||
| var GitTag string | ||||
|  | ||||
| func version(config versionOptions, telemetryData *telemetry.CustomData) { | ||||
| // 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() { | ||||
|  | ||||
| 	gitCommit, gitTag := "<n/a>", "<n/a>" | ||||
|  | ||||
|   | ||||
| @@ -1,90 +0,0 @@ | ||||
| // Code generated by piper's step-generator. DO NOT EDIT. | ||||
|  | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/config" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| type versionOptions struct { | ||||
| } | ||||
|  | ||||
| // VersionCommand Returns the version of the piper binary | ||||
| func VersionCommand() *cobra.Command { | ||||
| 	const STEP_NAME = "version" | ||||
|  | ||||
| 	metadata := versionMetadata() | ||||
| 	var stepConfig versionOptions | ||||
| 	var startTime time.Time | ||||
|  | ||||
| 	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.`, | ||||
| 		PreRunE: func(cmd *cobra.Command, args []string) error { | ||||
| 			startTime = time.Now() | ||||
| 			log.SetStepName(STEP_NAME) | ||||
| 			log.SetVerbose(GeneralConfig.Verbose) | ||||
|  | ||||
| 			path, _ := os.Getwd() | ||||
| 			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} | ||||
| 			log.RegisterHook(fatalHook) | ||||
|  | ||||
| 			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| 				log.RegisterHook(&sentryHook) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}, | ||||
| 		Run: func(cmd *cobra.Command, args []string) { | ||||
| 			telemetryData := telemetry.CustomData{} | ||||
| 			telemetryData.ErrorCode = "1" | ||||
| 			handler := func() { | ||||
| 				telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) | ||||
| 				telemetry.Send(&telemetryData) | ||||
| 			} | ||||
| 			log.DeferExitHandler(handler) | ||||
| 			defer handler() | ||||
| 			telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) | ||||
| 			version(stepConfig, &telemetryData) | ||||
| 			telemetryData.ErrorCode = "0" | ||||
| 			log.Entry().Info("SUCCESS") | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	addVersionFlags(createVersionCmd, &stepConfig) | ||||
| 	return createVersionCmd | ||||
| } | ||||
|  | ||||
| func addVersionFlags(cmd *cobra.Command, stepConfig *versionOptions) { | ||||
|  | ||||
| } | ||||
|  | ||||
| // retrieve step metadata | ||||
| func versionMetadata() config.StepData { | ||||
| 	var theMetaData = config.StepData{ | ||||
| 		Metadata: config.StepMetadata{ | ||||
| 			Name:    "version", | ||||
| 			Aliases: []config.Alias{}, | ||||
| 		}, | ||||
| 		Spec: config.StepSpec{ | ||||
| 			Inputs: config.StepInputs{ | ||||
| 				Parameters: []config.StepParameters{}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	return theMetaData | ||||
| } | ||||
| @@ -1,16 +0,0 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestVersionCommand(t *testing.T) { | ||||
|  | ||||
| 	testCmd := VersionCommand() | ||||
|  | ||||
| 	// only high level testing performed - details are tested in step generation procudure | ||||
| 	assert.Equal(t, "version", testCmd.Use, "command name incorrect") | ||||
|  | ||||
| } | ||||
| @@ -6,8 +6,6 @@ import ( | ||||
| 	"io" | ||||
| 	"os" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| ) | ||||
|  | ||||
| func TestVersion(t *testing.T) { | ||||
| @@ -52,10 +50,7 @@ func runVersionCommand(t *testing.T, commitID, tag string) string { | ||||
| 	// | ||||
| 	// | ||||
|  | ||||
| 	var myVersionOptions versionOptions | ||||
| 	telemetryData := telemetry.CustomData{} | ||||
|  | ||||
| 	version(myVersionOptions, &telemetryData) | ||||
| 	version() | ||||
|  | ||||
| 	w.Close() | ||||
|  | ||||
|   | ||||
| @@ -1,5 +0,0 @@ | ||||
| metadata: | ||||
|   name: version | ||||
|   description: Returns the version of the piper binary | ||||
|   longDescription: | | ||||
|     Writes the commit hash and the tag (if any) to stdout and exits with 0. | ||||
		Reference in New Issue
	
	Block a user