| 280 | } |
| 281 | |
| 282 | func (om *Manager) unmarshalMaybeGzipped(filename string, data []byte) (map[string]any, error) { |
| 283 | yamlFile := map[string]any{} |
| 284 | if strings.HasSuffix(filename, ".gz") { |
| 285 | r, err := gzip.NewReader(bytes.NewReader(data)) |
| 286 | if err != nil { |
| 287 | return nil, errors.Wrap(err, "read gzipped file") |
| 288 | } |
| 289 | defer r.Close() |
| 290 | err = yaml.NewDecoder(r).Decode(&yamlFile) |
| 291 | return yamlFile, errors.Wrap(err, "uncompress/unmarshal gzipped file") |
| 292 | } |
| 293 | |
| 294 | if err := yaml.Unmarshal(data, &yamlFile); err != nil { |
| 295 | // Give a hint if we think that file is gzipped. |
| 296 | if isGzip(data) { |
| 297 | return nil, errors.Wrap(err, "file looks gzipped but doesn't have a .gz extension") |
| 298 | } |
| 299 | return nil, err |
| 300 | } |
| 301 | return yamlFile, nil |
| 302 | } |
| 303 | |
| 304 | func isGzip(data []byte) bool { |
| 305 | return len(data) > 2 && data[0] == 0x1f && data[1] == 0x8b |