Skip to content

Commit

Permalink
feat: change config inheritance to support multiple files via include…
Browse files Browse the repository at this point in the history
… statement
  • Loading branch information
Jnoack331 authored and shyim committed Oct 9, 2023
1 parent a3db72c commit a14e097
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
34 changes: 18 additions & 16 deletions shop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions wiki/docs/shopware-project-yml-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit a14e097

Please sign in to comment.