github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/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 "path" 21 22 "github.com/palantir/pkg/matcher" 23 "github.com/palantir/pkg/specdir" 24 "github.com/pkg/errors" 25 "gopkg.in/yaml.v2" 26 27 "github.com/palantir/godel/layout" 28 ) 29 30 const ( 31 ExcludeYML = "exclude.yml" 32 ) 33 34 type Exclude struct { 35 // Exclude specifies the files and directories that should be excluded from gödel operations. This parameter is 36 // also passed to subtasks to augment their notion of included and excluded files and directories. 37 Exclude matcher.NamesPathsCfg `json:"exclude"` 38 } 39 40 func LoadFromFile(cfgPath string) (matcher.NamesPathsCfg, error) { 41 fileBytes, err := ioutil.ReadFile(cfgPath) 42 if err != nil { 43 return matcher.NamesPathsCfg{}, errors.Wrapf(err, "Failed to read file %s", cfgPath) 44 } 45 return LoadFromYML(string(fileBytes)) 46 } 47 48 func LoadFromYML(ymlContent string) (matcher.NamesPathsCfg, error) { 49 excludeCfg := matcher.NamesPathsCfg{} 50 if err := yaml.Unmarshal([]byte(ymlContent), &excludeCfg); err != nil { 51 return matcher.NamesPathsCfg{}, errors.Wrapf(err, "Failed to unmarshal YML %s", ymlContent) 52 } 53 return excludeCfg, nil 54 } 55 56 func ReadExcludeJSONFromYML(cfgPath string) ([]byte, error) { 57 excludeCfg, err := LoadFromFile(cfgPath) 58 if err != nil { 59 return nil, err 60 } 61 return json.Marshal(Exclude{ 62 Exclude: excludeCfg, 63 }) 64 } 65 66 func GetCfgDirPath(cfgDirPath, wrapperScriptPath string) (string, error) { 67 if cfgDirPath != "" { 68 return cfgDirPath, nil 69 } 70 if wrapperScriptPath == "" { 71 return "", nil 72 } 73 wrapper, err := specdir.New(path.Dir(wrapperScriptPath), layout.WrapperSpec(), nil, specdir.Validate) 74 if err != nil { 75 return "", err 76 } 77 return wrapper.Path(layout.WrapperConfigDir), nil 78 }