github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/hadolintExecute_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 hadolintExecuteOptions struct {
    19  	ConfigurationURL          string   `json:"configurationUrl,omitempty"`
    20  	ConfigurationUsername     string   `json:"configurationUsername,omitempty"`
    21  	ConfigurationPassword     string   `json:"configurationPassword,omitempty"`
    22  	DockerFile                string   `json:"dockerFile,omitempty"`
    23  	ConfigurationFile         string   `json:"configurationFile,omitempty"`
    24  	ReportFile                string   `json:"reportFile,omitempty"`
    25  	CustomTLSCertificateLinks []string `json:"customTlsCertificateLinks,omitempty"`
    26  }
    27  
    28  // HadolintExecuteCommand Executes the Haskell Dockerfile Linter which is a smarter Dockerfile linter that helps you build [best practice](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) Docker images.
    29  func HadolintExecuteCommand() *cobra.Command {
    30  	const STEP_NAME = "hadolintExecute"
    31  
    32  	metadata := hadolintExecuteMetadata()
    33  	var stepConfig hadolintExecuteOptions
    34  	var startTime time.Time
    35  	var logCollector *log.CollectorHook
    36  	var splunkClient *splunk.Splunk
    37  	telemetryClient := &telemetry.Telemetry{}
    38  
    39  	var createHadolintExecuteCmd = &cobra.Command{
    40  		Use:   STEP_NAME,
    41  		Short: "Executes the Haskell Dockerfile Linter which is a smarter Dockerfile linter that helps you build [best practice](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) Docker images.",
    42  		Long: `Executes the Haskell Dockerfile Linter which is a smarter Dockerfile linter that helps you build [best practice](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) Docker images.
    43  The linter is parsing the Dockerfile into an abstract syntax tree (AST) and performs rules on top of the AST.`,
    44  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    45  			startTime = time.Now()
    46  			log.SetStepName(STEP_NAME)
    47  			log.SetVerbose(GeneralConfig.Verbose)
    48  
    49  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    50  
    51  			path, _ := os.Getwd()
    52  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    53  			log.RegisterHook(fatalHook)
    54  
    55  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    56  			if err != nil {
    57  				log.SetErrorCategory(log.ErrorConfiguration)
    58  				return err
    59  			}
    60  			log.RegisterSecret(stepConfig.ConfigurationUsername)
    61  			log.RegisterSecret(stepConfig.ConfigurationPassword)
    62  
    63  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    64  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    65  				log.RegisterHook(&sentryHook)
    66  			}
    67  
    68  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    69  				splunkClient = &splunk.Splunk{}
    70  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    71  				log.RegisterHook(logCollector)
    72  			}
    73  
    74  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    75  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    76  			}
    77  
    78  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    79  			if err != nil {
    80  				return err
    81  			}
    82  			if err = validation.ValidateStruct(stepConfig); err != nil {
    83  				log.SetErrorCategory(log.ErrorConfiguration)
    84  				return err
    85  			}
    86  
    87  			return nil
    88  		},
    89  		Run: func(_ *cobra.Command, _ []string) {
    90  			stepTelemetryData := telemetry.CustomData{}
    91  			stepTelemetryData.ErrorCode = "1"
    92  			handler := func() {
    93  				config.RemoveVaultSecretFiles()
    94  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    95  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    96  				stepTelemetryData.PiperCommitHash = GitCommit
    97  				telemetryClient.SetData(&stepTelemetryData)
    98  				telemetryClient.Send()
    99  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   100  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   101  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   102  						GeneralConfig.HookConfig.SplunkConfig.Token,
   103  						GeneralConfig.HookConfig.SplunkConfig.Index,
   104  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   105  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   106  				}
   107  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   108  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   109  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   110  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   111  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   112  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   113  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   114  				}
   115  			}
   116  			log.DeferExitHandler(handler)
   117  			defer handler()
   118  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   119  			hadolintExecute(stepConfig, &stepTelemetryData)
   120  			stepTelemetryData.ErrorCode = "0"
   121  			log.Entry().Info("SUCCESS")
   122  		},
   123  	}
   124  
   125  	addHadolintExecuteFlags(createHadolintExecuteCmd, &stepConfig)
   126  	return createHadolintExecuteCmd
   127  }
   128  
   129  func addHadolintExecuteFlags(cmd *cobra.Command, stepConfig *hadolintExecuteOptions) {
   130  	cmd.Flags().StringVar(&stepConfig.ConfigurationURL, "configurationUrl", os.Getenv("PIPER_configurationUrl"), "URL pointing to the .hadolint.yaml exclude configuration to be used for linting. Also have a look at `configurationFile` which could avoid central configuration download in case the file is part of your repository.")
   131  	cmd.Flags().StringVar(&stepConfig.ConfigurationUsername, "configurationUsername", os.Getenv("PIPER_configurationUsername"), "The username to authenticate")
   132  	cmd.Flags().StringVar(&stepConfig.ConfigurationPassword, "configurationPassword", os.Getenv("PIPER_configurationPassword"), "The password to authenticate")
   133  	cmd.Flags().StringVar(&stepConfig.DockerFile, "dockerFile", `./Dockerfile`, "Dockerfile to be used for the assessment.")
   134  	cmd.Flags().StringVar(&stepConfig.ConfigurationFile, "configurationFile", `.hadolint.yaml`, "Name of the configuration file used locally within the step. If a file with this name is detected as part of your repo downloading the central configuration via `configurationUrl` will be skipped. If you change the file's name make sure your stashing configuration also reflects this.")
   135  	cmd.Flags().StringVar(&stepConfig.ReportFile, "reportFile", `hadolint.xml`, "Name of the result file used locally within the step.")
   136  	cmd.Flags().StringSliceVar(&stepConfig.CustomTLSCertificateLinks, "customTlsCertificateLinks", []string{}, "List of download links to custom TLS certificates. This is required to ensure trusted connections between Piper and the system where the configuration file is to be downloaded from.")
   137  
   138  }
   139  
   140  // retrieve step metadata
   141  func hadolintExecuteMetadata() config.StepData {
   142  	var theMetaData = config.StepData{
   143  		Metadata: config.StepMetadata{
   144  			Name:        "hadolintExecute",
   145  			Aliases:     []config.Alias{},
   146  			Description: "Executes the Haskell Dockerfile Linter which is a smarter Dockerfile linter that helps you build [best practice](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) Docker images.",
   147  		},
   148  		Spec: config.StepSpec{
   149  			Inputs: config.StepInputs{
   150  				Secrets: []config.StepSecrets{
   151  					{Name: "configurationCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing username/password for access to your remote configuration file.", Type: "jenkins"},
   152  				},
   153  				Parameters: []config.StepParameters{
   154  					{
   155  						Name:        "configurationUrl",
   156  						ResourceRef: []config.ResourceReference{},
   157  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   158  						Type:        "string",
   159  						Mandatory:   false,
   160  						Aliases:     []config.Alias{},
   161  						Default:     os.Getenv("PIPER_configurationUrl"),
   162  					},
   163  					{
   164  						Name: "configurationUsername",
   165  						ResourceRef: []config.ResourceReference{
   166  							{
   167  								Name:  "configurationCredentialsId",
   168  								Param: "username",
   169  								Type:  "secret",
   170  							},
   171  
   172  							{
   173  								Name:    "hadolintConfigSecretName",
   174  								Type:    "vaultSecret",
   175  								Default: "hadolintConfig",
   176  							},
   177  						},
   178  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   179  						Type:      "string",
   180  						Mandatory: false,
   181  						Aliases:   []config.Alias{{Name: "username"}},
   182  						Default:   os.Getenv("PIPER_configurationUsername"),
   183  					},
   184  					{
   185  						Name: "configurationPassword",
   186  						ResourceRef: []config.ResourceReference{
   187  							{
   188  								Name:  "configurationCredentialsId",
   189  								Param: "password",
   190  								Type:  "secret",
   191  							},
   192  
   193  							{
   194  								Name:    "hadolintConfigSecretName",
   195  								Type:    "vaultSecret",
   196  								Default: "hadolintConfig",
   197  							},
   198  						},
   199  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   200  						Type:      "string",
   201  						Mandatory: false,
   202  						Aliases:   []config.Alias{{Name: "password"}},
   203  						Default:   os.Getenv("PIPER_configurationPassword"),
   204  					},
   205  					{
   206  						Name:        "dockerFile",
   207  						ResourceRef: []config.ResourceReference{},
   208  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   209  						Type:        "string",
   210  						Mandatory:   false,
   211  						Aliases:     []config.Alias{{Name: "dockerfile"}},
   212  						Default:     `./Dockerfile`,
   213  					},
   214  					{
   215  						Name:        "configurationFile",
   216  						ResourceRef: []config.ResourceReference{},
   217  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   218  						Type:        "string",
   219  						Mandatory:   false,
   220  						Aliases:     []config.Alias{},
   221  						Default:     `.hadolint.yaml`,
   222  					},
   223  					{
   224  						Name:        "reportFile",
   225  						ResourceRef: []config.ResourceReference{},
   226  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   227  						Type:        "string",
   228  						Mandatory:   false,
   229  						Aliases:     []config.Alias{},
   230  						Default:     `hadolint.xml`,
   231  					},
   232  					{
   233  						Name:        "customTlsCertificateLinks",
   234  						ResourceRef: []config.ResourceReference{},
   235  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   236  						Type:        "[]string",
   237  						Mandatory:   false,
   238  						Aliases:     []config.Alias{},
   239  						Default:     []string{},
   240  					},
   241  				},
   242  			},
   243  			Containers: []config.Container{
   244  				{Name: "hadolint", Image: "hadolint/hadolint:latest-alpine"},
   245  			},
   246  		},
   247  	}
   248  	return theMetaData
   249  }