MCPcopy
hub / github.com/grafana/dskit / unmarshalMaybeGzipped

Method unmarshalMaybeGzipped

runtimeconfig/manager.go:282–302  ·  view source on GitHub ↗
(filename string, data []byte)

Source from the content-addressed store, hash-verified

280}
281
282func (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
304func isGzip(data []byte) bool {
305 return len(data) > 2 && data[0] == 0x1f && data[1] == 0x8b

Callers 1

loadConfigMethod · 0.95

Calls 5

isGzipFunction · 0.85
WrapMethod · 0.65
CloseMethod · 0.65
DecodeMethod · 0.65
UnmarshalMethod · 0.45

Tested by

no test coverage detected