github.com/xgoffin/jenkins-library@v1.154.0/cmd/gctsCloneRepository_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 gctsCloneRepositoryOptions struct {
    19  	Username   string `json:"username,omitempty"`
    20  	Password   string `json:"password,omitempty"`
    21  	Repository string `json:"repository,omitempty"`
    22  	Host       string `json:"host,omitempty"`
    23  	Client     string `json:"client,omitempty"`
    24  }
    25  
    26  // GctsCloneRepositoryCommand Clones a Git repository
    27  func GctsCloneRepositoryCommand() *cobra.Command {
    28  	const STEP_NAME = "gctsCloneRepository"
    29  
    30  	metadata := gctsCloneRepositoryMetadata()
    31  	var stepConfig gctsCloneRepositoryOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createGctsCloneRepositoryCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Clones a Git repository",
    40  		Long:  `Clones a Git repository from a remote repository to a local repository on an ABAP system. To be able to execute this step, the corresponding local repository has to exist on the local ABAP system.`,
    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  			log.RegisterSecret(stepConfig.Username)
    58  			log.RegisterSecret(stepConfig.Password)
    59  
    60  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    61  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    62  				log.RegisterHook(&sentryHook)
    63  			}
    64  
    65  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    66  				splunkClient = &splunk.Splunk{}
    67  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    68  				log.RegisterHook(logCollector)
    69  			}
    70  
    71  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    72  			if err != nil {
    73  				return err
    74  			}
    75  			if err = validation.ValidateStruct(stepConfig); err != nil {
    76  				log.SetErrorCategory(log.ErrorConfiguration)
    77  				return err
    78  			}
    79  
    80  			return nil
    81  		},
    82  		Run: func(_ *cobra.Command, _ []string) {
    83  			stepTelemetryData := telemetry.CustomData{}
    84  			stepTelemetryData.ErrorCode = "1"
    85  			handler := func() {
    86  				config.RemoveVaultSecretFiles()
    87  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    88  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    89  				stepTelemetryData.PiperCommitHash = GitCommit
    90  				telemetryClient.SetData(&stepTelemetryData)
    91  				telemetryClient.Send()
    92  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    93  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    94  				}
    95  			}
    96  			log.DeferExitHandler(handler)
    97  			defer handler()
    98  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    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  			}
   106  			gctsCloneRepository(stepConfig, &stepTelemetryData)
   107  			stepTelemetryData.ErrorCode = "0"
   108  			log.Entry().Info("SUCCESS")
   109  		},
   110  	}
   111  
   112  	addGctsCloneRepositoryFlags(createGctsCloneRepositoryCmd, &stepConfig)
   113  	return createGctsCloneRepositoryCmd
   114  }
   115  
   116  func addGctsCloneRepositoryFlags(cmd *cobra.Command, stepConfig *gctsCloneRepositoryOptions) {
   117  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User to authenticate to the ABAP system")
   118  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password to authenticate to the ABAP system")
   119  	cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Specifies the name (ID) of the local repsitory on the ABAP system")
   120  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the protocol and host address, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.")
   121  	cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Specifies the client of the ABAP system to be addressed")
   122  
   123  	cmd.MarkFlagRequired("username")
   124  	cmd.MarkFlagRequired("password")
   125  	cmd.MarkFlagRequired("repository")
   126  	cmd.MarkFlagRequired("host")
   127  	cmd.MarkFlagRequired("client")
   128  }
   129  
   130  // retrieve step metadata
   131  func gctsCloneRepositoryMetadata() config.StepData {
   132  	var theMetaData = config.StepData{
   133  		Metadata: config.StepMetadata{
   134  			Name:        "gctsCloneRepository",
   135  			Aliases:     []config.Alias{},
   136  			Description: "Clones a Git repository",
   137  		},
   138  		Spec: config.StepSpec{
   139  			Inputs: config.StepInputs{
   140  				Secrets: []config.StepSecrets{
   141  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing username and password for authentication to the ABAP system on which you want to clone the repository", Type: "jenkins"},
   142  				},
   143  				Parameters: []config.StepParameters{
   144  					{
   145  						Name: "username",
   146  						ResourceRef: []config.ResourceReference{
   147  							{
   148  								Name:  "abapCredentialsId",
   149  								Param: "username",
   150  								Type:  "secret",
   151  							},
   152  						},
   153  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   154  						Type:      "string",
   155  						Mandatory: true,
   156  						Aliases:   []config.Alias{},
   157  						Default:   os.Getenv("PIPER_username"),
   158  					},
   159  					{
   160  						Name: "password",
   161  						ResourceRef: []config.ResourceReference{
   162  							{
   163  								Name:  "abapCredentialsId",
   164  								Param: "password",
   165  								Type:  "secret",
   166  							},
   167  						},
   168  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   169  						Type:      "string",
   170  						Mandatory: true,
   171  						Aliases:   []config.Alias{},
   172  						Default:   os.Getenv("PIPER_password"),
   173  					},
   174  					{
   175  						Name:        "repository",
   176  						ResourceRef: []config.ResourceReference{},
   177  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   178  						Type:        "string",
   179  						Mandatory:   true,
   180  						Aliases:     []config.Alias{},
   181  						Default:     os.Getenv("PIPER_repository"),
   182  					},
   183  					{
   184  						Name:        "host",
   185  						ResourceRef: []config.ResourceReference{},
   186  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   187  						Type:        "string",
   188  						Mandatory:   true,
   189  						Aliases:     []config.Alias{},
   190  						Default:     os.Getenv("PIPER_host"),
   191  					},
   192  					{
   193  						Name:        "client",
   194  						ResourceRef: []config.ResourceReference{},
   195  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   196  						Type:        "string",
   197  						Mandatory:   true,
   198  						Aliases:     []config.Alias{},
   199  						Default:     os.Getenv("PIPER_client"),
   200  					},
   201  				},
   202  			},
   203  		},
   204  	}
   205  	return theMetaData
   206  }