Skip to content
Snippets Groups Projects
config.go 2.13 KiB
Newer Older
package runner

import (
	"github.com/spf13/viper"
)

Florent Aide's avatar
Florent Aide committed
// Variable ...
type Variable struct {
	Name  string `mapstructure:"name"`
	Value string `mapstructure:"value"`
}

Florent Aide's avatar
Florent Aide committed
// Value ...
type Value struct {
	Key   string `mapstructure:"key"`
	Value string `mapstructure:"value"`
type HelmChart struct {
Florent Aide's avatar
Florent Aide committed
	Type   string      `mapstructure:"type"`
	Name   string      `mapstructure:"name"`
	Values interface{} `mapstructure:"values"`
}

type YttChart struct {
	Type   string  `mapstructure:"type"`
	Name   string  `mapstructure:"name"`
	Values []Value `mapstructure:"values"`
}

type Charts struct {
	Helm map[string]HelmChart `mapstructure:"helm"`
	Ytt  map[string]YttChart  `mapstructure:"ytt"`
}

Florent Aide's avatar
Florent Aide committed
// Spec ...
type Spec struct {
	Variables []Variable `mapstructure:"variables"`
	Charts    Charts     `mapstructure:"charts"`
Florent Aide's avatar
Florent Aide committed
}

// Config is the configuration we get after parsing our beaver.yml file
type Config struct {
	APIVersion string `mapstructure:"apiVersion"`
	Kind       string `mapstructure:"kind"`
	Spec       Spec   `mapstructure:"spec"`
}

Florent Aide's avatar
Florent Aide committed
// NewConfig returns a *Config
steeve.chailloux's avatar
steeve.chailloux committed
func NewConfig(configDir string) (*Config, error) {
	v := viper.New()
	v.SetConfigName("beaver")
	v.AddConfigPath(configDir)
	if err := v.ReadInConfig(); err != nil {
		return nil, err
	}
	var config Config
Florent Aide's avatar
Florent Aide committed
	cfg := &config
steeve.chailloux's avatar
steeve.chailloux committed
	if err := v.Unmarshal(&cfg); err != nil {
Florent Aide's avatar
Florent Aide committed

	return cfg, nil
}

// MergeVariables takes another config and will import those variables into
// the current config by replacing old ones and adding the new ones
func (c *Config) MergeVariables(other *Config) {
	for _, variable := range other.Spec.Variables {
		c.overlayVariable(variable)
Florent Aide's avatar
Florent Aide committed

// overlayVariable takes a variable in and either replaces an existing variable
// of the same name or create a new variable in the config if no matching name
// is found
func (c *Config) overlayVariable(v Variable) {
	// find same variable by name and replace is value
	// if not found then create the variable
	for index, originalVariable := range c.Spec.Variables {
		if originalVariable.Name == v.Name {
			c.Spec.Variables[index].Value = v.Value
			return
Florent Aide's avatar
Florent Aide committed
		}
	}
	c.Spec.Variables = append(c.Spec.Variables, v)
Florent Aide's avatar
Florent Aide committed
}