github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/cli/evaluate.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 7 "github.com/evergreen-ci/evergreen/model" 8 "github.com/pkg/errors" 9 "gopkg.in/yaml.v2" 10 ) 11 12 // EvaluateCommand reads in a project config, expanding tags and matrix definitions, 13 // then prints the expanded definitions back out as yaml. 14 type EvaluateCommand struct { 15 Tasks bool `short:"t" long:"tasks" description:"only show task and function definitions"` 16 Variants bool `short:"v" long:"variants" description:"only show variant definitions"` 17 } 18 19 func (ec *EvaluateCommand) Execute(args []string) error { 20 if len(args) != 1 { 21 return errors.New("the evaluate command takes one project config path as an argument") 22 } 23 configBytes, err := ioutil.ReadFile(args[0]) 24 if err != nil { 25 return errors.Wrap(err, "error reading project config") 26 } 27 28 p := &model.Project{} 29 err = model.LoadProjectInto(configBytes, "", p) 30 if err != nil { 31 return errors.Wrap(err, "error loading project") 32 } 33 34 var out interface{} 35 if ec.Tasks || ec.Variants { 36 tmp := struct { 37 Functions interface{} `yaml:"functions,omitempty"` 38 Tasks interface{} `yaml:"tasks,omitempty"` 39 Variants interface{} `yaml:"buildvariants,omitempty"` 40 }{} 41 if ec.Tasks { 42 tmp.Functions = p.Functions 43 tmp.Tasks = p.Tasks 44 } 45 if ec.Variants { 46 tmp.Variants = p.BuildVariants 47 } 48 out = tmp 49 } else { 50 out = p 51 } 52 53 outYAML, err := yaml.Marshal(out) 54 if err != nil { 55 return errors.Wrap(err, "error marshaling evaluated project YAML") 56 } 57 fmt.Println(string(outYAML)) 58 59 return nil 60 }