github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/apps/okgo/config/config.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 import ( 18 "encoding/json" 19 "io/ioutil" 20 21 "github.com/palantir/amalgomate/amalgomated" 22 "github.com/palantir/pkg/matcher" 23 "github.com/pkg/errors" 24 "gopkg.in/yaml.v2" 25 26 "github.com/palantir/godel/apps/okgo/checkoutput" 27 "github.com/palantir/godel/apps/okgo/cmd/cmdlib" 28 "github.com/palantir/godel/apps/okgo/params" 29 ) 30 31 type OKGo struct { 32 // Checks specifies the configuration used by the checks. The key is the name of the check and the value is the 33 // custom configuration for that check. 34 Checks map[string]Checker `yaml:"checks" json:"checks"` 35 36 // Exclude specifies the files that should be excluded from tests. 37 Exclude matcher.NamesPathsCfg `yaml:"exclude" json:"exclude"` 38 } 39 40 type Checker struct { 41 // Skip specifies whether or not the check should be skipped entirely. 42 Skip bool `yaml:"skip" json:"skip"` 43 44 // Args specifies the commnand-line arguments provided to the check. 45 Args []string `yaml:"args" json:"args"` 46 47 // Filters specifies the filter definitions. Raw output lines that match the filter are excluded from 48 // processing. 49 Filters []Filter `yaml:"filters" json:"filters"` 50 } 51 52 type Filter struct { 53 // Type specifies the type of the filter: "message", "name" or "path". 54 Type string `yaml:"type" json:"type"` 55 56 // The value of the filter. 57 Value string `yaml:"value" json:"value"` 58 } 59 60 func (r *OKGo) ToParams() (params.OKGo, error) { 61 checks := make(map[amalgomated.Cmd]params.Checker) 62 for key, value := range r.Checks { 63 singleParam, err := value.ToParam() 64 if err != nil { 65 return params.OKGo{}, err 66 } 67 cmd, err := cmdlib.Instance().NewCmd(key) 68 if err != nil { 69 return params.OKGo{}, errors.Wrapf(err, "unable to convert %s into a command", key) 70 } 71 checks[cmd] = singleParam 72 } 73 return params.OKGo{ 74 Checks: checks, 75 Exclude: r.Exclude.Matcher(), 76 }, nil 77 } 78 79 func (r *Checker) ToParam() (params.Checker, error) { 80 var lineFilters []checkoutput.Filterer 81 for _, cfg := range r.Filters { 82 checkFilter, err := cfg.toFilter(checkoutput.MessageRegexpFilter) 83 if err != nil { 84 return params.Checker{}, errors.Wrapf(err, "failed to parse filter: %v", cfg) 85 } 86 lineFilters = append(lineFilters, checkFilter) 87 } 88 return params.Checker{ 89 Skip: r.Skip, 90 Args: r.Args, 91 LineFilters: lineFilters, 92 }, nil 93 } 94 95 func (f *Filter) toFilter(filterForBlankType func(name string) checkoutput.Filterer) (checkoutput.Filterer, error) { 96 switch f.Type { 97 case "message": 98 return checkoutput.MessageRegexpFilter(f.Value), nil 99 case "name": 100 return checkoutput.NamePathFilter(f.Value), nil 101 case "path": 102 return checkoutput.RelativePathFilter(f.Value), nil 103 case "": 104 if filterForBlankType != nil { 105 return filterForBlankType(f.Value), nil 106 } 107 fallthrough 108 default: 109 return nil, errors.Errorf("unknown filter type: %v", f.Type) 110 } 111 } 112 113 func Load(configPath, jsonContent string) (params.OKGo, error) { 114 var yml []byte 115 if configPath != "" { 116 var err error 117 yml, err = ioutil.ReadFile(configPath) 118 if err != nil { 119 return params.OKGo{}, errors.Wrapf(err, "failed to read file %s", configPath) 120 } 121 } 122 cfg, err := LoadRawConfig(string(yml), jsonContent) 123 if err != nil { 124 return params.OKGo{}, err 125 } 126 return cfg.ToParams() 127 } 128 129 func LoadRawConfig(ymlContent, jsonContent string) (OKGo, error) { 130 rawCfg := OKGo{} 131 if ymlContent != "" { 132 if err := yaml.Unmarshal([]byte(ymlContent), &rawCfg); err != nil { 133 return OKGo{}, errors.Wrapf(err, "failed to unmarshal YML %s", ymlContent) 134 } 135 } 136 if jsonContent != "" { 137 jsonCfg := OKGo{} 138 if err := json.Unmarshal([]byte(jsonContent), &jsonCfg); err != nil { 139 return OKGo{}, errors.Wrapf(err, "failed to parse JSON %s", jsonContent) 140 } 141 rawCfg.Exclude.Add(jsonCfg.Exclude) 142 } 143 return rawCfg, nil 144 }