github.com/jaylevin/jenkins-library@v1.230.4/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  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    75  			if err != nil {
    76  				return err
    77  			}
    78  			if err = validation.ValidateStruct(stepConfig); err != nil {
    79  				log.SetErrorCategory(log.ErrorConfiguration)
    80  				return err
    81  			}
    82  
    83  			return nil
    84  		},
    85  		Run: func(_ *cobra.Command, _ []string) {
    86  			stepTelemetryData := telemetry.CustomData{}
    87  			stepTelemetryData.ErrorCode = "1"
    88  			handler := func() {
    89  				config.RemoveVaultSecretFiles()
    90  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    91  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    92  				stepTelemetryData.PiperCommitHash = GitCommit
    93  				telemetryClient.SetData(&stepTelemetryData)
    94  				telemetryClient.Send()
    95  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    96  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    97  				}
    98  			}
    99  			log.DeferExitHandler(handler)
   100  			defer handler()
   101  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   102  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   103  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   104  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   105  					GeneralConfig.HookConfig.SplunkConfig.Token,
   106  					GeneralConfig.HookConfig.SplunkConfig.Index,
   107  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   108  			}
   109  			hadolintExecute(stepConfig, &stepTelemetryData)
   110  			stepTelemetryData.ErrorCode = "0"
   111  			log.Entry().Info("SUCCESS")
   112  		},
   113  	}
   114  
   115  	addHadolintExecuteFlags(createHadolintExecuteCmd, &stepConfig)
   116  	return createHadolintExecuteCmd
   117  }
   118  
   119  func addHadolintExecuteFlags(cmd *cobra.Command, stepConfig *hadolintExecuteOptions) {
   120  	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.")
   121  	cmd.Flags().StringVar(&stepConfig.ConfigurationUsername, "configurationUsername", os.Getenv("PIPER_configurationUsername"), "The username to authenticate")
   122  	cmd.Flags().StringVar(&stepConfig.ConfigurationPassword, "configurationPassword", os.Getenv("PIPER_configurationPassword"), "The password to authenticate")
   123  	cmd.Flags().StringVar(&stepConfig.DockerFile, "dockerFile", `./Dockerfile`, "Dockerfile to be used for the assessment.")
   124  	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.")
   125  	cmd.Flags().StringVar(&stepConfig.ReportFile, "reportFile", `hadolint.xml`, "Name of the result file used locally within the step.")
   126  	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.")
   127  
   128  }
   129  
   130  // retrieve step metadata
   131  func hadolintExecuteMetadata() config.StepData {
   132  	var theMetaData = config.StepData{
   133  		Metadata: config.StepMetadata{
   134  			Name:        "hadolintExecute",
   135  			Aliases:     []config.Alias{},
   136  			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.",
   137  		},
   138  		Spec: config.StepSpec{
   139  			Inputs: config.StepInputs{
   140  				Secrets: []config.StepSecrets{
   141  					{Name: "configurationCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing username/password for access to your remote configuration file.", Type: "jenkins"},
   142  				},
   143  				Parameters: []config.StepParameters{
   144  					{
   145  						Name:        "configurationUrl",
   146  						ResourceRef: []config.ResourceReference{},
   147  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   148  						Type:        "string",
   149  						Mandatory:   false,
   150  						Aliases:     []config.Alias{},
   151  						Default:     os.Getenv("PIPER_configurationUrl"),
   152  					},
   153  					{
   154  						Name: "configurationUsername",
   155  						ResourceRef: []config.ResourceReference{
   156  							{
   157  								Name:  "configurationCredentialsId",
   158  								Param: "username",
   159  								Type:  "secret",
   160  							},
   161  
   162  							{
   163  								Name:    "hadolintConfigSecretName",
   164  								Type:    "vaultSecret",
   165  								Default: "hadolintConfig",
   166  							},
   167  						},
   168  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   169  						Type:      "string",
   170  						Mandatory: false,
   171  						Aliases:   []config.Alias{{Name: "username"}},
   172  						Default:   os.Getenv("PIPER_configurationUsername"),
   173  					},
   174  					{
   175  						Name: "configurationPassword",
   176  						ResourceRef: []config.ResourceReference{
   177  							{
   178  								Name:  "configurationCredentialsId",
   179  								Param: "password",
   180  								Type:  "secret",
   181  							},
   182  
   183  							{
   184  								Name:    "hadolintConfigSecretName",
   185  								Type:    "vaultSecret",
   186  								Default: "hadolintConfig",
   187  							},
   188  						},
   189  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   190  						Type:      "string",
   191  						Mandatory: false,
   192  						Aliases:   []config.Alias{{Name: "password"}},
   193  						Default:   os.Getenv("PIPER_configurationPassword"),
   194  					},
   195  					{
   196  						Name:        "dockerFile",
   197  						ResourceRef: []config.ResourceReference{},
   198  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   199  						Type:        "string",
   200  						Mandatory:   false,
   201  						Aliases:     []config.Alias{{Name: "dockerfile"}},
   202  						Default:     `./Dockerfile`,
   203  					},
   204  					{
   205  						Name:        "configurationFile",
   206  						ResourceRef: []config.ResourceReference{},
   207  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   208  						Type:        "string",
   209  						Mandatory:   false,
   210  						Aliases:     []config.Alias{},
   211  						Default:     `.hadolint.yaml`,
   212  					},
   213  					{
   214  						Name:        "reportFile",
   215  						ResourceRef: []config.ResourceReference{},
   216  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   217  						Type:        "string",
   218  						Mandatory:   false,
   219  						Aliases:     []config.Alias{},
   220  						Default:     `hadolint.xml`,
   221  					},
   222  					{
   223  						Name:        "customTlsCertificateLinks",
   224  						ResourceRef: []config.ResourceReference{},
   225  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   226  						Type:        "[]string",
   227  						Mandatory:   false,
   228  						Aliases:     []config.Alias{},
   229  						Default:     []string{},
   230  					},
   231  				},
   232  			},
   233  			Containers: []config.Container{
   234  				{Name: "hadolint", Image: "hadolint/hadolint:latest-alpine"},
   235  			},
   236  		},
   237  	}
   238  	return theMetaData
   239  }