diff --git a/shop/config.go b/shop/config.go index bca0a21d..568cd80a 100644 --- a/shop/config.go +++ b/shop/config.go @@ -1,6 +1,7 @@ package shop import ( + "dario.cat/mergo" "fmt" "os" "strings" @@ -12,11 +13,12 @@ import ( ) type Config struct { - URL string `yaml:"url"` - Build *ConfigBuild `yaml:"build,omitempty"` - AdminApi *ConfigAdminApi `yaml:"admin_api,omitempty"` - ConfigDump *ConfigDump `yaml:"dump,omitempty"` - Sync *ConfigSync `yaml:"sync,omitempty"` + AdditionalConfigs []string `yaml:"include,omitempty"` + URL string `yaml:"url"` + Build *ConfigBuild `yaml:"build,omitempty"` + AdminApi *ConfigAdminApi `yaml:"admin_api,omitempty"` + ConfigDump *ConfigDump `yaml:"dump,omitempty"` + Sync *ConfigSync `yaml:"sync,omitempty"` } type ConfigBuild struct { @@ -89,7 +91,7 @@ func ReadConfig(fileName string, allowFallback bool) (*Config, error) { return fillEmptyConfig(config), nil } - return nil, fmt.Errorf("cannot find .shopware-project.yml, use shopware-cli project config init to create one") + return nil, fmt.Errorf("cannot find project configuration file \"%s\", use shopware-cli project config init to create one", fileName) } if err != nil { @@ -104,6 +106,22 @@ func ReadConfig(fileName string, allowFallback bool) (*Config, error) { substitutedConfig := os.ExpandEnv(string(fileHandle)) err = yaml.Unmarshal([]byte(substitutedConfig), &config) + if len(config.AdditionalConfigs) > 0 { + for _, additionalConfigFile := range config.AdditionalConfigs { + additionalConfig, err := ReadConfig(additionalConfigFile, allowFallback) + if err != nil { + return nil, fmt.Errorf("error while reading included config: %s", err.Error()) + } + + err = mergo.Merge(additionalConfig, config, mergo.WithOverride, mergo.WithSliceDeepCopy) + if err != nil { + return nil, fmt.Errorf("error while merging included config: %s", err.Error()) + } + + config = additionalConfig + } + } + if err != nil { return nil, fmt.Errorf("ReadConfig: %v", err) } diff --git a/wiki/docs/shopware-project-yml-schema.md b/wiki/docs/shopware-project-yml-schema.md index 9b3892e8..a378fdd3 100644 --- a/wiki/docs/shopware-project-yml-schema.md +++ b/wiki/docs/shopware-project-yml-schema.md @@ -88,6 +88,57 @@ sync: taxRate: 19 ``` +## Advanced usage + +### Configuration includes + +You can include one or more `.shopware-project.yml` files to reuse or override configurations. This is useful when you have multiple projects with the same configuration. You can also use this to create a base configuration for your stages or teams and extend it for your own needs. +This also can be used to toggle specific plugin configurations for different stages e.g. enabling/disabling the Paypal sandbox mode depending on the environment. + +Parent `.shopware-project.base.yml`: + +```yaml +url: 'http://localhost' +admin_api: + # For integration use this both fields + client_id: 'client id' + client_secret: 'client secret' +``` + +Child `.shopware-project.dev.yml`: + +```yaml +include: + - '.shopware-project.base.yml' +url: 'http://dev.localhost.test' +sync: + config: + - settings: + SwagPayPal.settings.sandbox: true +``` + +Child `.shopware-project.prod.yml`: + +```yaml +include: + - '.shopware-project.base.yml' +url: 'http://prod.localhost.test' +sync: + config: + - settings: + SwagPayPal.settings.sandbox: false +``` + +You would apply them using the `--project-config` option: + +```bash +# for development +shopware-cli project --project-config='.shopware-project.dev.yml' config push + +# for production +shopware-cli project --project-config='.shopware-project.prod.yml' config push +``` + ### Environment Variables You can use environment variables in your `.shopware-project.yml` file. This is useful for example when you want to use the same configuration for multiple projects or environments. This can also be useful to apply secrets without committing them in the config directly.