github.com/xgoffin/jenkins-library@v1.154.0/cmd/shellExecute_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 shellExecuteOptions struct {
    19  	Sources []string `json:"sources,omitempty"`
    20  }
    21  
    22  // ShellExecuteCommand Step executes defined script
    23  func ShellExecuteCommand() *cobra.Command {
    24  	const STEP_NAME = "shellExecute"
    25  
    26  	metadata := shellExecuteMetadata()
    27  	var stepConfig shellExecuteOptions
    28  	var startTime time.Time
    29  	var logCollector *log.CollectorHook
    30  	var splunkClient *splunk.Splunk
    31  	telemetryClient := &telemetry.Telemetry{}
    32  
    33  	var createShellExecuteCmd = &cobra.Command{
    34  		Use:   STEP_NAME,
    35  		Short: "Step executes defined script",
    36  		Long:  `Step executes defined script with using test Vault credentials`,
    37  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    38  			startTime = time.Now()
    39  			log.SetStepName(STEP_NAME)
    40  			log.SetVerbose(GeneralConfig.Verbose)
    41  
    42  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    43  
    44  			path, _ := os.Getwd()
    45  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    46  			log.RegisterHook(fatalHook)
    47  
    48  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    49  			if err != nil {
    50  				log.SetErrorCategory(log.ErrorConfiguration)
    51  				return err
    52  			}
    53  
    54  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    55  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    56  				log.RegisterHook(&sentryHook)
    57  			}
    58  
    59  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    60  				splunkClient = &splunk.Splunk{}
    61  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    62  				log.RegisterHook(logCollector)
    63  			}
    64  
    65  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    66  			if err != nil {
    67  				return err
    68  			}
    69  			if err = validation.ValidateStruct(stepConfig); err != nil {
    70  				log.SetErrorCategory(log.ErrorConfiguration)
    71  				return err
    72  			}
    73  
    74  			return nil
    75  		},
    76  		Run: func(_ *cobra.Command, _ []string) {
    77  			stepTelemetryData := telemetry.CustomData{}
    78  			stepTelemetryData.ErrorCode = "1"
    79  			handler := func() {
    80  				config.RemoveVaultSecretFiles()
    81  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    82  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    83  				stepTelemetryData.PiperCommitHash = GitCommit
    84  				telemetryClient.SetData(&stepTelemetryData)
    85  				telemetryClient.Send()
    86  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    87  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    88  				}
    89  			}
    90  			log.DeferExitHandler(handler)
    91  			defer handler()
    92  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    93  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    94  				splunkClient.Initialize(GeneralConfig.CorrelationID,
    95  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
    96  					GeneralConfig.HookConfig.SplunkConfig.Token,
    97  					GeneralConfig.HookConfig.SplunkConfig.Index,
    98  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
    99  			}
   100  			shellExecute(stepConfig, &stepTelemetryData)
   101  			stepTelemetryData.ErrorCode = "0"
   102  			log.Entry().Info("SUCCESS")
   103  		},
   104  	}
   105  
   106  	addShellExecuteFlags(createShellExecuteCmd, &stepConfig)
   107  	return createShellExecuteCmd
   108  }
   109  
   110  func addShellExecuteFlags(cmd *cobra.Command, stepConfig *shellExecuteOptions) {
   111  	cmd.Flags().StringSliceVar(&stepConfig.Sources, "sources", []string{}, "Scripts names for execution or links to scripts")
   112  
   113  }
   114  
   115  // retrieve step metadata
   116  func shellExecuteMetadata() config.StepData {
   117  	var theMetaData = config.StepData{
   118  		Metadata: config.StepMetadata{
   119  			Name:        "shellExecute",
   120  			Aliases:     []config.Alias{},
   121  			Description: "Step executes defined script",
   122  		},
   123  		Spec: config.StepSpec{
   124  			Inputs: config.StepInputs{
   125  				Parameters: []config.StepParameters{
   126  					{
   127  						Name:        "sources",
   128  						ResourceRef: []config.ResourceReference{},
   129  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   130  						Type:        "[]string",
   131  						Mandatory:   false,
   132  						Aliases:     []config.Alias{},
   133  						Default:     []string{},
   134  					},
   135  				},
   136  			},
   137  			Containers: []config.Container{
   138  				{Name: "shell", Image: "node:lts-stretch", WorkingDir: "/home/node"},
   139  			},
   140  		},
   141  	}
   142  	return theMetaData
   143  }