1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/generator/helper/testdata/TestProcessMetaFiles/step_code_generated.golden
Oliver Nocon 43947e6ef5
Pass golang error details to Jenkins pipeline (#1443)
Do not exit with os.Exit(1) but using log.Entry().Fatal() instead
* Golang: forward error details
* extend groovy wrapper to provide proper error message
* create closure for error handling
2020-04-28 07:42:02 +02:00

192 lines
5.2 KiB
Plaintext

// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperenv"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/spf13/cobra"
)
type testStepOptions struct {
Param0 string `json:"param0,omitempty"`
Param1 string `json:"param1,omitempty"`
Param2 string `json:"param2,omitempty"`
}
type testStepCommonPipelineEnvironment struct {
artifactVersion string
git struct {
commitID string
branch string
}
}
func (p *testStepCommonPipelineEnvironment) persist(path, resourceName string) {
content := []struct{
category string
name string
value string
}{
{category: "", name: "artifactVersion", value: p.artifactVersion},
{category: "git", name: "commitId", value: p.git.commitID},
{category: "git", name: "branch", value: p.git.branch},
}
errCount := 0
for _, param := range content {
err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
if err != nil {
log.Entry().WithError(err).Error("Error persisting piper environment.")
errCount++
}
}
if errCount > 0 {
log.Entry().Fatal("failed to persist Piper environment")
}
}
type testStepInfluxTest struct {
m1 struct {
fields struct {
f1 string
}
tags struct {
t1 string
}
}
}
func (i *testStepInfluxTest) persist(path, resourceName string) {
measurementContent := []struct{
measurement string
valType string
name string
value string
}{
{valType: config.InfluxField, measurement: "m1" , name: "f1", value: i.m1.fields.f1},
{valType: config.InfluxTag, measurement: "m1" , name: "t1", value: i.m1.tags.t1},
}
errCount := 0
for _, metric := range measurementContent {
err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(metric.measurement, fmt.Sprintf("%vs", metric.valType), metric.name), metric.value)
if err != nil {
log.Entry().WithError(err).Error("Error persisting influx environment.")
errCount++
}
}
if errCount > 0 {
log.Entry().Fatal("failed to persist Influx environment")
}
}
// TestStepCommand Test description
func TestStepCommand() *cobra.Command {
const STEP_NAME = "testStep"
metadata := testStepMetadata()
var stepConfig testStepOptions
var startTime time.Time
var commonPipelineEnvironment testStepCommonPipelineEnvironment
var influxTest testStepInfluxTest
var createTestStepCmd = &cobra.Command{
Use: STEP_NAME,
Short: "Test description",
Long: `Long Test description`,
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
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
telemetryData := telemetry.CustomData{}
telemetryData.ErrorCode = "1"
handler := func() {
commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
influxTest.persist(GeneralConfig.EnvRootPath, "influxTest")
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
telemetry.Send(&telemetryData)
}
log.DeferExitHandler(handler)
defer handler()
telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
testStep(stepConfig, &telemetryData, &commonPipelineEnvironment, &influxTest)
telemetryData.ErrorCode = "0"
},
}
addTestStepFlags(createTestStepCmd, &stepConfig)
return createTestStepCmd
}
func addTestStepFlags(cmd *cobra.Command, stepConfig *testStepOptions) {
cmd.Flags().StringVar(&stepConfig.Param0, "param0", "val0", "param0 description")
cmd.Flags().StringVar(&stepConfig.Param1, "param1", os.Getenv("PIPER_param1"), "param1 description")
cmd.Flags().StringVar(&stepConfig.Param2, "param2", os.Getenv("PIPER_param2"), "param1 description")
cmd.MarkFlagRequired("param0")
cmd.MarkFlagRequired("param2")
}
// retrieve step metadata
func testStepMetadata() config.StepData {
var theMetaData = config.StepData{
Metadata: config.StepMetadata{
Name: "testStep",
Aliases: []config.Alias{{Name: "testStepAlias", Deprecated: true},},
},
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{
Name: "param0",
ResourceRef: []config.ResourceReference{},
Scope: []string{"GENERAL","PARAMETERS",},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "param1",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS",},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "param2",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS",},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
},
},
},
},
}
return theMetaData
}