1 // Copyright 2017 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
29 var fileCache map[string][]byte
30 var infoCache map[string]interface{}
33 var verboseReader = false
35 func initializeFileCache() {
37 fileCache = make(map[string][]byte, 0)
41 func initializeInfoCache() {
43 infoCache = make(map[string]interface{}, 0)
47 // FetchFile gets a specified file from the local filesystem or a remote location.
48 func FetchFile(fileurl string) ([]byte, error) {
50 bytes, ok := fileCache[fileurl]
53 log.Printf("Cache hit %s", fileurl)
58 log.Printf("Fetching %s", fileurl)
60 response, err := http.Get(fileurl)
64 if response.StatusCode != 200 {
65 return nil, errors.New(fmt.Sprintf("Error downloading %s: %s", fileurl, response.Status))
67 defer response.Body.Close()
68 bytes, err = ioutil.ReadAll(response.Body)
70 fileCache[fileurl] = bytes
75 // ReadBytesForFile reads the bytes of a file.
76 func ReadBytesForFile(filename string) ([]byte, error) {
77 // is the filename a url?
78 fileurl, _ := url.Parse(filename)
79 if fileurl.Scheme != "" {
81 bytes, err := FetchFile(filename)
87 // no, it's a local filename
88 bytes, err := ioutil.ReadFile(filename)
95 // ReadInfoFromBytes unmarshals a file as a yaml.MapSlice.
96 func ReadInfoFromBytes(filename string, bytes []byte) (interface{}, error) {
98 cachedInfo, ok := infoCache[filename]
101 log.Printf("Cache hit info for file %s", filename)
103 return cachedInfo, nil
106 log.Printf("Reading info for file %s", filename)
108 var info yaml.MapSlice
109 err := yaml.Unmarshal(bytes, &info)
113 if len(filename) > 0 {
114 infoCache[filename] = info
119 // ReadInfoForRef reads a file and return the fragment needed to resolve a $ref.
120 func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
121 initializeInfoCache()
123 info, ok := infoCache[ref]
126 log.Printf("Cache hit for ref %s#%s", basefile, ref)
132 log.Printf("Reading info for ref %s#%s", basefile, ref)
135 basedir, _ := filepath.Split(basefile)
136 parts := strings.Split(ref, "#")
139 filename = basedir + parts[0]
143 bytes, err := ReadBytesForFile(filename)
147 info, err := ReadInfoFromBytes(filename, bytes)
149 log.Printf("File error: %v\n", err)
152 path := strings.Split(parts[1], "/")
153 for i, key := range path {
155 m, ok := info.(yaml.MapSlice)
158 for _, section := range m {
159 if section.Key == key {
166 return nil, NewError(nil, fmt.Sprintf("could not resolve %s", ref))
173 infoCache[ref] = info