mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +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:
parent
186204b37e
commit
ede322c8bb
@ -24,7 +24,7 @@ var configOptions configCommandOptions
|
||||
// ConfigCommand is the entry command for loading the configuration of a pipeline step
|
||||
func ConfigCommand() *cobra.Command {
|
||||
|
||||
configOptions.openFile = openPiperFile
|
||||
configOptions.openFile = OpenPiperFile
|
||||
var createConfigCmd = &cobra.Command{
|
||||
Use: "getConfig",
|
||||
Short: "Loads the project 'Piper' configuration respecting defaults and parameters.",
|
||||
@ -54,8 +54,8 @@ func generateConfig() error {
|
||||
}
|
||||
|
||||
var customConfig io.ReadCloser
|
||||
if fileExists(GeneralConfig.customConfig) {
|
||||
customConfig, err = configOptions.openFile(GeneralConfig.customConfig)
|
||||
if fileExists(GeneralConfig.CustomConfig) {
|
||||
customConfig, err = configOptions.openFile(GeneralConfig.CustomConfig)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "config: open failed")
|
||||
}
|
||||
@ -66,7 +66,7 @@ func generateConfig() error {
|
||||
return errors.Wrap(err, "defaults: retrieving step defaults failed")
|
||||
}
|
||||
|
||||
for _, f := range GeneralConfig.defaultConfig {
|
||||
for _, f := range GeneralConfig.DefaultConfig {
|
||||
fc, err := configOptions.openFile(f)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "config: getting defaults failed: '%v'", f)
|
||||
@ -81,7 +81,7 @@ func generateConfig() error {
|
||||
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 {
|
||||
return errors.Wrap(err, "getting step config failed")
|
||||
}
|
||||
|
@ -47,8 +47,8 @@ The result looks like
|
||||
![Example release](../images/githubRelease.png)`,
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.SetStepName("githubPublishRelease")
|
||||
log.SetVerbose(GeneralConfig.verbose)
|
||||
return PrepareConfig(cmd, &metadata, "githubPublishRelease", &myGithubPublishReleaseOptions, openPiperFile)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
return PrepareConfig(cmd, &metadata, "githubPublishRelease", &myGithubPublishReleaseOptions, OpenPiperFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
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` + "`" + `.`,
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.SetStepName("karmaExecuteTests")
|
||||
log.SetVerbose(GeneralConfig.verbose)
|
||||
return PrepareConfig(cmd, &metadata, "karmaExecuteTests", &myKarmaExecuteTestsOptions, openPiperFile)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
return PrepareConfig(cmd, &metadata, "karmaExecuteTests", &myKarmaExecuteTestsOptions, OpenPiperFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return karmaExecuteTests(myKarmaExecuteTestsOptions)
|
||||
|
46
cmd/piper.go
46
cmd/piper.go
@ -12,15 +12,16 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type generalConfigOptions struct {
|
||||
customConfig string
|
||||
defaultConfig []string //ordered list of Piper default configurations. Can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
||||
parametersJSON string
|
||||
stageName string
|
||||
stepConfigJSON string
|
||||
stepMetadata string //metadata to be considered, can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
||||
stepName string
|
||||
verbose bool
|
||||
// GeneralConfigOptions contains all global configuration options for piper binary
|
||||
type GeneralConfigOptions struct {
|
||||
CustomConfig string
|
||||
DefaultConfig []string //ordered list of Piper default configurations. Can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
||||
ParametersJSON string
|
||||
StageName string
|
||||
StepConfigJSON string
|
||||
StepMetadata string //metadata to be considered, can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
||||
StepName string
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
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
|
||||
var GeneralConfig generalConfigOptions
|
||||
var GeneralConfig GeneralConfigOptions
|
||||
|
||||
// Execute is the starting point of the piper command line tool
|
||||
func Execute() {
|
||||
@ -53,12 +54,12 @@ func Execute() {
|
||||
|
||||
func addRootFlags(rootCmd *cobra.Command) {
|
||||
|
||||
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().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.stepConfigJSON, "stepConfigJSON", os.Getenv("PIPER_stepConfigJSON"), "Step configuration in JSON format")
|
||||
rootCmd.PersistentFlags().BoolVarP(&GeneralConfig.verbose, "verbose", "v", false, "verbose output")
|
||||
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().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.StepConfigJSON, "stepConfigJSON", os.Getenv("PIPER_stepConfigJSON"), "Step configuration in JSON format")
|
||||
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 stepConfig config.StepConfig
|
||||
|
||||
if len(GeneralConfig.stepConfigJSON) != 0 {
|
||||
if len(GeneralConfig.StepConfigJSON) != 0 {
|
||||
// ignore config & defaults in favor of passed stepConfigJSON
|
||||
stepConfig = config.GetStepConfigWithJSON(flagValues, GeneralConfig.stepConfigJSON, filters)
|
||||
stepConfig = config.GetStepConfigWithJSON(flagValues, GeneralConfig.StepConfigJSON, filters)
|
||||
} else {
|
||||
// use config & defaults
|
||||
|
||||
//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
|
||||
for _, f := range GeneralConfig.defaultConfig {
|
||||
for _, f := range GeneralConfig.DefaultConfig {
|
||||
//ToDo: support also https as source
|
||||
fc, _ := openFile(f)
|
||||
defaultConfig = append(defaultConfig, fc)
|
||||
}
|
||||
|
||||
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 {
|
||||
return errors.Wrap(err, "retrieving step configuration failed")
|
||||
}
|
||||
@ -102,7 +103,8 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin
|
||||
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
|
||||
if !strings.HasPrefix(name, "http") {
|
||||
return os.Open(name)
|
||||
|
@ -81,14 +81,14 @@ func TestAddRootFlags(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPrepareConfig(t *testing.T) {
|
||||
defaultsBak := GeneralConfig.defaultConfig
|
||||
GeneralConfig.defaultConfig = []string{"testDefaults.yml"}
|
||||
defer func() { GeneralConfig.defaultConfig = defaultsBak }()
|
||||
defaultsBak := GeneralConfig.DefaultConfig
|
||||
GeneralConfig.DefaultConfig = []string{"testDefaults.yml"}
|
||||
defer func() { GeneralConfig.DefaultConfig = defaultsBak }()
|
||||
|
||||
t.Run("using stepConfigJSON", func(t *testing.T) {
|
||||
stepConfigJSONBak := GeneralConfig.stepConfigJSON
|
||||
GeneralConfig.stepConfigJSON = `{"testParam": "testValueJSON"}`
|
||||
defer func() { GeneralConfig.stepConfigJSON = stepConfigJSONBak }()
|
||||
stepConfigJSONBak := GeneralConfig.StepConfigJSON
|
||||
GeneralConfig.StepConfigJSON = `{"testParam": "testValueJSON"}`
|
||||
defer func() { GeneralConfig.StepConfigJSON = stepConfigJSONBak }()
|
||||
testOptions := stepOptions{}
|
||||
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
|
||||
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) {
|
||||
GeneralConfig.defaultConfig = []string{"testDefaultsInvalid.yml"}
|
||||
GeneralConfig.DefaultConfig = []string{"testDefaultsInvalid.yml"}
|
||||
testOptions := stepOptions{}
|
||||
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
|
||||
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.`,
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.SetStepName("version")
|
||||
log.SetVerbose(GeneralConfig.verbose)
|
||||
return PrepareConfig(cmd, &metadata, "version", &myVersionOptions, openPiperFile)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
return PrepareConfig(cmd, &metadata, "version", &myVersionOptions, OpenPiperFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return version(myVersionOptions)
|
||||
|
@ -54,8 +54,8 @@ func {{.CobraCmdFuncName}}() *cobra.Command {
|
||||
Long: {{ $tick := "` + "`" + `" }}{{ $tick }}{{.Long | longName }}{{ $tick }},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.SetStepName("{{ .StepName }}")
|
||||
log.SetVerbose(GeneralConfig.verbose)
|
||||
return PrepareConfig(cmd, &metadata, "{{ .StepName }}", &my{{ .StepName | title}}Options, openPiperFile)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
return PrepareConfig(cmd, &metadata, "{{ .StepName }}", &my{{ .StepName | title}}Options, OpenPiperFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return {{.StepName}}(my{{ .StepName | title }}Options)
|
||||
|
@ -26,8 +26,8 @@ func TestStepCommand() *cobra.Command {
|
||||
Long: `Long Test description`,
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.SetStepName("testStep")
|
||||
log.SetVerbose(GeneralConfig.verbose)
|
||||
return PrepareConfig(cmd, &metadata, "testStep", &myTestStepOptions, openPiperFile)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
return PrepareConfig(cmd, &metadata, "testStep", &myTestStepOptions, OpenPiperFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return testStep(myTestStepOptions)
|
||||
|
Loading…
Reference in New Issue
Block a user