mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-05 09:31:52 +02:00
This adds `values` to state files as proposed in #640. ```yaml values: - key1: val1 - defaults.yaml environments: default: - values: - environments/default.yaml production: - values: - environments/production.yaml ``` `{{ .Valuese.key1 }}` evaluates to `val1` if and only if it is not overrode via the production or the default env, or command-line args. Resolves #640
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package environment
|
|
|
|
import (
|
|
"github.com/imdario/mergo"
|
|
"github.com/roboll/helmfile/pkg/maputil"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type Environment struct {
|
|
Name string
|
|
Values map[string]interface{}
|
|
Defaults map[string]interface{}
|
|
}
|
|
|
|
var EmptyEnvironment Environment
|
|
|
|
func (e Environment) DeepCopy() Environment {
|
|
valuesBytes, err := yaml.Marshal(e.Values)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var values map[string]interface{}
|
|
if err := yaml.Unmarshal(valuesBytes, &values); err != nil {
|
|
panic(err)
|
|
}
|
|
values, err = maputil.CastKeysToStrings(values)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defaultsBytes, err := yaml.Marshal(e.Defaults)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var defaults map[string]interface{}
|
|
if err := yaml.Unmarshal(defaultsBytes, &defaults); err != nil {
|
|
panic(err)
|
|
}
|
|
defaults, err = maputil.CastKeysToStrings(defaults)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return Environment{
|
|
Name: e.Name,
|
|
Values: values,
|
|
Defaults: defaults,
|
|
}
|
|
}
|
|
|
|
func (e *Environment) Merge(other *Environment) (*Environment, error) {
|
|
if e == nil {
|
|
if other != nil {
|
|
copy := other.DeepCopy()
|
|
return ©, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
copy := e.DeepCopy()
|
|
if other != nil {
|
|
if err := mergo.Merge(©, other, mergo.WithOverride); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return ©, nil
|
|
}
|