2021-11-19 11:05:39 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/splunk"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/validation"
"github.com/spf13/cobra"
)
type shellExecuteOptions struct {
2022-06-10 16:10:46 +02:00
Sources [ ] string ` json:"sources,omitempty" `
GithubToken string ` json:"githubToken,omitempty" `
ScriptArguments [ ] string ` json:"scriptArguments,omitempty" `
2021-11-19 11:05:39 +02:00
}
// ShellExecuteCommand Step executes defined script
func ShellExecuteCommand ( ) * cobra . Command {
const STEP_NAME = "shellExecute"
metadata := shellExecuteMetadata ( )
var stepConfig shellExecuteOptions
var startTime time . Time
var logCollector * log . CollectorHook
var splunkClient * splunk . Splunk
telemetryClient := & telemetry . Telemetry { }
var createShellExecuteCmd = & cobra . Command {
Use : STEP_NAME ,
Short : "Step executes defined script" ,
2022-03-31 18:18:20 +02:00
Long : ` Step executes defined script provided in the 'sources' parameter ` ,
2021-11-19 11:05:39 +02:00
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
startTime = time . Now ( )
log . SetStepName ( STEP_NAME )
log . SetVerbose ( GeneralConfig . Verbose )
GeneralConfig . GitHubAccessTokens = ResolveAccessTokens ( GeneralConfig . GitHubTokens )
path , _ := os . Getwd ( )
fatalHook := & log . FatalHook { CorrelationID : GeneralConfig . CorrelationID , Path : path }
log . RegisterHook ( fatalHook )
err := PrepareConfig ( cmd , & metadata , STEP_NAME , & stepConfig , config . OpenPiperFile )
if err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
2022-03-31 18:18:20 +02:00
log . RegisterSecret ( stepConfig . GithubToken )
2021-11-19 11:05:39 +02:00
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunkClient = & splunk . Splunk { }
logCollector = & log . CollectorHook { CorrelationID : GeneralConfig . CorrelationID }
log . RegisterHook ( logCollector )
}
validation , err := validation . New ( validation . WithJSONNamesForStructFields ( ) , validation . WithPredefinedErrorMessages ( ) )
if err != nil {
return err
}
if err = validation . ValidateStruct ( stepConfig ) ; err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
return nil
} ,
Run : func ( _ * cobra . Command , _ [ ] string ) {
stepTelemetryData := telemetry . CustomData { }
stepTelemetryData . ErrorCode = "1"
handler := func ( ) {
config . RemoveVaultSecretFiles ( )
stepTelemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
stepTelemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
stepTelemetryData . PiperCommitHash = GitCommit
telemetryClient . SetData ( & stepTelemetryData )
telemetryClient . Send ( )
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunkClient . Send ( telemetryClient . GetData ( ) , logCollector )
}
}
log . DeferExitHandler ( handler )
defer handler ( )
telemetryClient . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunkClient . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
}
shellExecute ( stepConfig , & stepTelemetryData )
stepTelemetryData . ErrorCode = "0"
log . Entry ( ) . Info ( "SUCCESS" )
} ,
}
addShellExecuteFlags ( createShellExecuteCmd , & stepConfig )
return createShellExecuteCmd
}
func addShellExecuteFlags ( cmd * cobra . Command , stepConfig * shellExecuteOptions ) {
2022-03-31 18:18:20 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . Sources , "sources" , [ ] string { } , "Scripts paths that must be present in the current workspace or https links to scripts. Only https urls from github are allowed and must be in the format :https://{githubBaseurl}/api/v3/repos/{owner}/{repository}/contents/{path to script} Authentication for the download is only supported via the 'githubToken' param. Make sure the script has the necessary execute permissions." )
cmd . Flags ( ) . StringVar ( & stepConfig . GithubToken , "githubToken" , os . Getenv ( "PIPER_githubToken" ) , "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line" )
2022-06-10 16:10:46 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . ScriptArguments , "scriptArguments" , [ ] string { } , "scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param. For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list" )
2021-11-19 11:05:39 +02:00
}
// retrieve step metadata
func shellExecuteMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
Name : "shellExecute" ,
Aliases : [ ] config . Alias { } ,
Description : "Step executes defined script" ,
} ,
Spec : config . StepSpec {
Inputs : config . StepInputs {
2022-03-31 18:18:20 +02:00
Secrets : [ ] config . StepSecrets {
{ Name : "githubTokenCredentialsId" , Description : "Jenkins credentials ID containing the github token." , Type : "jenkins" } ,
} ,
2021-11-19 11:05:39 +02:00
Parameters : [ ] config . StepParameters {
{
Name : "sources" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : [ ] string { } ,
} ,
2022-03-31 18:18:20 +02:00
{
Name : "githubToken" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "githubTokenCredentialsId" ,
Type : "secret" ,
} ,
{
Name : "githubVaultSecretName" ,
Type : "vaultSecret" ,
Default : "github" ,
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { { Name : "access_token" } } ,
Default : os . Getenv ( "PIPER_githubToken" ) ,
} ,
2022-06-10 16:10:46 +02:00
{
Name : "scriptArguments" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : [ ] string { } ,
} ,
2021-11-19 11:05:39 +02:00
} ,
} ,
2021-12-13 13:31:31 +02:00
Containers : [ ] config . Container {
{ Name : "shell" , Image : "node:lts-stretch" , WorkingDir : "/home/node" } ,
} ,
2021-11-19 11:05:39 +02:00
} ,
}
return theMetaData
}