-
Notifications
You must be signed in to change notification settings - Fork 1
/
rwi.go
126 lines (106 loc) · 2.76 KB
/
rwi.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
// Package rwi : Reader/Writer Interface for command-line
//
// These codes are licensed under CC0.
// http://creativecommons.org/publicdomain/zero/1.0/
package rwi
import (
"bytes"
"fmt"
"io"
)
// RWI is Reader/Writer class for command-line
type RWI struct {
reader io.Reader
writer io.Writer
errorWriter io.Writer
}
//OptFunc is self-referential function for functional options pattern
type OptFunc func(*RWI)
// New returns a new RWI instance
func New(opts ...OptFunc) *RWI {
c := &RWI{reader: io.NopCloser(bytes.NewReader(nil)), writer: io.Discard, errorWriter: io.Discard}
for _, opt := range opts {
opt(c)
}
return c
}
//WithReader returns function for setting Reader
func WithReader(r io.Reader) OptFunc {
return func(c *RWI) {
if r != nil {
c.reader = r
}
}
}
//WithWriter returns function for setting Writer
func WithWriter(w io.Writer) OptFunc {
return func(c *RWI) {
if w != nil {
c.writer = w
}
}
}
//WithErrorWriter returns function for setting Writer (error)
func WithErrorWriter(e io.Writer) OptFunc {
return func(c *RWI) {
if e != nil {
c.errorWriter = e
}
}
}
//Reader returns RWI.reader
func (c *RWI) Reader() io.Reader {
return c.reader
}
//Writer returns RWI.writer
func (c *RWI) Writer() io.Writer {
return c.writer
}
//ErrorWriter returns RWI.errorWriter
func (c *RWI) ErrorWriter() io.Writer {
return c.errorWriter
}
//Output output to RWI.writer
func (c *RWI) Output(val ...interface{}) error {
return doOutput(c.writer, val)
}
//Outputln output to RWI.writer (add newline).
func (c *RWI) Outputln(val ...interface{}) error {
return doOutputln(c.writer, val)
}
//OutputBytes to RWI.writer ([]byte data).
func (c *RWI) OutputBytes(data []byte) error {
return c.WriteFrom(bytes.NewReader(data))
}
//WriteFrom copy from io.Reader to RWI.writer
func (c *RWI) WriteFrom(r io.Reader) error {
_, err := io.Copy(c.writer, r)
return err
}
//OutputErr output to RWI.errorWriter
func (c *RWI) OutputErr(val ...interface{}) error {
return doOutput(c.errorWriter, val)
}
//OutputErrln output to RWI.errorWriter (add newline).
func (c *RWI) OutputErrln(val ...interface{}) error {
return doOutputln(c.errorWriter, val)
}
//OutputErrBytes copy to RWI.errorWriter ([]byte data).
func (c *RWI) OutputErrBytes(data []byte) error {
return c.WriteErrFrom(bytes.NewReader(data))
}
//WriteErrFrom copy from io.Reader to RWI.errorWriter
func (c *RWI) WriteErrFrom(r io.Reader) error {
_, err := io.Copy(c.errorWriter, r)
return err
}
//Output to io.Writer (internal)
func doOutput(writer io.Writer, val []interface{}) error {
_, err := fmt.Fprint(writer, val...)
return err
}
//Output to io.Writer (add newline, internal)
func doOutputln(writer io.Writer, val []interface{}) error {
_, err := fmt.Fprintln(writer, val...)
return err
}