func loadConfig()

in internal/tfengine/config.go [117:148]


func loadConfig(path string, data map[string]interface{}) (*Config, error) {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("read file %v: %v", path, err)
	}
	buf, err := template.WriteBuffer(string(b), data)
	if err != nil {
		return nil, fmt.Errorf("parsing template %v: %v", string(b), err)
	}

	// Convert yaml to json so hcl decoder can parse it.
	cj := buf.Bytes()
	if filepath.Ext(path) == ".yaml" {
		cj, err = yaml.YAMLToJSON(cj)
		if err != nil {
			return nil, fmt.Errorf("converting yaml to json %v: %v", string(cj), err)
		}
		// hclsimple.Decode doesn't actually use the path for anything other
		// than its extension, so just pass in any file name ending with json so
		// the library knows to treat these bytes as json and not yaml.
		path = "file.json"
	}

	c := new(Config)
	if err := hclsimple.Decode(path, cj, nil, c); err != nil {
		return nil, fmt.Errorf("decode as hcl %v: %v", string(cj), err)
	}
	if err := c.Init(); err != nil {
		return nil, fmt.Errorf("init config %v: %v", c, err)
	}
	return c, nil
}