github.com/tonkpils/snag@v1.2.1-0.20160221223445-7f8829737a1d/config.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  type config struct {
    12  	DepWarnning  string
    13  	Script       []string `yaml:"script"`
    14  	Build        []string `yaml:"build"`
    15  	Run          []string `yaml:"run"`
    16  	IgnoredItems []string `yaml:"ignore"`
    17  	Verbose      bool     `yaml:"verbose"`
    18  }
    19  
    20  func parseConfig() (config, error) {
    21  	var c config
    22  
    23  	// if we have any cliCmds, set them to our build phase
    24  	c.Build = cliCmds
    25  
    26  	// if build phase is still empty try and find the snag.yml file
    27  	if len(c.Build) == 0 {
    28  		in, err := ioutil.ReadFile(SnagFile)
    29  		if err != nil {
    30  			return c, fmt.Errorf("could not find %q in your current directory", SnagFile)
    31  		}
    32  
    33  		if err := yaml.Unmarshal(in, &c); err != nil {
    34  			return c, fmt.Errorf("could not parse snag file: %s\n", err)
    35  		}
    36  	}
    37  
    38  	// if both script and build are specified
    39  	// blow up and tell the user to use build
    40  	if len(c.Script) != 0 && len(c.Build) != 0 {
    41  		return c, errors.New("cannot use 'script' and 'build' together. The 'script' tag is deprecated, please use 'build' instead.")
    42  	}
    43  
    44  	// if script has something, tell the user it's deprecated
    45  	// and set whatever its contents are to build
    46  	if len(c.Script) != 0 {
    47  		c.DepWarnning += "*\tThe use of 'script' in the yaml file has been deprecated and will be removed in the future.\n\tPlease start using 'build' instead.\n\n"
    48  		c.Build = c.Script
    49  	}
    50  
    51  	if len(c.Build) == 0 {
    52  		return c, errors.New("you must specify at least 1 command.")
    53  	}
    54  
    55  	c.Verbose = verbose || c.Verbose
    56  	return c, nil
    57  }