-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput.go
70 lines (63 loc) · 1.38 KB
/
output.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
61
62
63
64
65
66
67
68
69
70
package dump
import (
"errors"
"log"
"os"
)
type output struct {
path string
encoding string
kind string // cant use type,reserved word
}
// NewOutput returns an output type unless there is a validation error where
// it returns an error instead
func NewOutput(p, e, k string) (*output, error) {
c := &output{}
if !c.setPath(p) {
return &output{}, errors.New("failed to set output path")
}
if !c.setEncoding(e) {
return &output{}, errors.New("failed to set output encoding")
}
if !c.setKind(k) {
return &output{}, errors.New("failed to set output kind")
}
return c, nil
}
func (o *output) setPath(s string) bool {
o.path = s
return true
}
func (o *output) setEncoding(s string) bool {
expectedEncodings := []string{"json", "yaml"}
for _, e := range expectedEncodings {
if s == e {
o.encoding = s
return true
}
}
log.SetOutput(os.Stderr)
log.Printf("Unexpected encoding %s, we only accept: %v", s, expectedEncodings)
return false
}
func (o *output) setKind(s string) bool {
expectedKinds := []string{"file", "stdout", "s3"}
for _, k := range expectedKinds {
if s == k {
o.kind = s
return true
}
}
log.SetOutput(os.Stderr)
log.Printf("Unexpected output type %s\n", s)
return false
}
func (o *output) GetPath() string {
return o.path
}
func (o *output) GetEncoding() string {
return o.encoding
}
func (o *output) GetKind() string {
return o.kind
}