forked from val00274/chidley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
128 lines (101 loc) · 1.99 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
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
package main
import (
"bufio"
"io"
"log"
"net/http"
"os"
)
type Source interface {
io.Closer
newSource(name string) error
getName() string
getReader() io.Reader
}
type GenericSource struct {
name string
reader io.Reader
}
type FileSource struct {
GenericSource
file *os.File
}
type UrlSource struct {
GenericSource
}
type StdinSource struct {
GenericSource
}
//StdInSource impl
func (us *StdinSource) copySource() (Source, error) {
err := new(InternalError)
//error.ErrorString = "copySource not supported"
return nil, err
}
func (us *StdinSource) getName() string {
return ""
}
func (us *StdinSource) newSource(name string) error {
us.reader = bufio.NewReader(os.Stdin)
return nil
}
func (us *StdinSource) Close() error {
return nil
}
func (us *StdinSource) getReader() io.Reader {
return us.reader
}
//UrlSource impl
func (us *UrlSource) copySource() (Source, error) {
copy := new(UrlSource)
err := copy.newSource(us.name)
return copy, err
}
func (us *UrlSource) getName() string {
return us.name
}
func (us *UrlSource) newSource(name string) error {
us.name = name
var err error
res, err := http.Get(name)
if err != nil {
log.Fatal(err)
}
if res.StatusCode != 200 {
log.Fatal("ERROR: bad http status code != 200: ", res.StatusCode, " ", name)
return nil
}
us.reader = res.Body
return err
}
func (us UrlSource) Close() error {
closer, ok := us.reader.(io.Closer)
if ok {
return closer.Close()
}
return nil
}
func (us *UrlSource) getReader() io.Reader {
return us.reader
}
// FileSource impl
func (fs *FileSource) copySource() (Source, error) {
copy := new(FileSource)
err := copy.newSource(fs.name)
return copy, err
}
func (fs *FileSource) getName() string {
return fs.name
}
func (fs *FileSource) newSource(name string) error {
fs.name = name
var err error
fs.reader, fs.file, err = genericReader(name)
return err
}
func (fs *FileSource) Close() error {
return fs.file.Close()
}
func (fs FileSource) getReader() io.Reader {
return fs.reader
}