github.com/kcmerrill/alfred@v0.0.0-20180727171036-06445dcb5e3d/pkg/alfred/config.go (about)

     1  package alfred
     2  
     3  import (
     4  	"io/ioutil"
     5  
     6  	yaml "gopkg.in/yaml.v2"
     7  )
     8  
     9  func configC(task Task, context *Context, tasks map[string]Task) {
    10  	// load up the config, if any
    11  	if task.Config != "" {
    12  		var yamlFile []byte
    13  		dir, _ := task.dir(context)
    14  		if contents, configFileErr := ioutil.ReadFile(translate(task.Config, context)); configFileErr == nil {
    15  			yamlFile = contents
    16  		} else {
    17  			yamlFile = []byte(translate(task.Config, context))
    18  		}
    19  		c := make(map[string]string)
    20  		if configFileUnMarshalErr := yaml.Unmarshal(yamlFile, &c); configFileUnMarshalErr != nil {
    21  			outFail("config", "{{ .Text.Failure }}"+configFileUnMarshalErr.Error(), context)
    22  			// TODO: Should we bail if we can't unmarshal? Or leave it up to the task? ... Let me chew on this
    23  			task.Exit(context, tasks)
    24  		}
    25  		for key, value := range c {
    26  			context.Vars[key] = evaluate(value, dir)
    27  		}
    28  	}
    29  }