github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/mavenExecute_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type mavenExecuteOptions struct {
    19  	PomPath                     string   `json:"pomPath,omitempty"`
    20  	Goals                       []string `json:"goals,omitempty"`
    21  	Defines                     []string `json:"defines,omitempty"`
    22  	Flags                       []string `json:"flags,omitempty"`
    23  	ReturnStdout                bool     `json:"returnStdout,omitempty"`
    24  	ProjectSettingsFile         string   `json:"projectSettingsFile,omitempty"`
    25  	GlobalSettingsFile          string   `json:"globalSettingsFile,omitempty"`
    26  	M2Path                      string   `json:"m2Path,omitempty"`
    27  	LogSuccessfulMavenTransfers bool     `json:"logSuccessfulMavenTransfers,omitempty"`
    28  }
    29  
    30  // MavenExecuteCommand This step allows to run maven commands
    31  func MavenExecuteCommand() *cobra.Command {
    32  	const STEP_NAME = "mavenExecute"
    33  
    34  	metadata := mavenExecuteMetadata()
    35  	var stepConfig mavenExecuteOptions
    36  	var startTime time.Time
    37  	var logCollector *log.CollectorHook
    38  	var splunkClient *splunk.Splunk
    39  	telemetryClient := &telemetry.Telemetry{}
    40  
    41  	var createMavenExecuteCmd = &cobra.Command{
    42  		Use:   STEP_NAME,
    43  		Short: "This step allows to run maven commands",
    44  		Long:  `This step runs a maven command based on the parameters provided to the step.`,
    45  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    46  			startTime = time.Now()
    47  			log.SetStepName(STEP_NAME)
    48  			log.SetVerbose(GeneralConfig.Verbose)
    49  
    50  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    51  
    52  			path, _ := os.Getwd()
    53  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    54  			log.RegisterHook(fatalHook)
    55  
    56  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    57  			if err != nil {
    58  				log.SetErrorCategory(log.ErrorConfiguration)
    59  				return err
    60  			}
    61  
    62  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    63  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    64  				log.RegisterHook(&sentryHook)
    65  			}
    66  
    67  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    68  				splunkClient = &splunk.Splunk{}
    69  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    70  				log.RegisterHook(logCollector)
    71  			}
    72  
    73  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    74  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    75  			}
    76  
    77  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    78  			if err != nil {
    79  				return err
    80  			}
    81  			if err = validation.ValidateStruct(stepConfig); err != nil {
    82  				log.SetErrorCategory(log.ErrorConfiguration)
    83  				return err
    84  			}
    85  
    86  			return nil
    87  		},
    88  		Run: func(_ *cobra.Command, _ []string) {
    89  			stepTelemetryData := telemetry.CustomData{}
    90  			stepTelemetryData.ErrorCode = "1"
    91  			handler := func() {
    92  				config.RemoveVaultSecretFiles()
    93  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    94  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    95  				stepTelemetryData.PiperCommitHash = GitCommit
    96  				telemetryClient.SetData(&stepTelemetryData)
    97  				telemetryClient.Send()
    98  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    99  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   100  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   101  						GeneralConfig.HookConfig.SplunkConfig.Token,
   102  						GeneralConfig.HookConfig.SplunkConfig.Index,
   103  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   104  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   105  				}
   106  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   107  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   108  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   109  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   110  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   111  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   112  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   113  				}
   114  			}
   115  			log.DeferExitHandler(handler)
   116  			defer handler()
   117  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   118  			mavenExecute(stepConfig, &stepTelemetryData)
   119  			stepTelemetryData.ErrorCode = "0"
   120  			log.Entry().Info("SUCCESS")
   121  		},
   122  	}
   123  
   124  	addMavenExecuteFlags(createMavenExecuteCmd, &stepConfig)
   125  	return createMavenExecuteCmd
   126  }
   127  
   128  func addMavenExecuteFlags(cmd *cobra.Command, stepConfig *mavenExecuteOptions) {
   129  	cmd.Flags().StringVar(&stepConfig.PomPath, "pomPath", os.Getenv("PIPER_pomPath"), "Path to the pom file that should be used.")
   130  	cmd.Flags().StringSliceVar(&stepConfig.Goals, "goals", []string{}, "Maven goals that should be executed.")
   131  	cmd.Flags().StringSliceVar(&stepConfig.Defines, "defines", []string{}, "Additional properties in form of -Dkey=value.")
   132  	cmd.Flags().StringSliceVar(&stepConfig.Flags, "flags", []string{}, "Flags to provide when running mvn.")
   133  	cmd.Flags().BoolVar(&stepConfig.ReturnStdout, "returnStdout", false, "Returns the output of the maven command for further processing.")
   134  	cmd.Flags().StringVar(&stepConfig.ProjectSettingsFile, "projectSettingsFile", os.Getenv("PIPER_projectSettingsFile"), "Path to the mvn settings file that should be used as project settings file.")
   135  	cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Path to the mvn settings file that should be used as global settings file.")
   136  	cmd.Flags().StringVar(&stepConfig.M2Path, "m2Path", os.Getenv("PIPER_m2Path"), "Path to the location of the local repository that should be used.")
   137  	cmd.Flags().BoolVar(&stepConfig.LogSuccessfulMavenTransfers, "logSuccessfulMavenTransfers", false, "Configures maven to log successful downloads. This is set to `false` by default to reduce the noise in build logs.")
   138  
   139  	cmd.MarkFlagRequired("goals")
   140  }
   141  
   142  // retrieve step metadata
   143  func mavenExecuteMetadata() config.StepData {
   144  	var theMetaData = config.StepData{
   145  		Metadata: config.StepMetadata{
   146  			Name:        "mavenExecute",
   147  			Aliases:     []config.Alias{},
   148  			Description: "This step allows to run maven commands",
   149  		},
   150  		Spec: config.StepSpec{
   151  			Inputs: config.StepInputs{
   152  				Parameters: []config.StepParameters{
   153  					{
   154  						Name:        "pomPath",
   155  						ResourceRef: []config.ResourceReference{},
   156  						Scope:       []string{"PARAMETERS", "STEPS"},
   157  						Type:        "string",
   158  						Mandatory:   false,
   159  						Aliases:     []config.Alias{},
   160  						Default:     os.Getenv("PIPER_pomPath"),
   161  					},
   162  					{
   163  						Name:        "goals",
   164  						ResourceRef: []config.ResourceReference{},
   165  						Scope:       []string{"PARAMETERS"},
   166  						Type:        "[]string",
   167  						Mandatory:   true,
   168  						Aliases:     []config.Alias{},
   169  						Default:     []string{},
   170  					},
   171  					{
   172  						Name:        "defines",
   173  						ResourceRef: []config.ResourceReference{},
   174  						Scope:       []string{"PARAMETERS"},
   175  						Type:        "[]string",
   176  						Mandatory:   false,
   177  						Aliases:     []config.Alias{},
   178  						Default:     []string{},
   179  					},
   180  					{
   181  						Name:        "flags",
   182  						ResourceRef: []config.ResourceReference{},
   183  						Scope:       []string{"PARAMETERS", "STEPS"},
   184  						Type:        "[]string",
   185  						Mandatory:   false,
   186  						Aliases:     []config.Alias{},
   187  						Default:     []string{},
   188  					},
   189  					{
   190  						Name:        "returnStdout",
   191  						ResourceRef: []config.ResourceReference{},
   192  						Scope:       []string{"PARAMETERS"},
   193  						Type:        "bool",
   194  						Mandatory:   false,
   195  						Aliases:     []config.Alias{},
   196  						Default:     false,
   197  					},
   198  					{
   199  						Name:        "projectSettingsFile",
   200  						ResourceRef: []config.ResourceReference{},
   201  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   202  						Type:        "string",
   203  						Mandatory:   false,
   204  						Aliases:     []config.Alias{{Name: "maven/projectSettingsFile"}},
   205  						Default:     os.Getenv("PIPER_projectSettingsFile"),
   206  					},
   207  					{
   208  						Name:        "globalSettingsFile",
   209  						ResourceRef: []config.ResourceReference{},
   210  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   211  						Type:        "string",
   212  						Mandatory:   false,
   213  						Aliases:     []config.Alias{{Name: "maven/globalSettingsFile"}},
   214  						Default:     os.Getenv("PIPER_globalSettingsFile"),
   215  					},
   216  					{
   217  						Name:        "m2Path",
   218  						ResourceRef: []config.ResourceReference{},
   219  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   220  						Type:        "string",
   221  						Mandatory:   false,
   222  						Aliases:     []config.Alias{{Name: "maven/m2Path"}},
   223  						Default:     os.Getenv("PIPER_m2Path"),
   224  					},
   225  					{
   226  						Name:        "logSuccessfulMavenTransfers",
   227  						ResourceRef: []config.ResourceReference{},
   228  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   229  						Type:        "bool",
   230  						Mandatory:   false,
   231  						Aliases:     []config.Alias{{Name: "maven/logSuccessfulMavenTransfers"}},
   232  						Default:     false,
   233  					},
   234  				},
   235  			},
   236  			Containers: []config.Container{
   237  				{Name: "mvn", Image: "maven:3.6-jdk-8"},
   238  			},
   239  		},
   240  	}
   241  	return theMetaData
   242  }