You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
Export general configuration - part 2 (#957)
* Export general configuration - part 2 First part in #956 missed to export the elements of the struct ... * Add comment for exported struct * Add function for opening config files
This commit is contained in:
@@ -24,7 +24,7 @@ var configOptions configCommandOptions
|
|||||||
// ConfigCommand is the entry command for loading the configuration of a pipeline step
|
// ConfigCommand is the entry command for loading the configuration of a pipeline step
|
||||||
func ConfigCommand() *cobra.Command {
|
func ConfigCommand() *cobra.Command {
|
||||||
|
|
||||||
configOptions.openFile = openPiperFile
|
configOptions.openFile = OpenPiperFile
|
||||||
var createConfigCmd = &cobra.Command{
|
var createConfigCmd = &cobra.Command{
|
||||||
Use: "getConfig",
|
Use: "getConfig",
|
||||||
Short: "Loads the project 'Piper' configuration respecting defaults and parameters.",
|
Short: "Loads the project 'Piper' configuration respecting defaults and parameters.",
|
||||||
@@ -54,8 +54,8 @@ func generateConfig() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var customConfig io.ReadCloser
|
var customConfig io.ReadCloser
|
||||||
if fileExists(GeneralConfig.customConfig) {
|
if fileExists(GeneralConfig.CustomConfig) {
|
||||||
customConfig, err = configOptions.openFile(GeneralConfig.customConfig)
|
customConfig, err = configOptions.openFile(GeneralConfig.CustomConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "config: open failed")
|
return errors.Wrap(err, "config: open failed")
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ func generateConfig() error {
|
|||||||
return errors.Wrap(err, "defaults: retrieving step defaults failed")
|
return errors.Wrap(err, "defaults: retrieving step defaults failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range GeneralConfig.defaultConfig {
|
for _, f := range GeneralConfig.DefaultConfig {
|
||||||
fc, err := configOptions.openFile(f)
|
fc, err := configOptions.openFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "config: getting defaults failed: '%v'", f)
|
return errors.Wrapf(err, "config: getting defaults failed: '%v'", f)
|
||||||
@@ -81,7 +81,7 @@ func generateConfig() error {
|
|||||||
params = metadata.Spec.Inputs.Parameters
|
params = metadata.Spec.Inputs.Parameters
|
||||||
}
|
}
|
||||||
|
|
||||||
stepConfig, err = myConfig.GetStepConfig(flags, GeneralConfig.parametersJSON, customConfig, defaultConfig, paramFilter, params, GeneralConfig.stageName, configOptions.stepName)
|
stepConfig, err = myConfig.GetStepConfig(flags, GeneralConfig.ParametersJSON, customConfig, defaultConfig, paramFilter, params, GeneralConfig.StageName, configOptions.stepName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "getting step config failed")
|
return errors.Wrap(err, "getting step config failed")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ The result looks like
|
|||||||
`,
|
`,
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log.SetStepName("githubPublishRelease")
|
log.SetStepName("githubPublishRelease")
|
||||||
log.SetVerbose(GeneralConfig.verbose)
|
log.SetVerbose(GeneralConfig.Verbose)
|
||||||
return PrepareConfig(cmd, &metadata, "githubPublishRelease", &myGithubPublishReleaseOptions, openPiperFile)
|
return PrepareConfig(cmd, &metadata, "githubPublishRelease", &myGithubPublishReleaseOptions, OpenPiperFile)
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return githubPublishRelease(myGithubPublishReleaseOptions)
|
return githubPublishRelease(myGithubPublishReleaseOptions)
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ In the Docker network, the containers can be referenced by the values provided i
|
|||||||
In a Kubernetes environment, the containers both need to be referenced with ` + "`" + `localhost` + "`" + `.`,
|
In a Kubernetes environment, the containers both need to be referenced with ` + "`" + `localhost` + "`" + `.`,
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log.SetStepName("karmaExecuteTests")
|
log.SetStepName("karmaExecuteTests")
|
||||||
log.SetVerbose(GeneralConfig.verbose)
|
log.SetVerbose(GeneralConfig.Verbose)
|
||||||
return PrepareConfig(cmd, &metadata, "karmaExecuteTests", &myKarmaExecuteTestsOptions, openPiperFile)
|
return PrepareConfig(cmd, &metadata, "karmaExecuteTests", &myKarmaExecuteTestsOptions, OpenPiperFile)
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return karmaExecuteTests(myKarmaExecuteTestsOptions)
|
return karmaExecuteTests(myKarmaExecuteTestsOptions)
|
||||||
|
|||||||
46
cmd/piper.go
46
cmd/piper.go
@@ -12,15 +12,16 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type generalConfigOptions struct {
|
// GeneralConfigOptions contains all global configuration options for piper binary
|
||||||
customConfig string
|
type GeneralConfigOptions struct {
|
||||||
defaultConfig []string //ordered list of Piper default configurations. Can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
CustomConfig string
|
||||||
parametersJSON string
|
DefaultConfig []string //ordered list of Piper default configurations. Can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
||||||
stageName string
|
ParametersJSON string
|
||||||
stepConfigJSON string
|
StageName string
|
||||||
stepMetadata string //metadata to be considered, can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
StepConfigJSON string
|
||||||
stepName string
|
StepMetadata string //metadata to be considered, can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
||||||
verbose bool
|
StepName string
|
||||||
|
Verbose bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
@@ -34,7 +35,7 @@ It contains many steps which can be used within CI/CD systems as well as directl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GeneralConfig contains global configuration flags for piper binary
|
// GeneralConfig contains global configuration flags for piper binary
|
||||||
var GeneralConfig generalConfigOptions
|
var GeneralConfig GeneralConfigOptions
|
||||||
|
|
||||||
// Execute is the starting point of the piper command line tool
|
// Execute is the starting point of the piper command line tool
|
||||||
func Execute() {
|
func Execute() {
|
||||||
@@ -53,12 +54,12 @@ func Execute() {
|
|||||||
|
|
||||||
func addRootFlags(rootCmd *cobra.Command) {
|
func addRootFlags(rootCmd *cobra.Command) {
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVar(&GeneralConfig.customConfig, "customConfig", ".pipeline/config.yml", "Path to the pipeline configuration file")
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.CustomConfig, "customConfig", ".pipeline/config.yml", "Path to the pipeline configuration file")
|
||||||
rootCmd.PersistentFlags().StringSliceVar(&GeneralConfig.defaultConfig, "defaultConfig", nil, "Default configurations, passed as path to yaml file")
|
rootCmd.PersistentFlags().StringSliceVar(&GeneralConfig.DefaultConfig, "defaultConfig", nil, "Default configurations, passed as path to yaml file")
|
||||||
rootCmd.PersistentFlags().StringVar(&GeneralConfig.parametersJSON, "parametersJSON", os.Getenv("PIPER_parametersJSON"), "Parameters to be considered in JSON format")
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.ParametersJSON, "parametersJSON", os.Getenv("PIPER_parametersJSON"), "Parameters to be considered in JSON format")
|
||||||
rootCmd.PersistentFlags().StringVar(&GeneralConfig.stageName, "stageName", os.Getenv("STAGE_NAME"), "Name of the stage for which configuration should be included")
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.StageName, "stageName", os.Getenv("STAGE_NAME"), "Name of the stage for which configuration should be included")
|
||||||
rootCmd.PersistentFlags().StringVar(&GeneralConfig.stepConfigJSON, "stepConfigJSON", os.Getenv("PIPER_stepConfigJSON"), "Step configuration in JSON format")
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.StepConfigJSON, "stepConfigJSON", os.Getenv("PIPER_stepConfigJSON"), "Step configuration in JSON format")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&GeneralConfig.verbose, "verbose", "v", false, "verbose output")
|
rootCmd.PersistentFlags().BoolVarP(&GeneralConfig.Verbose, "verbose", "v", false, "verbose output")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,23 +73,23 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin
|
|||||||
var myConfig config.Config
|
var myConfig config.Config
|
||||||
var stepConfig config.StepConfig
|
var stepConfig config.StepConfig
|
||||||
|
|
||||||
if len(GeneralConfig.stepConfigJSON) != 0 {
|
if len(GeneralConfig.StepConfigJSON) != 0 {
|
||||||
// ignore config & defaults in favor of passed stepConfigJSON
|
// ignore config & defaults in favor of passed stepConfigJSON
|
||||||
stepConfig = config.GetStepConfigWithJSON(flagValues, GeneralConfig.stepConfigJSON, filters)
|
stepConfig = config.GetStepConfigWithJSON(flagValues, GeneralConfig.StepConfigJSON, filters)
|
||||||
} else {
|
} else {
|
||||||
// use config & defaults
|
// use config & defaults
|
||||||
|
|
||||||
//accept that config file and defaults cannot be loaded since both are not mandatory here
|
//accept that config file and defaults cannot be loaded since both are not mandatory here
|
||||||
customConfig, _ := openFile(GeneralConfig.customConfig)
|
customConfig, _ := openFile(GeneralConfig.CustomConfig)
|
||||||
var defaultConfig []io.ReadCloser
|
var defaultConfig []io.ReadCloser
|
||||||
for _, f := range GeneralConfig.defaultConfig {
|
for _, f := range GeneralConfig.DefaultConfig {
|
||||||
//ToDo: support also https as source
|
//ToDo: support also https as source
|
||||||
fc, _ := openFile(f)
|
fc, _ := openFile(f)
|
||||||
defaultConfig = append(defaultConfig, fc)
|
defaultConfig = append(defaultConfig, fc)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
stepConfig, err = myConfig.GetStepConfig(flagValues, GeneralConfig.parametersJSON, customConfig, defaultConfig, filters, metadata.Spec.Inputs.Parameters, GeneralConfig.stageName, stepName)
|
stepConfig, err = myConfig.GetStepConfig(flagValues, GeneralConfig.ParametersJSON, customConfig, defaultConfig, filters, metadata.Spec.Inputs.Parameters, GeneralConfig.StageName, stepName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "retrieving step configuration failed")
|
return errors.Wrap(err, "retrieving step configuration failed")
|
||||||
}
|
}
|
||||||
@@ -102,7 +103,8 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func openPiperFile(name string) (io.ReadCloser, error) {
|
// OpenPiperFile provides functionality to retrieve configuration via file or http
|
||||||
|
func OpenPiperFile(name string) (io.ReadCloser, error) {
|
||||||
//ToDo: support also https as source
|
//ToDo: support also https as source
|
||||||
if !strings.HasPrefix(name, "http") {
|
if !strings.HasPrefix(name, "http") {
|
||||||
return os.Open(name)
|
return os.Open(name)
|
||||||
|
|||||||
@@ -81,14 +81,14 @@ func TestAddRootFlags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrepareConfig(t *testing.T) {
|
func TestPrepareConfig(t *testing.T) {
|
||||||
defaultsBak := GeneralConfig.defaultConfig
|
defaultsBak := GeneralConfig.DefaultConfig
|
||||||
GeneralConfig.defaultConfig = []string{"testDefaults.yml"}
|
GeneralConfig.DefaultConfig = []string{"testDefaults.yml"}
|
||||||
defer func() { GeneralConfig.defaultConfig = defaultsBak }()
|
defer func() { GeneralConfig.DefaultConfig = defaultsBak }()
|
||||||
|
|
||||||
t.Run("using stepConfigJSON", func(t *testing.T) {
|
t.Run("using stepConfigJSON", func(t *testing.T) {
|
||||||
stepConfigJSONBak := GeneralConfig.stepConfigJSON
|
stepConfigJSONBak := GeneralConfig.StepConfigJSON
|
||||||
GeneralConfig.stepConfigJSON = `{"testParam": "testValueJSON"}`
|
GeneralConfig.StepConfigJSON = `{"testParam": "testValueJSON"}`
|
||||||
defer func() { GeneralConfig.stepConfigJSON = stepConfigJSONBak }()
|
defer func() { GeneralConfig.StepConfigJSON = stepConfigJSONBak }()
|
||||||
testOptions := stepOptions{}
|
testOptions := stepOptions{}
|
||||||
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
|
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
|
||||||
testCmd.Flags().StringVar(&testOptions.TestParam, "testParam", "", "test usage")
|
testCmd.Flags().StringVar(&testOptions.TestParam, "testParam", "", "test usage")
|
||||||
@@ -136,7 +136,7 @@ func TestPrepareConfig(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("error case", func(t *testing.T) {
|
t.Run("error case", func(t *testing.T) {
|
||||||
GeneralConfig.defaultConfig = []string{"testDefaultsInvalid.yml"}
|
GeneralConfig.DefaultConfig = []string{"testDefaultsInvalid.yml"}
|
||||||
testOptions := stepOptions{}
|
testOptions := stepOptions{}
|
||||||
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
|
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
|
||||||
metadata := config.StepData{}
|
metadata := config.StepData{}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ func VersionCommand() *cobra.Command {
|
|||||||
Long: `Writes the commit hash and the tag (if any) to stdout and exits with 0.`,
|
Long: `Writes the commit hash and the tag (if any) to stdout and exits with 0.`,
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log.SetStepName("version")
|
log.SetStepName("version")
|
||||||
log.SetVerbose(GeneralConfig.verbose)
|
log.SetVerbose(GeneralConfig.Verbose)
|
||||||
return PrepareConfig(cmd, &metadata, "version", &myVersionOptions, openPiperFile)
|
return PrepareConfig(cmd, &metadata, "version", &myVersionOptions, OpenPiperFile)
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return version(myVersionOptions)
|
return version(myVersionOptions)
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ func {{.CobraCmdFuncName}}() *cobra.Command {
|
|||||||
Long: {{ $tick := "` + "`" + `" }}{{ $tick }}{{.Long | longName }}{{ $tick }},
|
Long: {{ $tick := "` + "`" + `" }}{{ $tick }}{{.Long | longName }}{{ $tick }},
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log.SetStepName("{{ .StepName }}")
|
log.SetStepName("{{ .StepName }}")
|
||||||
log.SetVerbose(GeneralConfig.verbose)
|
log.SetVerbose(GeneralConfig.Verbose)
|
||||||
return PrepareConfig(cmd, &metadata, "{{ .StepName }}", &my{{ .StepName | title}}Options, openPiperFile)
|
return PrepareConfig(cmd, &metadata, "{{ .StepName }}", &my{{ .StepName | title}}Options, OpenPiperFile)
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return {{.StepName}}(my{{ .StepName | title }}Options)
|
return {{.StepName}}(my{{ .StepName | title }}Options)
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ func TestStepCommand() *cobra.Command {
|
|||||||
Long: `Long Test description`,
|
Long: `Long Test description`,
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log.SetStepName("testStep")
|
log.SetStepName("testStep")
|
||||||
log.SetVerbose(GeneralConfig.verbose)
|
log.SetVerbose(GeneralConfig.Verbose)
|
||||||
return PrepareConfig(cmd, &metadata, "testStep", &myTestStepOptions, openPiperFile)
|
return PrepareConfig(cmd, &metadata, "testStep", &myTestStepOptions, OpenPiperFile)
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return testStep(myTestStepOptions)
|
return testStep(myTestStepOptions)
|
||||||
|
|||||||
Reference in New Issue
Block a user