github.com/mkasner/goat@v0.0.0-20190419083224-77b17249a8e3/context/config.go (about)

     1  package context
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  
     7  	"github.com/yosssi/goat/config"
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  // Config represents a configuration of a process.
    12  type Config struct {
    13  	InitTasks []*Task    `json:"init_tasks" yaml:"init_tasks"`
    14  	Watchers  []*Watcher `json:"watchers" yaml:"watchers"`
    15  }
    16  
    17  // NewConfig parses a JSON file, generates a Config and returns it.
    18  func NewConfig() (*Config, error) {
    19  	bytes, err := ioutil.ReadFile(config.JSONConfigFile)
    20  	if err != nil {
    21  		bytes, err = ioutil.ReadFile(config.YAMLConfigFile)
    22  		if err != nil {
    23  			return nil, err
    24  		}
    25  
    26  		config := &Config{}
    27  		if err := yaml.Unmarshal(bytes, config); err != nil {
    28  			return nil, err
    29  		}
    30  		return config, nil
    31  	}
    32  
    33  	config := &Config{}
    34  	if err := json.Unmarshal(bytes, config); err != nil {
    35  		return nil, err
    36  	}
    37  	return config, nil
    38  }