Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add project configration inheritance support #234

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions shop/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shop

import (
"dario.cat/mergo"
"fmt"
"os"
"strings"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
51 changes: 51 additions & 0 deletions wiki/docs/shopware-project-yml-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading