-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
60 lines (51 loc) · 1.05 KB
/
file.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
49
50
51
52
53
54
55
56
57
58
59
60
package ctfdsetup
import (
"os"
"github.com/invopop/jsonschema"
"gopkg.in/yaml.v3"
)
type File struct {
Name string `yaml:"-" json:"-" jsonschema:"-"`
Content []byte `yaml:"-" json:"-" jsonschema:"-"`
}
var _ yaml.Unmarshaler = (*File)(nil)
func (file *File) UnmarshalYAML(node *yaml.Node) error {
if node.Value != "" {
file.Content = []byte(node.Value)
return nil
}
type lfi struct {
FromFile *string `yaml:"from_file"`
}
var lfiv lfi
if err := node.Decode(&lfiv); err != nil {
return err
}
if lfiv.FromFile == nil {
return nil
}
fc, err := os.ReadFile(*lfiv.FromFile)
if err != nil {
return err
}
file.Name = *lfiv.FromFile
file.Content = fc
return nil
}
func (file File) JSONSchema() *jsonschema.Schema {
subObj := jsonschema.NewProperties()
subObj.Set("from_file", &jsonschema.Schema{
Type: "string",
Description: "The file to import content from.",
})
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Type: "object",
Properties: subObj,
}, {
Type: "string",
},
},
}
}