github.com/xgoffin/jenkins-library@v1.154.0/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  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    74  			if err != nil {
    75  				return err
    76  			}
    77  			if err = validation.ValidateStruct(stepConfig); err != nil {
    78  				log.SetErrorCategory(log.ErrorConfiguration)
    79  				return err
    80  			}
    81  
    82  			return nil
    83  		},
    84  		Run: func(_ *cobra.Command, _ []string) {
    85  			stepTelemetryData := telemetry.CustomData{}
    86  			stepTelemetryData.ErrorCode = "1"
    87  			handler := func() {
    88  				config.RemoveVaultSecretFiles()
    89  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    90  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    91  				stepTelemetryData.PiperCommitHash = GitCommit
    92  				telemetryClient.SetData(&stepTelemetryData)
    93  				telemetryClient.Send()
    94  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    95  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    96  				}
    97  			}
    98  			log.DeferExitHandler(handler)
    99  			defer handler()
   100  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   101  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   102  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   103  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   104  					GeneralConfig.HookConfig.SplunkConfig.Token,
   105  					GeneralConfig.HookConfig.SplunkConfig.Index,
   106  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   107  			}
   108  			mavenExecute(stepConfig, &stepTelemetryData)
   109  			stepTelemetryData.ErrorCode = "0"
   110  			log.Entry().Info("SUCCESS")
   111  		},
   112  	}
   113  
   114  	addMavenExecuteFlags(createMavenExecuteCmd, &stepConfig)
   115  	return createMavenExecuteCmd
   116  }
   117  
   118  func addMavenExecuteFlags(cmd *cobra.Command, stepConfig *mavenExecuteOptions) {
   119  	cmd.Flags().StringVar(&stepConfig.PomPath, "pomPath", os.Getenv("PIPER_pomPath"), "Path to the pom file that should be used.")
   120  	cmd.Flags().StringSliceVar(&stepConfig.Goals, "goals", []string{}, "Maven goals that should be executed.")
   121  	cmd.Flags().StringSliceVar(&stepConfig.Defines, "defines", []string{}, "Additional properties in form of -Dkey=value.")
   122  	cmd.Flags().StringSliceVar(&stepConfig.Flags, "flags", []string{}, "Flags to provide when running mvn.")
   123  	cmd.Flags().BoolVar(&stepConfig.ReturnStdout, "returnStdout", false, "Returns the output of the maven command for further processing.")
   124  	cmd.Flags().StringVar(&stepConfig.ProjectSettingsFile, "projectSettingsFile", os.Getenv("PIPER_projectSettingsFile"), "Path to the mvn settings file that should be used as project settings file.")
   125  	cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Path to the mvn settings file that should be used as global settings file.")
   126  	cmd.Flags().StringVar(&stepConfig.M2Path, "m2Path", os.Getenv("PIPER_m2Path"), "Path to the location of the local repository that should be used.")
   127  	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.")
   128  
   129  	cmd.MarkFlagRequired("goals")
   130  }
   131  
   132  // retrieve step metadata
   133  func mavenExecuteMetadata() config.StepData {
   134  	var theMetaData = config.StepData{
   135  		Metadata: config.StepMetadata{
   136  			Name:        "mavenExecute",
   137  			Aliases:     []config.Alias{},
   138  			Description: "This step allows to run maven commands",
   139  		},
   140  		Spec: config.StepSpec{
   141  			Inputs: config.StepInputs{
   142  				Parameters: []config.StepParameters{
   143  					{
   144  						Name:        "pomPath",
   145  						ResourceRef: []config.ResourceReference{},
   146  						Scope:       []string{"PARAMETERS", "STEPS"},
   147  						Type:        "string",
   148  						Mandatory:   false,
   149  						Aliases:     []config.Alias{},
   150  						Default:     os.Getenv("PIPER_pomPath"),
   151  					},
   152  					{
   153  						Name:        "goals",
   154  						ResourceRef: []config.ResourceReference{},
   155  						Scope:       []string{"PARAMETERS"},
   156  						Type:        "[]string",
   157  						Mandatory:   true,
   158  						Aliases:     []config.Alias{},
   159  						Default:     []string{},
   160  					},
   161  					{
   162  						Name:        "defines",
   163  						ResourceRef: []config.ResourceReference{},
   164  						Scope:       []string{"PARAMETERS"},
   165  						Type:        "[]string",
   166  						Mandatory:   false,
   167  						Aliases:     []config.Alias{},
   168  						Default:     []string{},
   169  					},
   170  					{
   171  						Name:        "flags",
   172  						ResourceRef: []config.ResourceReference{},
   173  						Scope:       []string{"PARAMETERS", "STEPS"},
   174  						Type:        "[]string",
   175  						Mandatory:   false,
   176  						Aliases:     []config.Alias{},
   177  						Default:     []string{},
   178  					},
   179  					{
   180  						Name:        "returnStdout",
   181  						ResourceRef: []config.ResourceReference{},
   182  						Scope:       []string{"PARAMETERS"},
   183  						Type:        "bool",
   184  						Mandatory:   false,
   185  						Aliases:     []config.Alias{},
   186  						Default:     false,
   187  					},
   188  					{
   189  						Name:        "projectSettingsFile",
   190  						ResourceRef: []config.ResourceReference{},
   191  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   192  						Type:        "string",
   193  						Mandatory:   false,
   194  						Aliases:     []config.Alias{{Name: "maven/projectSettingsFile"}},
   195  						Default:     os.Getenv("PIPER_projectSettingsFile"),
   196  					},
   197  					{
   198  						Name:        "globalSettingsFile",
   199  						ResourceRef: []config.ResourceReference{},
   200  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   201  						Type:        "string",
   202  						Mandatory:   false,
   203  						Aliases:     []config.Alias{{Name: "maven/globalSettingsFile"}},
   204  						Default:     os.Getenv("PIPER_globalSettingsFile"),
   205  					},
   206  					{
   207  						Name:        "m2Path",
   208  						ResourceRef: []config.ResourceReference{},
   209  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   210  						Type:        "string",
   211  						Mandatory:   false,
   212  						Aliases:     []config.Alias{{Name: "maven/m2Path"}},
   213  						Default:     os.Getenv("PIPER_m2Path"),
   214  					},
   215  					{
   216  						Name:        "logSuccessfulMavenTransfers",
   217  						ResourceRef: []config.ResourceReference{},
   218  						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
   219  						Type:        "bool",
   220  						Mandatory:   false,
   221  						Aliases:     []config.Alias{{Name: "maven/logSuccessfulMavenTransfers"}},
   222  						Default:     false,
   223  					},
   224  				},
   225  			},
   226  			Containers: []config.Container{
   227  				{Name: "mvn", Image: "maven:3.6-jdk-8"},
   228  			},
   229  		},
   230  	}
   231  	return theMetaData
   232  }