2019-11-08 16:35:11 +02:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-05-25 19:48:59 +02:00
|
|
|
"reflect"
|
2019-11-08 16:35:11 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2020-05-25 19:48:59 +02:00
|
|
|
"github.com/Masterminds/sprig"
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
2020-01-15 13:16:25 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2019-11-08 16:35:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepInfo struct {
|
|
|
|
CobraCmdFuncName string
|
|
|
|
CreateCmdVar string
|
|
|
|
ExportPrefix string
|
|
|
|
FlagsFunc string
|
|
|
|
Long string
|
2020-03-19 18:24:35 +02:00
|
|
|
StepParameters []config.StepParameters
|
|
|
|
StepAliases []config.Alias
|
2019-11-08 16:35:11 +02:00
|
|
|
OSImport bool
|
2020-01-15 13:16:25 +02:00
|
|
|
OutputResources []map[string]string
|
2019-11-08 16:35:11 +02:00
|
|
|
Short string
|
|
|
|
StepFunc string
|
|
|
|
StepName string
|
2020-04-16 14:37:45 +02:00
|
|
|
StepSecrets []string
|
2020-11-20 09:13:59 +02:00
|
|
|
Containers []config.Container
|
|
|
|
Sidecars []config.Container
|
|
|
|
Outputs config.StepOutputs
|
2021-03-01 14:03:42 +02:00
|
|
|
Resources []config.StepResources
|
2021-06-16 08:43:30 +02:00
|
|
|
Secrets []config.StepSecrets
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//StepGoTemplate ...
|
2020-02-21 13:56:09 +02:00
|
|
|
const stepGoTemplate = `// Code generated by piper's step-generator. DO NOT EDIT.
|
|
|
|
|
|
|
|
package cmd
|
2019-11-08 16:35:11 +02:00
|
|
|
|
|
|
|
import (
|
2020-01-31 15:06:08 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
{{ if .OutputResources -}}
|
|
|
|
"path/filepath"
|
|
|
|
{{ end -}}
|
|
|
|
"time"
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-01-31 15:06:08 +02:00
|
|
|
{{ if .ExportPrefix -}}
|
|
|
|
{{ .ExportPrefix }} "github.com/SAP/jenkins-library/cmd"
|
|
|
|
{{ end -}}
|
2019-11-08 16:35:11 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2020-01-31 15:06:08 +02:00
|
|
|
{{ if .OutputResources -}}
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperenv"
|
|
|
|
{{ end -}}
|
2020-01-29 14:17:54 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2021-05-17 12:14:04 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/splunk"
|
2019-11-08 16:35:11 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type {{ .StepName }}Options struct {
|
2020-05-25 19:48:59 +02:00
|
|
|
{{- $names := list ""}}
|
|
|
|
{{- range $key, $value := uniqueName .StepParameters }}
|
|
|
|
{{ if ne (has $value.Name $names) true -}}
|
|
|
|
{{ $names | last }}{{ $value.Name | golangName }} {{ $value.Type }} ` + "`json:\"{{$value.Name}},omitempty\"`" + `
|
|
|
|
{{- else -}}
|
|
|
|
{{- $names = append $names $value.Name }} {{ end -}}
|
|
|
|
{{ end }}
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
{{ range $notused, $oRes := .OutputResources }}
|
|
|
|
{{ index $oRes "def"}}
|
|
|
|
{{ end }}
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
// {{.CobraCmdFuncName}} {{.Short}}
|
|
|
|
func {{.CobraCmdFuncName}}() *cobra.Command {
|
2020-04-28 07:42:02 +02:00
|
|
|
const STEP_NAME = "{{ .StepName }}"
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
metadata := {{ .StepName }}Metadata()
|
2020-02-04 11:46:43 +02:00
|
|
|
var stepConfig {{.StepName}}Options
|
2020-01-31 15:06:08 +02:00
|
|
|
var startTime time.Time
|
2020-01-15 13:16:25 +02:00
|
|
|
{{- range $notused, $oRes := .OutputResources }}
|
|
|
|
var {{ index $oRes "name" }} {{ index $oRes "objectname" }}{{ end }}
|
2021-05-17 12:14:04 +02:00
|
|
|
var logCollector *log.CollectorHook
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
var {{.CreateCmdVar}} = &cobra.Command{
|
2020-04-28 07:42:02 +02:00
|
|
|
Use: STEP_NAME,
|
2019-11-08 16:35:11 +02:00
|
|
|
Short: "{{.Short}}",
|
|
|
|
Long: {{ $tick := "` + "`" + `" }}{{ $tick }}{{.Long | longName }}{{ $tick }},
|
2020-06-23 18:05:21 +02:00
|
|
|
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
2020-01-31 15:06:08 +02:00
|
|
|
startTime = time.Now()
|
2020-04-28 07:42:02 +02:00
|
|
|
log.SetStepName(STEP_NAME)
|
2019-11-08 16:35:11 +02:00
|
|
|
log.SetVerbose({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.Verbose)
|
2020-04-28 07:42:02 +02:00
|
|
|
|
|
|
|
path, _ := os.Getwd()
|
2020-05-08 10:41:01 +02:00
|
|
|
fatalHook := &log.FatalHook{CorrelationID: {{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.CorrelationID, Path: path}
|
2020-04-28 07:42:02 +02:00
|
|
|
log.RegisterHook(fatalHook)
|
|
|
|
|
|
|
|
err := {{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
|
2020-04-16 14:37:45 +02:00
|
|
|
if err != nil {
|
2020-06-24 10:04:05 +02:00
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
2020-04-16 14:37:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
{{- range $key, $value := .StepSecrets }}
|
|
|
|
log.RegisterSecret(stepConfig.{{ $value | golangName }}){{end}}
|
2020-05-05 08:36:24 +02:00
|
|
|
|
2020-05-08 10:41:01 +02:00
|
|
|
if len({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
|
|
|
|
sentryHook := log.NewSentryHook({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SentryConfig.Dsn, {{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.CorrelationID)
|
2020-05-05 08:36:24 +02:00
|
|
|
log.RegisterHook(&sentryHook)
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:14:04 +02:00
|
|
|
if len({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
|
|
|
logCollector = &log.CollectorHook{CorrelationID: {{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.CorrelationID}
|
|
|
|
log.RegisterHook(logCollector)
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:37:45 +02:00
|
|
|
return nil
|
2019-11-08 16:35:11 +02:00
|
|
|
},
|
2020-06-23 18:05:21 +02:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
telemetryData.ErrorCode = "1"
|
2020-01-15 13:16:25 +02:00
|
|
|
handler := func() {
|
2020-10-26 15:20:04 +02:00
|
|
|
config.RemoveVaultSecretFiles()
|
2020-01-15 13:16:25 +02:00
|
|
|
{{- range $notused, $oRes := .OutputResources }}
|
2020-01-31 16:39:45 +02:00
|
|
|
{{ index $oRes "name" }}.persist({{if $.ExportPrefix}}{{ $.ExportPrefix }}.{{end}}GeneralConfig.EnvRootPath, "{{ index $oRes "name" }}"){{ end }}
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
2020-09-29 13:49:40 +02:00
|
|
|
telemetryData.ErrorCategory = log.GetErrorCategory().String()
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetry.Send(&telemetryData)
|
2021-05-17 12:14:04 +02:00
|
|
|
if len({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
|
|
|
splunk.Send(&telemetryData, logCollector)
|
|
|
|
}
|
2020-01-15 13:16:25 +02:00
|
|
|
}
|
|
|
|
log.DeferExitHandler(handler)
|
|
|
|
defer handler()
|
2020-04-28 07:42:02 +02:00
|
|
|
telemetry.Initialize({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.NoTelemetry, STEP_NAME)
|
2021-05-17 12:14:04 +02:00
|
|
|
if len({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
|
|
|
splunk.Initialize({{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.CorrelationID,
|
|
|
|
{{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.Dsn,
|
|
|
|
{{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.Token,
|
|
|
|
{{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.Index,
|
|
|
|
{{if .ExportPrefix}}{{ .ExportPrefix }}.{{end}}GeneralConfig.HookConfig.SplunkConfig.SendLogs)
|
|
|
|
}
|
2020-02-04 11:46:43 +02:00
|
|
|
{{.StepName}}(stepConfig, &telemetryData{{ range $notused, $oRes := .OutputResources}}, &{{ index $oRes "name" }}{{ end }})
|
2020-01-31 15:06:08 +02:00
|
|
|
telemetryData.ErrorCode = "0"
|
2020-05-18 21:52:54 +02:00
|
|
|
log.Entry().Info("SUCCESS")
|
2019-11-08 16:35:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
{{.FlagsFunc}}({{.CreateCmdVar}}, &stepConfig)
|
2019-11-08 16:35:11 +02:00
|
|
|
return {{.CreateCmdVar}}
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
func {{.FlagsFunc}}(cmd *cobra.Command, stepConfig *{{.StepName}}Options) {
|
2020-05-25 19:48:59 +02:00
|
|
|
{{- range $key, $value := uniqueName .StepParameters }}
|
2020-11-23 08:55:20 +02:00
|
|
|
{{ if isCLIParam $value.Type }}cmd.Flags().{{ $value.Type | flagType }}(&stepConfig.{{ $value.Name | golangName }}, "{{ $value.Name }}", {{ $value.Default }}, "{{ $value.Description }}"){{end}}{{ end }}
|
2019-11-08 16:35:11 +02:00
|
|
|
{{- printf "\n" }}
|
2020-03-19 18:24:35 +02:00
|
|
|
{{- range $key, $value := .StepParameters }}{{ if $value.Mandatory }}
|
2019-11-08 16:35:11 +02:00
|
|
|
cmd.MarkFlagRequired("{{ $value.Name }}"){{ end }}{{ end }}
|
|
|
|
}
|
|
|
|
|
2020-09-16 14:50:09 +02:00
|
|
|
{{ define "resourceRefs"}}
|
|
|
|
{{ "{" }}
|
|
|
|
Name: "{{- .Name }}",
|
|
|
|
{{- if .Param }}
|
|
|
|
Param: "{{ .Param }}",
|
|
|
|
{{- end }}
|
|
|
|
{{- if gt (len .Paths) 0 }}
|
|
|
|
Paths: []string{{ "{" }}{{ range $_, $path := .Paths }}"{{$path}}",{{ end }}{{"}"}},
|
|
|
|
{{- end }}
|
|
|
|
{{- if .Type }}
|
|
|
|
Type: "{{ .Type }}",
|
|
|
|
{{- end }}
|
|
|
|
{{ "}" }},
|
|
|
|
{{- nindent 24 ""}}
|
|
|
|
{{- end -}}
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
// retrieve step metadata
|
|
|
|
func {{ .StepName }}Metadata() config.StepData {
|
|
|
|
var theMetaData = config.StepData{
|
2020-03-19 18:24:35 +02:00
|
|
|
Metadata: config.StepMetadata{
|
|
|
|
Name: "{{ .StepName }}",
|
|
|
|
Aliases: []config.Alias{{ "{" }}{{ range $notused, $alias := .StepAliases }}{{ "{" }}Name: "{{ $alias.Name }}", Deprecated: {{ $alias.Deprecated }}{{ "}" }},{{ end }}{{ "}" }},
|
2020-11-20 09:13:59 +02:00
|
|
|
Description: "{{ .Short }}",
|
2020-03-19 18:24:35 +02:00
|
|
|
},
|
2019-11-08 16:35:11 +02:00
|
|
|
Spec: config.StepSpec{
|
|
|
|
Inputs: config.StepInputs{
|
2021-06-16 08:43:30 +02:00
|
|
|
{{ if .Secrets -}}
|
|
|
|
Secrets: []config.StepSecrets{
|
|
|
|
{{- range $secrets := .Secrets }}
|
|
|
|
{
|
|
|
|
{{- if $secrets.Name -}} Name: "{{$secrets.Name}}",{{- end }}
|
|
|
|
{{- if $secrets.Description -}} Description: "{{$secrets.Description}}",{{- end }}
|
|
|
|
{{- if $secrets.Type -}} Type: "{{$secrets.Type}}",{{- end }}
|
|
|
|
{{- if $secrets.Aliases -}} Aliases: []config.Alias{ {{- range $i, $a := $secrets.Aliases }} {Name: "{{$a.Name}}", Deprecated: {{$a.Deprecated}}}, {{ end -}} },{{- end }}
|
|
|
|
}, {{ end }}
|
|
|
|
},
|
|
|
|
{{ end -}}
|
2021-03-01 14:03:42 +02:00
|
|
|
{{ if .Resources -}}
|
|
|
|
Resources: []config.StepResources{
|
|
|
|
{{- range $resource := .Resources }}
|
|
|
|
{
|
|
|
|
{{- if $resource.Name -}} Name: "{{$resource.Name}}",{{- end }}
|
|
|
|
{{- if $resource.Description -}} Description: "{{$resource.Description}}",{{- end }}
|
|
|
|
{{- if $resource.Type -}} Type: "{{$resource.Type}}",{{- end }}
|
|
|
|
{{- if $resource.Conditions -}} Conditions: []config.Condition{ {{- range $i, $cond := $resource.Conditions }} {ConditionRef: "{{$cond.ConditionRef}}", Params: []config.Param{ {{- range $j, $p := $cond.Params}} { Name: "{{$p.Name}}", Value: "{{$p.Value}}" }, {{end -}} } }, {{ end -}} },{{ end }}
|
|
|
|
},{{- end }}
|
|
|
|
},
|
|
|
|
{{ end -}}
|
2019-11-08 16:35:11 +02:00
|
|
|
Parameters: []config.StepParameters{
|
2020-03-19 18:24:35 +02:00
|
|
|
{{- range $key, $value := .StepParameters }}
|
2019-11-08 16:35:11 +02:00
|
|
|
{
|
|
|
|
Name: "{{ $value.Name }}",
|
2020-09-16 14:50:09 +02:00
|
|
|
ResourceRef: []config.ResourceReference{{ "{" }}{{ range $notused, $ref := $value.ResourceRef }}{{ template "resourceRefs" $ref }}{{ end }}{{ "}" }},
|
2019-11-08 16:35:11 +02:00
|
|
|
Scope: []string{{ "{" }}{{ range $notused, $scope := $value.Scope }}"{{ $scope }}",{{ end }}{{ "}" }},
|
|
|
|
Type: "{{ $value.Type }}",
|
|
|
|
Mandatory: {{ $value.Mandatory }},
|
2019-11-21 17:34:22 +02:00
|
|
|
Aliases: []config.Alias{{ "{" }}{{ range $notused, $alias := $value.Aliases }}{{ "{" }}Name: "{{ $alias.Name }}"{{ "}" }},{{ end }}{{ "}" }},
|
2021-06-16 08:43:30 +02:00
|
|
|
{{ if $value.Default -}} Default: {{ $value.Default }}, {{- end}}{{ if $value.Conditions }}
|
|
|
|
Conditions: []config.Condition{ {{- range $i, $cond := $value.Conditions }} {ConditionRef: "{{$cond.ConditionRef}}", Params: []config.Param{ {{- range $j, $p := $cond.Params}} { Name: "{{$p.Name}}", Value: "{{$p.Value}}" }, {{end -}} } }, {{ end -}} },{{- end }}
|
2019-11-08 16:35:11 +02:00
|
|
|
},{{ end }}
|
|
|
|
},
|
|
|
|
},
|
2020-11-20 09:13:59 +02:00
|
|
|
{{ if .Containers -}}
|
|
|
|
Containers: []config.Container{
|
|
|
|
{{- range $container := .Containers }}
|
|
|
|
{
|
|
|
|
{{- if $container.Name -}} Name: "{{$container.Name}}",{{- end }}
|
|
|
|
{{- if $container.Image -}} Image: "{{$container.Image}}",{{- end }}
|
|
|
|
{{- if $container.EnvVars -}} EnvVars: []config.EnvVar{ {{- range $i, $env := $container.EnvVars }} {Name: "{{$env.Name}}", Value: "{{$env.Value}}"}, {{ end -}} },{{- end }}
|
|
|
|
{{- if $container.WorkingDir -}} WorkingDir: "{{$container.WorkingDir}}",{{- end }}
|
|
|
|
{{- if $container.Options -}} Options: []config.Option{ {{- range $i, $option := $container.Options }} {Name: "{{$option.Name}}", Value: "{{$option.Value}}"}, {{ end -}} },{{ end }}
|
|
|
|
{{- if $container.Conditions -}} Conditions: []config.Condition{ {{- range $i, $cond := $container.Conditions }} {ConditionRef: "{{$cond.ConditionRef}}", Params: []config.Param{ {{- range $j, $p := $cond.Params}} { Name: "{{$p.Name}}", Value: "{{$p.Value}}" }, {{end -}} } }, {{ end -}} },{{ end }}
|
|
|
|
}, {{ end }}
|
|
|
|
},
|
|
|
|
{{ end -}}
|
|
|
|
{{ if .Sidecars -}}
|
|
|
|
Sidecars: []config.Container{
|
|
|
|
{{- range $container := .Sidecars }}
|
|
|
|
{
|
|
|
|
{{- if $container.Name -}} Name: "{{$container.Name}}", {{- end }}
|
|
|
|
{{- if $container.Image -}} Image: "{{$container.Image}}", {{- end }}
|
|
|
|
{{- if $container.EnvVars -}} EnvVars: []config.EnvVar{ {{- range $i, $env := $container.EnvVars }} {Name: "{{$env.Name}}", Value: "{{$env.Value}}"}, {{ end -}} }, {{- end }}
|
|
|
|
{{- if $container.WorkingDir -}} WorkingDir: "{{$container.WorkingDir}}", {{- end }}
|
|
|
|
{{- if $container.Options -}} Options: []config.Option{ {{- range $i, $option := $container.Options }} {Name: "{{$option.Name}}", Value: "{{$option.Value}}"}, {{ end -}} }, {{- end }}
|
|
|
|
{{- if $container.Conditions -}} Conditions: []config.Condition{ {{- range $i, $cond := $container.Conditions }} {ConditionRef: "{{$cond.ConditionRef}}", Params: []config.Param{ {{- range $j, $p := $cond.Params}} { Name: "{{$p.Name}}", Value: "{{$p.Value}}" }, {{end -}} } }, {{ end -}} }, {{- end }}
|
|
|
|
}, {{ end }}
|
|
|
|
},
|
|
|
|
{{ end -}}
|
|
|
|
{{- if .Outputs.Resources -}}
|
|
|
|
Outputs: config.StepOutputs{
|
|
|
|
Resources: []config.StepResources{
|
|
|
|
{{- range $res := .Outputs.Resources }}
|
|
|
|
{
|
|
|
|
{{ if $res.Name }}Name: "{{$res.Name}}", {{- end }}
|
|
|
|
{{ if $res.Type }}Type: "{{$res.Type}}", {{- end }}
|
|
|
|
{{ if $res.Parameters }}Parameters: []map[string]interface{}{ {{- end -}}
|
|
|
|
{{ range $i, $p := $res.Parameters }}
|
|
|
|
{{ if $p.name}}{"Name": "{{$p.name}}"},{{ end -}}
|
|
|
|
{{ if $p.fields}}{"fields": []map[string]string{ {{- range $j, $f := $p.fields}} {"name": "{{$f.name}}"}, {{end -}} } },{{ end -}}
|
|
|
|
{{ if $p.tags}}{"tags": []map[string]string{ {{- range $j, $t := $p.tags}} {"name": "{{$t.name}}"}, {{end -}} } },{{ end -}}
|
|
|
|
{{ end }}
|
|
|
|
{{ if $res.Parameters -}} }, {{- end }}
|
2021-03-01 14:03:42 +02:00
|
|
|
{{- if $res.Conditions -}} Conditions: []config.Condition{ {{- range $i, $cond := $res.Conditions }} {ConditionRef: "{{$cond.ConditionRef}}", Params: []config.Param{ {{- range $j, $p := $cond.Params}} { Name: "{{$p.Name}}", Value: "{{$p.Value}}" }, {{end -}} } }, {{ end -}} },{{ end }}
|
2020-11-20 09:13:59 +02:00
|
|
|
}, {{- end }}
|
|
|
|
},
|
|
|
|
}, {{- end }}
|
2019-11-08 16:35:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return theMetaData
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
//StepTestGoTemplate ...
|
|
|
|
const stepTestGoTemplate = `package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test{{.CobraCmdFuncName}}(t *testing.T) {
|
2020-12-22 10:30:54 +02:00
|
|
|
t.Parallel()
|
2019-11-08 16:35:11 +02:00
|
|
|
|
|
|
|
testCmd := {{.CobraCmdFuncName}}()
|
|
|
|
|
2020-09-10 14:37:46 +02:00
|
|
|
// only high level testing performed - details are tested in step generation procedure
|
2020-04-28 07:42:02 +02:00
|
|
|
assert.Equal(t, "{{ .StepName }}", testCmd.Use, "command name incorrect")
|
2019-11-08 16:35:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
const stepGoImplementationTemplate = `package cmd
|
|
|
|
import (
|
2020-10-29 18:29:07 +02:00
|
|
|
"fmt"
|
2020-02-17 14:30:30 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
2020-01-15 13:16:25 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2020-02-04 11:46:43 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2020-10-29 18:29:07 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2020-01-15 13:16:25 +02:00
|
|
|
)
|
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
type {{.StepName}}Utils interface {
|
|
|
|
command.ExecRunner
|
|
|
|
|
|
|
|
FileExists(filename string) (bool, error)
|
|
|
|
|
|
|
|
// Add more methods here, or embed additional interfaces, or remove/replace as required.
|
|
|
|
// The {{.StepName}}Utils interface should be descriptive of your runtime dependencies,
|
|
|
|
// i.e. include everything you need to be able to mock in tests.
|
|
|
|
// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
|
|
|
|
}
|
|
|
|
|
|
|
|
type {{.StepName}}UtilsBundle struct {
|
|
|
|
*command.Command
|
|
|
|
*piperutils.Files
|
|
|
|
|
|
|
|
// Embed more structs as necessary to implement methods or interfaces you add to {{.StepName}}Utils.
|
|
|
|
// Structs embedded in this way must each have a unique set of methods attached.
|
|
|
|
// If there is no struct which implements the method you need, attach the method to
|
|
|
|
// {{.StepName}}UtilsBundle and forward to the implementation of the dependency.
|
|
|
|
}
|
|
|
|
|
|
|
|
func new{{.StepName | title}}Utils() {{.StepName}}Utils {
|
|
|
|
utils := {{.StepName}}UtilsBundle{
|
|
|
|
Command: &command.Command{},
|
|
|
|
Files: &piperutils.Files{},
|
|
|
|
}
|
|
|
|
// Reroute command output to logging framework
|
|
|
|
utils.Stdout(log.Writer())
|
|
|
|
utils.Stderr(log.Writer())
|
|
|
|
return &utils
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:30:30 +02:00
|
|
|
func {{.StepName}}(config {{ .StepName }}Options, telemetryData *telemetry.CustomData{{ range $notused, $oRes := .OutputResources}}, {{ index $oRes "name" }} *{{ index $oRes "objectname" }}{{ end }}) {
|
2020-10-29 18:29:07 +02:00
|
|
|
// Utils can be used wherever the command.ExecRunner interface is expected.
|
|
|
|
// It can also be used for example as a mavenExecRunner.
|
|
|
|
utils := new{{.StepName | title}}Utils()
|
2020-02-17 14:30:30 +02:00
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
// For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
2020-02-17 14:30:30 +02:00
|
|
|
// and use a &piperhttp.Client{} in a custom system
|
|
|
|
// Example: step checkmarxExecuteScan.go
|
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
// Error situations should be bubbled up until they reach the line below which will then stop execution
|
|
|
|
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
|
|
|
|
err := run{{.StepName | title}}(&config, telemetryData, utils{{ range $notused, $oRes := .OutputResources}}, &{{ index $oRes "name" }}{{ end }})
|
2020-02-17 14:30:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
func run{{.StepName | title}}(config *{{ .StepName }}Options, telemetryData *telemetry.CustomData, utils {{.StepName}}Utils{{ range $notused, $oRes := .OutputResources}}, {{ index $oRes "name" }} *{{ index $oRes "objectname" }} {{ end }}) error {
|
2020-02-17 14:30:30 +02:00
|
|
|
log.Entry().WithField("LogField", "Log field content").Info("This is just a demo for a simple step.")
|
2020-10-29 18:29:07 +02:00
|
|
|
|
|
|
|
// Example of calling methods from external dependencies directly on utils:
|
|
|
|
exists, err := utils.FileExists("file.txt")
|
|
|
|
if err != nil {
|
|
|
|
// It is good practice to set an error category.
|
|
|
|
// Most likely you want to do this at the place where enough context is known.
|
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
|
|
// Always wrap non-descriptive errors to enrich them with context for when they appear in the log:
|
|
|
|
return fmt.Errorf("failed to check for important file: %w", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
|
|
return fmt.Errorf("cannot run without important file")
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
const stepGoImplementationTestTemplate = `package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type {{.StepName}}MockUtils struct {
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
*mock.FilesMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func new{{.StepName | title}}TestsUtils() {{.StepName}}MockUtils {
|
|
|
|
utils := {{.StepName}}MockUtils{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
|
|
|
return utils
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun{{.StepName | title}}(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("happy path", func(t *testing.T) {
|
2020-12-22 10:30:54 +02:00
|
|
|
t.Parallel()
|
2020-10-29 18:29:07 +02:00
|
|
|
// init
|
|
|
|
config := {{.StepName}}Options{}
|
|
|
|
|
|
|
|
utils := new{{.StepName | title}}TestsUtils()
|
|
|
|
utils.AddFile("file.txt", []byte("dummy content"))
|
|
|
|
|
|
|
|
// test
|
|
|
|
err := run{{.StepName | title}}(&config, nil, utils)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error path", func(t *testing.T) {
|
2020-12-22 10:30:54 +02:00
|
|
|
t.Parallel()
|
2020-10-29 18:29:07 +02:00
|
|
|
// init
|
|
|
|
config := {{.StepName}}Options{}
|
|
|
|
|
|
|
|
utils := new{{.StepName | title}}TestsUtils()
|
|
|
|
|
|
|
|
// test
|
|
|
|
err := run{{.StepName | title}}(&config, nil, utils)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.EqualError(t, err, "cannot run without important file")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2020-11-20 09:13:59 +02:00
|
|
|
const metadataGeneratedFileName = "metadata_generated.go"
|
|
|
|
const metadataGeneratedTemplate = `// Code generated by piper's step-generator. DO NOT EDIT.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import "github.com/SAP/jenkins-library/pkg/config"
|
|
|
|
|
|
|
|
// GetStepMetadata return a map with all the step metadata mapped to their stepName
|
|
|
|
func GetAllStepMetadata() map[string]config.StepData {
|
|
|
|
return map[string]config.StepData{
|
|
|
|
{{range $stepName := .Steps }} "{{$stepName}}": {{$stepName}}Metadata(),
|
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
// ProcessMetaFiles generates step coding based on step configuration provided in yaml files
|
2020-09-23 13:55:17 +02:00
|
|
|
func ProcessMetaFiles(metadataFiles []string, targetDir string, stepHelperData StepHelperData) error {
|
2020-07-17 08:06:11 +02:00
|
|
|
|
2020-11-20 09:13:59 +02:00
|
|
|
allSteps := struct{ Steps []string }{}
|
2019-11-08 16:35:11 +02:00
|
|
|
for key := range metadataFiles {
|
|
|
|
|
|
|
|
var stepData config.StepData
|
|
|
|
|
|
|
|
configFilePath := metadataFiles[key]
|
|
|
|
|
2019-11-21 12:12:30 +02:00
|
|
|
metadataFile, err := stepHelperData.OpenFile(configFilePath)
|
2019-11-08 16:35:11 +02:00
|
|
|
checkError(err)
|
|
|
|
defer metadataFile.Close()
|
|
|
|
|
|
|
|
fmt.Printf("Reading file %v\n", configFilePath)
|
|
|
|
|
|
|
|
err = stepData.ReadPipelineStepData(metadataFile)
|
|
|
|
checkError(err)
|
|
|
|
|
2020-11-20 09:13:59 +02:00
|
|
|
stepName := stepData.Metadata.Name
|
|
|
|
fmt.Printf("Step name: %v\n", stepName)
|
|
|
|
allSteps.Steps = append(allSteps.Steps, stepName)
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-09-23 13:55:17 +02:00
|
|
|
osImport := false
|
|
|
|
osImport, err = setDefaultParameters(&stepData)
|
|
|
|
checkError(err)
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-09-23 13:55:17 +02:00
|
|
|
myStepInfo, err := getStepInfo(&stepData, osImport, stepHelperData.ExportPrefix)
|
|
|
|
checkError(err)
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
step := stepTemplate(myStepInfo, "step", stepGoTemplate)
|
2020-11-20 09:13:59 +02:00
|
|
|
err = stepHelperData.WriteFile(filepath.Join(targetDir, fmt.Sprintf("%v_generated.go", stepName)), step, 0644)
|
2020-09-23 13:55:17 +02:00
|
|
|
checkError(err)
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
test := stepTemplate(myStepInfo, "stepTest", stepTestGoTemplate)
|
2020-11-20 09:13:59 +02:00
|
|
|
err = stepHelperData.WriteFile(filepath.Join(targetDir, fmt.Sprintf("%v_generated_test.go", stepName)), test, 0644)
|
2020-09-23 13:55:17 +02:00
|
|
|
checkError(err)
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2020-11-20 09:13:59 +02:00
|
|
|
exists, _ := piperutils.FileExists(filepath.Join(targetDir, fmt.Sprintf("%v.go", stepName)))
|
2020-09-23 13:55:17 +02:00
|
|
|
if !exists {
|
2020-10-29 18:29:07 +02:00
|
|
|
impl := stepImplementation(myStepInfo, "impl", stepGoImplementationTemplate)
|
2020-11-20 09:13:59 +02:00
|
|
|
err = stepHelperData.WriteFile(filepath.Join(targetDir, fmt.Sprintf("%v.go", stepName)), impl, 0644)
|
2020-09-23 13:55:17 +02:00
|
|
|
checkError(err)
|
2019-11-12 13:56:04 +02:00
|
|
|
}
|
2020-10-29 18:29:07 +02:00
|
|
|
|
2020-11-20 09:13:59 +02:00
|
|
|
exists, _ = piperutils.FileExists(filepath.Join(targetDir, fmt.Sprintf("%v_test.go", stepName)))
|
2020-10-29 18:29:07 +02:00
|
|
|
if !exists {
|
|
|
|
impl := stepImplementation(myStepInfo, "implTest", stepGoImplementationTestTemplate)
|
2020-11-20 09:13:59 +02:00
|
|
|
err = stepHelperData.WriteFile(filepath.Join(targetDir, fmt.Sprintf("%v_test.go", stepName)), impl, 0644)
|
2020-10-29 18:29:07 +02:00
|
|
|
checkError(err)
|
|
|
|
}
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
2020-11-20 09:13:59 +02:00
|
|
|
// expose metadata functions
|
|
|
|
code := generateCode(allSteps, "metadata", metadataGeneratedTemplate, nil)
|
|
|
|
err := stepHelperData.WriteFile(filepath.Join(targetDir, metadataGeneratedFileName), code, 0644)
|
|
|
|
checkError(err)
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openMetaFile(name string) (io.ReadCloser, error) {
|
|
|
|
return os.Open(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileWriter(filename string, data []byte, perm os.FileMode) error {
|
|
|
|
return ioutil.WriteFile(filename, data, perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDefaultParameters(stepData *config.StepData) (bool, error) {
|
|
|
|
//ToDo: custom function for default handling, support all relevant parameter types
|
|
|
|
osImportRequired := false
|
|
|
|
for k, param := range stepData.Spec.Inputs.Parameters {
|
|
|
|
|
|
|
|
if param.Default == nil {
|
|
|
|
switch param.Type {
|
|
|
|
case "bool":
|
|
|
|
// ToDo: Check if default should be read from env
|
|
|
|
param.Default = "false"
|
2020-01-24 15:30:27 +02:00
|
|
|
case "int":
|
|
|
|
param.Default = "0"
|
|
|
|
case "string":
|
|
|
|
param.Default = fmt.Sprintf("os.Getenv(\"PIPER_%v\")", param.Name)
|
|
|
|
osImportRequired = true
|
2019-11-08 16:35:11 +02:00
|
|
|
case "[]string":
|
|
|
|
// ToDo: Check if default should be read from env
|
|
|
|
param.Default = "[]string{}"
|
2020-11-23 08:55:20 +02:00
|
|
|
case "map[string]interface{}":
|
|
|
|
// Currently we don't need to set a default here since in this case the default
|
|
|
|
// is never used. Needs to be changed in case we enable cli parameter handling
|
|
|
|
// for that type.
|
2019-11-08 16:35:11 +02:00
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("Meta data type not set or not known: '%v'", param.Type)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch param.Type {
|
|
|
|
case "bool":
|
|
|
|
boolVal := "false"
|
|
|
|
if param.Default.(bool) == true {
|
|
|
|
boolVal = "true"
|
|
|
|
}
|
|
|
|
param.Default = boolVal
|
2020-01-24 15:30:27 +02:00
|
|
|
case "int":
|
|
|
|
param.Default = fmt.Sprintf("%v", param.Default)
|
|
|
|
case "string":
|
2020-05-25 19:48:59 +02:00
|
|
|
param.Default = fmt.Sprintf("`%v`", param.Default)
|
2019-11-08 16:35:11 +02:00
|
|
|
case "[]string":
|
2020-05-25 19:48:59 +02:00
|
|
|
param.Default = fmt.Sprintf("[]string{`%v`}", strings.Join(getStringSliceFromInterface(param.Default), "`, `"))
|
2020-11-23 08:55:20 +02:00
|
|
|
case "map[string]interface{}":
|
|
|
|
// Currently we don't need to set a default here since in this case the default
|
|
|
|
// is never used. Needs to be changed in case we enable cli parameter handling
|
|
|
|
// for that type.
|
2019-11-08 16:35:11 +02:00
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("Meta data type not set or not known: '%v'", param.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stepData.Spec.Inputs.Parameters[k] = param
|
|
|
|
}
|
|
|
|
return osImportRequired, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
func getStepInfo(stepData *config.StepData, osImport bool, exportPrefix string) (stepInfo, error) {
|
|
|
|
oRes, err := getOutputResourceDetails(stepData)
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
return stepInfo{
|
2020-01-15 13:16:25 +02:00
|
|
|
StepName: stepData.Metadata.Name,
|
|
|
|
CobraCmdFuncName: fmt.Sprintf("%vCommand", strings.Title(stepData.Metadata.Name)),
|
|
|
|
CreateCmdVar: fmt.Sprintf("create%vCmd", strings.Title(stepData.Metadata.Name)),
|
|
|
|
Short: stepData.Metadata.Description,
|
|
|
|
Long: stepData.Metadata.LongDescription,
|
2020-03-19 18:24:35 +02:00
|
|
|
StepParameters: stepData.Spec.Inputs.Parameters,
|
|
|
|
StepAliases: stepData.Metadata.Aliases,
|
2020-01-15 13:16:25 +02:00
|
|
|
FlagsFunc: fmt.Sprintf("add%vFlags", strings.Title(stepData.Metadata.Name)),
|
|
|
|
OSImport: osImport,
|
|
|
|
OutputResources: oRes,
|
|
|
|
ExportPrefix: exportPrefix,
|
2020-04-16 14:37:45 +02:00
|
|
|
StepSecrets: getSecretFields(stepData),
|
2020-11-20 09:13:59 +02:00
|
|
|
Containers: stepData.Spec.Containers,
|
|
|
|
Sidecars: stepData.Spec.Sidecars,
|
|
|
|
Outputs: stepData.Spec.Outputs,
|
2021-03-01 14:03:42 +02:00
|
|
|
Resources: stepData.Spec.Inputs.Resources,
|
2021-06-16 08:43:30 +02:00
|
|
|
Secrets: stepData.Spec.Inputs.Secrets,
|
2020-01-15 13:16:25 +02:00
|
|
|
},
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:37:45 +02:00
|
|
|
func getSecretFields(stepData *config.StepData) []string {
|
|
|
|
var secretFields []string
|
|
|
|
|
|
|
|
for _, parameter := range stepData.Spec.Inputs.Parameters {
|
|
|
|
if parameter.Secret {
|
|
|
|
secretFields = append(secretFields, parameter.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secretFields
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
func getOutputResourceDetails(stepData *config.StepData) ([]map[string]string, error) {
|
|
|
|
outputResources := []map[string]string{}
|
|
|
|
|
|
|
|
for _, res := range stepData.Spec.Outputs.Resources {
|
|
|
|
currentResource := map[string]string{}
|
|
|
|
currentResource["name"] = res.Name
|
|
|
|
|
|
|
|
switch res.Type {
|
|
|
|
case "piperEnvironment":
|
|
|
|
var envResource PiperEnvironmentResource
|
|
|
|
envResource.Name = res.Name
|
|
|
|
envResource.StepName = stepData.Metadata.Name
|
|
|
|
for _, param := range res.Parameters {
|
|
|
|
paramSections := strings.Split(fmt.Sprintf("%v", param["name"]), "/")
|
|
|
|
category := ""
|
|
|
|
name := paramSections[0]
|
|
|
|
if len(paramSections) > 1 {
|
|
|
|
name = strings.Join(paramSections[1:], "_")
|
|
|
|
category = paramSections[0]
|
|
|
|
if !contains(envResource.Categories, category) {
|
|
|
|
envResource.Categories = append(envResource.Categories, category)
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 09:51:58 +02:00
|
|
|
envParam := PiperEnvironmentParameter{Category: category, Name: name, Type: fmt.Sprint(param["type"])}
|
2020-01-15 13:16:25 +02:00
|
|
|
envResource.Parameters = append(envResource.Parameters, envParam)
|
|
|
|
}
|
|
|
|
def, err := envResource.StructString()
|
|
|
|
if err != nil {
|
|
|
|
return outputResources, err
|
|
|
|
}
|
|
|
|
currentResource["def"] = def
|
|
|
|
currentResource["objectname"] = envResource.StructName()
|
|
|
|
outputResources = append(outputResources, currentResource)
|
|
|
|
case "influx":
|
|
|
|
var influxResource InfluxResource
|
|
|
|
influxResource.Name = res.Name
|
|
|
|
influxResource.StepName = stepData.Metadata.Name
|
|
|
|
for _, measurement := range res.Parameters {
|
|
|
|
influxMeasurement := InfluxMeasurement{Name: fmt.Sprintf("%v", measurement["name"])}
|
|
|
|
if fields, ok := measurement["fields"].([]interface{}); ok {
|
|
|
|
for _, field := range fields {
|
|
|
|
if fieldParams, ok := field.(map[string]interface{}); ok {
|
2020-10-13 16:37:48 +02:00
|
|
|
influxMeasurement.Fields = append(influxMeasurement.Fields, InfluxMetric{Name: fmt.Sprintf("%v", fieldParams["name"]), Type: fmt.Sprintf("%v", fieldParams["type"])})
|
2020-01-15 13:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tags, ok := measurement["tags"].([]interface{}); ok {
|
|
|
|
for _, tag := range tags {
|
|
|
|
if tagParams, ok := tag.(map[string]interface{}); ok {
|
|
|
|
influxMeasurement.Tags = append(influxMeasurement.Tags, InfluxMetric{Name: fmt.Sprintf("%v", tagParams["name"])})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
influxResource.Measurements = append(influxResource.Measurements, influxMeasurement)
|
|
|
|
}
|
|
|
|
def, err := influxResource.StructString()
|
|
|
|
if err != nil {
|
|
|
|
return outputResources, err
|
|
|
|
}
|
|
|
|
currentResource["def"] = def
|
|
|
|
currentResource["objectname"] = influxResource.StructName()
|
|
|
|
outputResources = append(outputResources, currentResource)
|
|
|
|
}
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
2020-01-15 13:16:25 +02:00
|
|
|
|
|
|
|
return outputResources, nil
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MetadataFiles provides a list of all step metadata files
|
|
|
|
func MetadataFiles(sourceDirectory string) ([]string, error) {
|
|
|
|
|
|
|
|
var metadataFiles []string
|
|
|
|
|
|
|
|
err := filepath.Walk(sourceDirectory, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if filepath.Ext(path) == ".yaml" {
|
|
|
|
metadataFiles = append(metadataFiles, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return metadataFiles, nil
|
|
|
|
}
|
|
|
|
return metadataFiles, nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 08:55:20 +02:00
|
|
|
func isCLIParam(myType string) bool {
|
|
|
|
return myType != "map[string]interface{}"
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
func stepTemplate(myStepInfo stepInfo, templateName, goTemplate string) []byte {
|
2020-05-25 19:48:59 +02:00
|
|
|
funcMap := sprig.HermeticTxtFuncMap()
|
|
|
|
funcMap["flagType"] = flagType
|
|
|
|
funcMap["golangName"] = golangNameTitle
|
|
|
|
funcMap["title"] = strings.Title
|
|
|
|
funcMap["longName"] = longName
|
|
|
|
funcMap["uniqueName"] = mustUniqName
|
2020-11-23 08:55:20 +02:00
|
|
|
funcMap["isCLIParam"] = isCLIParam
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
return generateCode(myStepInfo, templateName, goTemplate, funcMap)
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
func stepImplementation(myStepInfo stepInfo, templateName, goTemplate string) []byte {
|
2020-05-25 19:48:59 +02:00
|
|
|
funcMap := sprig.HermeticTxtFuncMap()
|
|
|
|
funcMap["title"] = strings.Title
|
|
|
|
funcMap["uniqueName"] = mustUniqName
|
2019-11-08 16:35:11 +02:00
|
|
|
|
2020-10-29 18:29:07 +02:00
|
|
|
return generateCode(myStepInfo, templateName, goTemplate, funcMap)
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 09:13:59 +02:00
|
|
|
func generateCode(dataObject interface{}, templateName, goTemplate string, funcMap template.FuncMap) []byte {
|
2020-10-29 18:29:07 +02:00
|
|
|
tmpl, err := template.New(templateName).Funcs(funcMap).Parse(goTemplate)
|
2020-01-15 13:16:25 +02:00
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
var generatedCode bytes.Buffer
|
2020-11-20 09:13:59 +02:00
|
|
|
err = tmpl.Execute(&generatedCode, dataObject)
|
2020-01-15 13:16:25 +02:00
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
return generatedCode.Bytes()
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
func longName(long string) string {
|
|
|
|
l := strings.ReplaceAll(long, "`", "` + \"`\" + `")
|
|
|
|
l = strings.TrimSpace(l)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:51:58 +02:00
|
|
|
func resourceFieldType(fieldType string) string {
|
2020-10-13 16:37:48 +02:00
|
|
|
//TODO: clarify why fields are initialized with <nil> and tags are initialized with ''
|
|
|
|
if len(fieldType) == 0 || fieldType == "<nil>" {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
return fieldType
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:35:11 +02:00
|
|
|
func golangName(name string) string {
|
|
|
|
properName := strings.Replace(name, "Api", "API", -1)
|
|
|
|
properName = strings.Replace(properName, "api", "API", -1)
|
|
|
|
properName = strings.Replace(properName, "Url", "URL", -1)
|
|
|
|
properName = strings.Replace(properName, "Id", "ID", -1)
|
|
|
|
properName = strings.Replace(properName, "Json", "JSON", -1)
|
|
|
|
properName = strings.Replace(properName, "json", "JSON", -1)
|
2020-03-23 11:38:31 +02:00
|
|
|
properName = strings.Replace(properName, "Tls", "TLS", -1)
|
2020-01-15 13:16:25 +02:00
|
|
|
return properName
|
|
|
|
}
|
|
|
|
|
|
|
|
func golangNameTitle(name string) string {
|
|
|
|
return strings.Title(golangName(name))
|
2019-11-08 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func flagType(paramType string) string {
|
|
|
|
var theFlagType string
|
|
|
|
switch paramType {
|
|
|
|
case "bool":
|
|
|
|
theFlagType = "BoolVar"
|
2020-01-24 15:30:27 +02:00
|
|
|
case "int":
|
|
|
|
theFlagType = "IntVar"
|
2019-11-08 16:35:11 +02:00
|
|
|
case "string":
|
|
|
|
theFlagType = "StringVar"
|
|
|
|
case "[]string":
|
|
|
|
theFlagType = "StringSliceVar"
|
|
|
|
default:
|
|
|
|
fmt.Printf("Meta data type not set or not known: '%v'\n", paramType)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return theFlagType
|
|
|
|
}
|
2019-12-13 11:55:45 +02:00
|
|
|
|
|
|
|
func getStringSliceFromInterface(iSlice interface{}) []string {
|
|
|
|
s := []string{}
|
|
|
|
|
|
|
|
t, ok := iSlice.([]interface{})
|
|
|
|
if ok {
|
|
|
|
for _, v := range t {
|
|
|
|
s = append(s, fmt.Sprintf("%v", v))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s = append(s, fmt.Sprintf("%v", iSlice))
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2020-05-25 19:48:59 +02:00
|
|
|
|
|
|
|
func mustUniqName(list []config.StepParameters) ([]config.StepParameters, error) {
|
|
|
|
tp := reflect.TypeOf(list).Kind()
|
|
|
|
switch tp {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
l2 := reflect.ValueOf(list)
|
|
|
|
|
|
|
|
l := l2.Len()
|
|
|
|
names := []string{}
|
|
|
|
dest := []config.StepParameters{}
|
|
|
|
var item config.StepParameters
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
item = l2.Index(i).Interface().(config.StepParameters)
|
|
|
|
if !piperutils.ContainsString(names, item.Name) {
|
|
|
|
names = append(names, item.Name)
|
|
|
|
dest = append(dest, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Cannot find uniq on type %s", tp)
|
|
|
|
}
|
|
|
|
}
|