forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadataJSON.go
48 lines (40 loc) · 1.24 KB
/
metadataJSON.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package plugin
import (
"context"
"encoding/json"
)
// MetadataJSONFile represents a metadata.json file that contains another entry's metadata.
type MetadataJSONFile struct {
EntryBase
other Entry
}
// NewMetadataJSONFile creates a new MetadataJSONFile. If caching Metadata on the `other` entry is
// disabled, it will use that to compute the file size upfront.
func NewMetadataJSONFile(ctx context.Context, other Entry) (*MetadataJSONFile, error) {
return &MetadataJSONFile{
EntryBase: NewEntry("metadata.json"),
other: other,
}, nil
}
// Schema defines the schema of a metadata.json file.
func (m *MetadataJSONFile) Schema() *EntrySchema {
return NewEntrySchema(m, "metadata.json").
SetDescription(metadataJSONDescription).
IsSingleton()
}
// Read returns the metadata of the `other` entry as its content.
func (m *MetadataJSONFile) Read(ctx context.Context) ([]byte, error) {
meta, err := Metadata(ctx, m.other)
if err != nil {
return nil, err
}
prettyMeta, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return nil, err
}
return prettyMeta, nil
}
const metadataJSONDescription = `
A read-only 'file' whose content contains the underlying entry's full metadata.
This makes it easier for you to grep its values.
`