-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsource.go
78 lines (63 loc) · 1.61 KB
/
source.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
71
72
73
74
75
76
77
78
package faker
import (
"io/ioutil"
"path/filepath"
"github.com/pkg/errors"
)
// Source provides an interface used to get the response
type Source interface {
Response() ([]byte, error)
}
type textSource struct {
content string
}
func (ts *textSource) Response() ([]byte, error) { return []byte(ts.content), nil }
// NewTextSource returns TextSource that uses the content in the config file
func NewTextSource(args map[string]string) (Source, error) {
ct, ok := args["content"]
if !ok {
return nil, errors.New("Insufficient Argument, missing \"content\"")
}
return &textSource{ct}, nil
}
type fileSource struct {
filepath string
}
func (fs *fileSource) Response() ([]byte, error) { return ioutil.ReadFile(fs.filepath) }
// NewFileSource returns source which uses file as input
func NewFileSource(args map[string]string) (Source, error) {
fp, ok := args["filepath"]
if !ok {
return nil, errors.New("Insufficient Argument, missing\"filepath\"")
}
abs, err := filepath.Abs(fp)
if err != nil {
return nil, err
}
return &fileSource{abs}, nil
}
type reflectSource struct {
bt []byte
}
func (rs *reflectSource) Response() ([]byte, error) {
return rs.bt, nil
}
func NewReflectedSource(bt []byte) (Source, error) {
return &reflectSource{bt}, nil
}
// NewSource returns text source
func NewSource(kind string, bt []byte, args map[string]string) (Source, error) {
switch kind {
case "text":
return NewTextSource(args)
case "file":
return NewFileSource(args)
case "reflect":
return NewReflectedSource(bt)
default:
return nil, errors.Wrap(
errors.New("Uknown kind of source"),
"Kind: ["+kind+"]",
)
}
}