-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathfile.go
More file actions
146 lines (127 loc) · 2.85 KB
/
file.go
File metadata and controls
146 lines (127 loc) · 2.85 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package file
import (
"context"
_ "embed"
"fmt"
"os"
"strings"
"github.com/odpf/meteor/models"
"github.com/odpf/meteor/plugins"
"github.com/odpf/meteor/registry"
"github.com/odpf/meteor/utils"
"github.com/odpf/salt/log"
ndjson "github.com/scizorman/go-ndjson"
"gopkg.in/yaml.v3"
)
//go:embed README.md
var summary string
type Config struct {
Overwrite bool `mapstructure:"overwrite" default:"true"`
Path string `mapstructure:"path" validate:"required"`
Format string `mapstructure:"format" validate:"required"`
}
var sampleConfig = `
path: ./output-filename.txt
format: ndjson
`
type Sink struct {
logger log.Logger
config Config
format string
File *os.File
}
func New() plugins.Syncer {
return new(Sink)
}
func (s *Sink) Info() plugins.Info {
return plugins.Info{
Description: "save output to a file",
SampleConfig: sampleConfig,
Summary: summary,
Tags: []string{"file", "json", "yaml", "sink"},
}
}
func (s *Sink) Validate(configMap map[string]interface{}) (err error) {
return utils.BuildConfig(configMap, &Config{})
}
func (s *Sink) Init(ctx context.Context, config map[string]interface{}) (err error) {
if err := utils.BuildConfig(config, &s.config); err != nil {
return plugins.InvalidConfigError{Type: "sink", PluginName: "file"}
}
if err := s.validateFilePath(s.config.Path); err != nil {
return err
}
s.format = s.config.Format
if s.config.Overwrite {
s.File, err = os.Create(s.config.Path)
return err
}
s.File, err = os.OpenFile(s.config.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
return err
}
return
}
func (s *Sink) Sink(ctx context.Context, batch []models.Record) (err error) {
var data []models.Metadata
for _, record := range batch {
data = append(data, record.Data())
}
if s.format == "ndjson" {
err := s.ndjsonOut(data)
if err != nil {
return err
}
return nil
}
err = s.yamlOut(data)
if err != nil {
return err
}
return nil
}
func (s *Sink) Close() (err error) {
// return s.File.Close()
return nil
}
func (s *Sink) ndjsonOut(data []models.Metadata) error {
jsnBy, err := ndjson.Marshal(data)
if err != nil {
return err
}
err = s.writeBytes(jsnBy)
return err
}
func (s *Sink) yamlOut(data []models.Metadata) error {
ymlByte, err := yaml.Marshal(data)
if err != nil {
return err
}
err = s.writeBytes(ymlByte)
return err
}
func (s *Sink) writeBytes(b []byte) error {
_, err := s.File.Write(b)
if err != nil {
return err
}
return nil
}
func (s *Sink) validateFilePath(path string) error {
dirs := strings.Split(path, "/")
filename := dirs[len(dirs)-1]
format := strings.Split(filename, ".")
if len(format) != 2 {
return fmt.Errorf("invalid filename")
}
return nil
}
func init() {
if err := registry.Sinks.Register("file", func() plugins.Syncer {
return &Sink{
logger: plugins.GetLog(),
}
}); err != nil {
panic(err)
}
}