github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/jsonApplyPatch_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 jsonApplyPatchOptions struct {
    19  	Input  string `json:"input,omitempty"`
    20  	Patch  string `json:"patch,omitempty"`
    21  	Output string `json:"output,omitempty"`
    22  }
    23  
    24  // JsonApplyPatchCommand Patches a json with a patch file
    25  func JsonApplyPatchCommand() *cobra.Command {
    26  	const STEP_NAME = "jsonApplyPatch"
    27  
    28  	metadata := jsonApplyPatchMetadata()
    29  	var stepConfig jsonApplyPatchOptions
    30  	var startTime time.Time
    31  	var logCollector *log.CollectorHook
    32  	var splunkClient *splunk.Splunk
    33  	telemetryClient := &telemetry.Telemetry{}
    34  
    35  	var createJsonApplyPatchCmd = &cobra.Command{
    36  		Use:   STEP_NAME,
    37  		Short: "Patches a json with a patch file",
    38  		Long: `This steps patches a json file with patch file using the json patch standard.
    39  This step can, e.g., be used if there is a json schema which needs to be patched.`,
    40  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    41  			startTime = time.Now()
    42  			log.SetStepName(STEP_NAME)
    43  			log.SetVerbose(GeneralConfig.Verbose)
    44  
    45  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    46  
    47  			path, _ := os.Getwd()
    48  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    49  			log.RegisterHook(fatalHook)
    50  
    51  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    52  			if err != nil {
    53  				log.SetErrorCategory(log.ErrorConfiguration)
    54  				return err
    55  			}
    56  
    57  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    58  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    59  				log.RegisterHook(&sentryHook)
    60  			}
    61  
    62  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    63  				splunkClient = &splunk.Splunk{}
    64  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    65  				log.RegisterHook(logCollector)
    66  			}
    67  
    68  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    69  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    70  			}
    71  
    72  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    73  			if err != nil {
    74  				return err
    75  			}
    76  			if err = validation.ValidateStruct(stepConfig); err != nil {
    77  				log.SetErrorCategory(log.ErrorConfiguration)
    78  				return err
    79  			}
    80  
    81  			return nil
    82  		},
    83  		Run: func(_ *cobra.Command, _ []string) {
    84  			stepTelemetryData := telemetry.CustomData{}
    85  			stepTelemetryData.ErrorCode = "1"
    86  			handler := func() {
    87  				config.RemoveVaultSecretFiles()
    88  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    89  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    90  				stepTelemetryData.PiperCommitHash = GitCommit
    91  				telemetryClient.SetData(&stepTelemetryData)
    92  				telemetryClient.Send()
    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  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   100  				}
   101  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   102  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   103  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   104  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   105  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   106  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   107  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   108  				}
   109  			}
   110  			log.DeferExitHandler(handler)
   111  			defer handler()
   112  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   113  			jsonApplyPatch(stepConfig, &stepTelemetryData)
   114  			stepTelemetryData.ErrorCode = "0"
   115  			log.Entry().Info("SUCCESS")
   116  		},
   117  	}
   118  
   119  	addJsonApplyPatchFlags(createJsonApplyPatchCmd, &stepConfig)
   120  	return createJsonApplyPatchCmd
   121  }
   122  
   123  func addJsonApplyPatchFlags(cmd *cobra.Command, stepConfig *jsonApplyPatchOptions) {
   124  	cmd.Flags().StringVar(&stepConfig.Input, "input", os.Getenv("PIPER_input"), "File path to the json file which schould be patched.")
   125  	cmd.Flags().StringVar(&stepConfig.Patch, "patch", os.Getenv("PIPER_patch"), "File path to the patch which should be applied to the json file.")
   126  	cmd.Flags().StringVar(&stepConfig.Output, "output", os.Getenv("PIPER_output"), "File path to destination of the patched json file.")
   127  
   128  	cmd.MarkFlagRequired("input")
   129  	cmd.MarkFlagRequired("patch")
   130  	cmd.MarkFlagRequired("output")
   131  }
   132  
   133  // retrieve step metadata
   134  func jsonApplyPatchMetadata() config.StepData {
   135  	var theMetaData = config.StepData{
   136  		Metadata: config.StepMetadata{
   137  			Name:        "jsonApplyPatch",
   138  			Aliases:     []config.Alias{},
   139  			Description: "Patches a json with a patch file",
   140  		},
   141  		Spec: config.StepSpec{
   142  			Inputs: config.StepInputs{
   143  				Parameters: []config.StepParameters{
   144  					{
   145  						Name:        "input",
   146  						ResourceRef: []config.ResourceReference{},
   147  						Scope:       []string{"PARAMETERS"},
   148  						Type:        "string",
   149  						Mandatory:   true,
   150  						Aliases:     []config.Alias{},
   151  						Default:     os.Getenv("PIPER_input"),
   152  					},
   153  					{
   154  						Name:        "patch",
   155  						ResourceRef: []config.ResourceReference{},
   156  						Scope:       []string{"PARAMETERS"},
   157  						Type:        "string",
   158  						Mandatory:   true,
   159  						Aliases:     []config.Alias{},
   160  						Default:     os.Getenv("PIPER_patch"),
   161  					},
   162  					{
   163  						Name:        "output",
   164  						ResourceRef: []config.ResourceReference{},
   165  						Scope:       []string{"PARAMETERS"},
   166  						Type:        "string",
   167  						Mandatory:   true,
   168  						Aliases:     []config.Alias{},
   169  						Default:     os.Getenv("PIPER_output"),
   170  					},
   171  				},
   172  			},
   173  		},
   174  	}
   175  	return theMetaData
   176  }