github.com/xgoffin/jenkins-library@v1.154.0/cmd/npmExecuteLint_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 npmExecuteLintOptions struct { 19 Install bool `json:"install,omitempty"` 20 RunScript string `json:"runScript,omitempty"` 21 FailOnError bool `json:"failOnError,omitempty"` 22 DefaultNpmRegistry string `json:"defaultNpmRegistry,omitempty"` 23 } 24 25 // NpmExecuteLintCommand Execute ci-lint script on all npm packages in a project or execute default linting 26 func NpmExecuteLintCommand() *cobra.Command { 27 const STEP_NAME = "npmExecuteLint" 28 29 metadata := npmExecuteLintMetadata() 30 var stepConfig npmExecuteLintOptions 31 var startTime time.Time 32 var logCollector *log.CollectorHook 33 var splunkClient *splunk.Splunk 34 telemetryClient := &telemetry.Telemetry{} 35 36 var createNpmExecuteLintCmd = &cobra.Command{ 37 Use: STEP_NAME, 38 Short: "Execute ci-lint script on all npm packages in a project or execute default linting", 39 Long: `Execute ci-lint script for all package json files, if they implement the script. If no ci-lint script is defined, 40 either use ESLint configurations present in the project or use the provided general purpose configuration to run ESLint.`, 41 PreRunE: func(cmd *cobra.Command, _ []string) error { 42 startTime = time.Now() 43 log.SetStepName(STEP_NAME) 44 log.SetVerbose(GeneralConfig.Verbose) 45 46 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 47 48 path, _ := os.Getwd() 49 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 50 log.RegisterHook(fatalHook) 51 52 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 53 if err != nil { 54 log.SetErrorCategory(log.ErrorConfiguration) 55 return err 56 } 57 58 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 59 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 60 log.RegisterHook(&sentryHook) 61 } 62 63 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 64 splunkClient = &splunk.Splunk{} 65 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 66 log.RegisterHook(logCollector) 67 } 68 69 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 70 if err != nil { 71 return err 72 } 73 if err = validation.ValidateStruct(stepConfig); err != nil { 74 log.SetErrorCategory(log.ErrorConfiguration) 75 return err 76 } 77 78 return nil 79 }, 80 Run: func(_ *cobra.Command, _ []string) { 81 stepTelemetryData := telemetry.CustomData{} 82 stepTelemetryData.ErrorCode = "1" 83 handler := func() { 84 config.RemoveVaultSecretFiles() 85 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 86 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 87 stepTelemetryData.PiperCommitHash = GitCommit 88 telemetryClient.SetData(&stepTelemetryData) 89 telemetryClient.Send() 90 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 91 splunkClient.Send(telemetryClient.GetData(), logCollector) 92 } 93 } 94 log.DeferExitHandler(handler) 95 defer handler() 96 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 97 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 98 splunkClient.Initialize(GeneralConfig.CorrelationID, 99 GeneralConfig.HookConfig.SplunkConfig.Dsn, 100 GeneralConfig.HookConfig.SplunkConfig.Token, 101 GeneralConfig.HookConfig.SplunkConfig.Index, 102 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 103 } 104 npmExecuteLint(stepConfig, &stepTelemetryData) 105 stepTelemetryData.ErrorCode = "0" 106 log.Entry().Info("SUCCESS") 107 }, 108 } 109 110 addNpmExecuteLintFlags(createNpmExecuteLintCmd, &stepConfig) 111 return createNpmExecuteLintCmd 112 } 113 114 func addNpmExecuteLintFlags(cmd *cobra.Command, stepConfig *npmExecuteLintOptions) { 115 cmd.Flags().BoolVar(&stepConfig.Install, "install", false, "Run npm install or similar commands depending on the project structure.") 116 cmd.Flags().StringVar(&stepConfig.RunScript, "runScript", `ci-lint`, "List of additional run scripts to execute from package.json.") 117 cmd.Flags().BoolVar(&stepConfig.FailOnError, "failOnError", false, "Defines the behavior in case linting errors are found.") 118 cmd.Flags().StringVar(&stepConfig.DefaultNpmRegistry, "defaultNpmRegistry", os.Getenv("PIPER_defaultNpmRegistry"), "URL of the npm registry to use. Defaults to https://registry.npmjs.org/") 119 120 } 121 122 // retrieve step metadata 123 func npmExecuteLintMetadata() config.StepData { 124 var theMetaData = config.StepData{ 125 Metadata: config.StepMetadata{ 126 Name: "npmExecuteLint", 127 Aliases: []config.Alias{{Name: "executeNpm", Deprecated: false}}, 128 Description: "Execute ci-lint script on all npm packages in a project or execute default linting", 129 }, 130 Spec: config.StepSpec{ 131 Inputs: config.StepInputs{ 132 Parameters: []config.StepParameters{ 133 { 134 Name: "install", 135 ResourceRef: []config.ResourceReference{}, 136 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 137 Type: "bool", 138 Mandatory: false, 139 Aliases: []config.Alias{}, 140 Default: false, 141 }, 142 { 143 Name: "runScript", 144 ResourceRef: []config.ResourceReference{}, 145 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 146 Type: "string", 147 Mandatory: false, 148 Aliases: []config.Alias{}, 149 Default: `ci-lint`, 150 }, 151 { 152 Name: "failOnError", 153 ResourceRef: []config.ResourceReference{}, 154 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 155 Type: "bool", 156 Mandatory: false, 157 Aliases: []config.Alias{}, 158 Default: false, 159 }, 160 { 161 Name: "defaultNpmRegistry", 162 ResourceRef: []config.ResourceReference{}, 163 Scope: []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, 164 Type: "string", 165 Mandatory: false, 166 Aliases: []config.Alias{{Name: "npm/defaultNpmRegistry"}}, 167 Default: os.Getenv("PIPER_defaultNpmRegistry"), 168 }, 169 }, 170 }, 171 Containers: []config.Container{ 172 {Name: "node", Image: "node:lts-stretch"}, 173 }, 174 }, 175 } 176 return theMetaData 177 }