-
Notifications
You must be signed in to change notification settings - Fork 225
/
yaml.go
43 lines (37 loc) · 961 Bytes
/
yaml.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
package yaml
import (
"os"
"github.com/goccy/go-yaml"
"github.com/projectdiscovery/proxify/pkg/types"
)
// YamlMultiDocWriter is a writer for yaml multi doc
type YamlMultiDocWriter struct {
f *os.File
enc *yaml.Encoder
}
// NewYamlMultiDocWriter creates a new yaml multi doc writer
func NewYamlMultiDocWriter(filePath string) (*YamlMultiDocWriter, error) {
file, err := os.Create(filePath)
if err != nil {
return nil, err
}
enc := yaml.NewEncoder(file, yaml.UseLiteralStyleIfMultiline(true), yaml.UseSingleQuote(true))
return &YamlMultiDocWriter{f: file, enc: enc}, nil
}
// Write writes a http transaction to the file.
func (y *YamlMultiDocWriter) Write(data *types.HTTPRequestResponseLog) error {
if err := y.enc.Encode(data); err != nil {
return err
}
return nil
}
// Close closes the file writer.
func (y *YamlMultiDocWriter) Close() error {
if y.enc != nil {
y.enc.Close()
}
if y.f != nil {
_ = y.f.Close()
}
return nil
}