util.go 682 Bytes
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package backend

import (
	"io/ioutil"
	"path/filepath"

	"gopkg.in/yaml.v2"
)

// Unmarshal a partially-parsed yaml.MapSlice.
func UnmarshalMapSlice(raw yaml.MapSlice, obj interface{}) error {
	b, err := yaml.Marshal(raw)
	if err != nil {
		return err
	}
	return yaml.Unmarshal(b, obj)
}

// Load and unmarshal a YAML file.
func LoadYAML(path string, obj interface{}) error {
	data, err := ioutil.ReadFile(path) // #nosec
	if err != nil {
		return err
	}
	return yaml.Unmarshal(data, obj)
}

// ResolvePath returns the path evaluated as relative to base.
func ResolvePath(path, base string) string {
	if !filepath.IsAbs(path) {
		path = filepath.Join(base, path)
	}
	return path
}