From a14e097d1de6170d954133382f7976f2be7d9815 Mon Sep 17 00:00:00 2001 From: Julius Noack Date: Mon, 2 Oct 2023 15:44:19 +0200 Subject: [PATCH] feat: change config inheritance to support multiple files via include statement --- shop/config.go | 34 +++++++++++++----------- wiki/docs/shopware-project-yml-schema.md | 13 +++++---- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/shop/config.go b/shop/config.go index 5019241b..568cd80a 100644 --- a/shop/config.go +++ b/shop/config.go @@ -13,12 +13,12 @@ import ( ) type Config struct { - BaseConfig string `yaml:"extends,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"` + 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 { @@ -106,18 +106,20 @@ func ReadConfig(fileName string, allowFallback bool) (*Config, error) { substitutedConfig := os.ExpandEnv(string(fileHandle)) err = yaml.Unmarshal([]byte(substitutedConfig), &config) - if config.BaseConfig != "" { - baseConfig, err := ReadConfig(config.BaseConfig, false) - if err != nil { - return nil, fmt.Errorf("error while reading base config: %s", err.Error()) - } + 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(baseConfig, config, mergo.WithOverride) - if err != nil { - return nil, fmt.Errorf("error while merging base 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 = baseConfig + config = additionalConfig + } } if err != nil { diff --git a/wiki/docs/shopware-project-yml-schema.md b/wiki/docs/shopware-project-yml-schema.md index a7b936ef..a378fdd3 100644 --- a/wiki/docs/shopware-project-yml-schema.md +++ b/wiki/docs/shopware-project-yml-schema.md @@ -7,6 +7,7 @@ Any configuration field is optional. When you create a `.shopware-project.yml`, ```yaml # .shopware-project.yml + # URL to Shopware instance, required for admin api calls (clear cache, sync stuff) url: 'http://localhost' admin_api: @@ -89,12 +90,12 @@ sync: ## Advanced usage -### Configuration inheritance +### Configuration includes -You can extend another `.shopware-project.yml` file 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. +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.yml`: +Parent `.shopware-project.base.yml`: ```yaml url: 'http://localhost' @@ -107,7 +108,8 @@ admin_api: Child `.shopware-project.dev.yml`: ```yaml -extends: 'path/to/parent/.shopware-project.yml' +include: + - '.shopware-project.base.yml' url: 'http://dev.localhost.test' sync: config: @@ -118,7 +120,8 @@ sync: Child `.shopware-project.prod.yml`: ```yaml -extends: 'path/to/parent/.shopware-project.yml' +include: + - '.shopware-project.base.yml' url: 'http://prod.localhost.test' sync: config: