grim/convey

closing closed branch again
multiple-images
2018-01-26, Gary Kramlich
8e45b1f8ccff
closing closed branch again
/*
* Convey
* Copyright 2016-2017 Gary Kramlich <grim@reaperworld.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package config
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/go-yaml/yaml"
"bitbucket.org/rw_grim/convey/plans"
"bitbucket.org/rw_grim/convey/tasks"
)
type Config struct {
Tasks map[string]tasks.Task
Plans map[string]plans.Plan
Environment []string
}
type rawConfig struct {
Tasks map[string]yaml.MapSlice `yaml:"tasks"`
Plans map[string]plans.Plan `yaml:"plans"`
Environment []string `yaml:"environment"`
}
// LoadFile attempts to load the config from a string containing a file path
func LoadFile(file string) (*Config, error) {
// split the filename into our parts
path, base := filepath.Split(file)
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read config file '%s'", file)
}
return Load(path, base, data)
}
// Load loads the config from a data and uses path for all path operations
func Load(path string, base string, data []byte) (*Config, error) {
// load the raw config
config := rawConfig{}
err := yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
if len(config.Tasks) == 0 {
return nil, fmt.Errorf("no tasks specified")
}
if len(config.Plans) == 0 {
return nil, fmt.Errorf("no plans specified")
}
// turn the raw tasks into real tasks
realTasks, err := loadTasks(path, config.Tasks)
if err != nil {
return nil, err
}
// iterate through each plan and do any cleanup we need to
for _, plan := range config.Plans {
// set stage names for any that are missing them
for idx, _ := range plan.Stages {
if plan.Stages[idx].Name == "" {
plan.Stages[idx].Name = fmt.Sprintf("stage-%d", idx)
}
}
}
// create the real config
realConfig := &Config{
Environment: config.Environment,
Plans: config.Plans,
Tasks: realTasks,
}
// now load the override
loadOverride(path, base, realConfig)
return realConfig, nil
}