-
Notifications
You must be signed in to change notification settings - Fork 18
/
context.go
156 lines (132 loc) · 2.91 KB
/
context.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package out
import (
"bytes"
"fmt"
"io"
"os"
)
type Context interface {
StdOut() io.Writer
StdErr() io.Writer
StdIn() io.Reader
}
func Outf(ctx Context, msg string, args ...interface{}) (int, error) {
if len(args) == 0 {
return fmt.Fprint(ctx.StdOut(), msg)
}
return fmt.Fprintf(ctx.StdOut(), msg, args...)
}
func Out(ctx Context, args ...interface{}) (int, error) {
return fmt.Fprint(ctx.StdOut(), args...)
}
func Outln(ctx Context, args ...interface{}) (int, error) {
return fmt.Fprintln(ctx.StdOut(), args...)
}
func Errf(ctx Context, msg string, args ...interface{}) (int, error) {
if len(args) == 0 {
return fmt.Fprint(ctx.StdErr(), msg)
}
return fmt.Fprintf(ctx.StdErr(), msg, args...)
}
func Err(ctx Context, args ...interface{}) (int, error) {
return fmt.Fprint(ctx.StdOut(), args...)
}
func Error(ctx Context, msg string, args ...interface{}) (int, error) {
return Errf(ctx, "Error: "+msg+"\n", args...)
}
func Warning(ctx Context, msg string, args ...interface{}) (int, error) {
return Errf(ctx, "Warning: "+msg+"\n", args...)
}
////////////////////////////////////////////////////////////////////////////////
type outputContext struct {
parent Context
in io.Reader
out io.Writer
err io.Writer
}
var DefaultContext = New()
func New() Context {
return &outputContext{
in: os.Stdin,
out: os.Stdout,
err: os.Stderr,
}
}
func NewBuffered() (Context, *bytes.Buffer) {
var buffer bytes.Buffer
return &outputContext{
in: os.Stdin,
out: &buffer,
err: &buffer,
}, &buffer
}
func NewFor(ctx Context) Context {
if ctx == nil {
return DefaultContext
}
return &outputContext{
in: ctx.StdIn(),
out: ctx.StdOut(),
err: ctx.StdErr(),
}
}
func (o *outputContext) StdOut() io.Writer {
if o == nil {
return os.Stdout
}
if o.out != nil {
return o.out
}
return o.parent.StdOut()
}
func (o *outputContext) StdErr() io.Writer {
if o == nil {
return os.Stderr
}
if o.err != nil {
return o.err
}
return o.parent.StdErr()
}
func (o *outputContext) StdIn() io.Reader {
if o == nil {
return os.Stdin
}
if o.in != nil {
return o.in
}
return o.parent.StdIn()
}
func WithInput(ctx Context, in io.Reader) Context {
if ctx == nil {
ctx = DefaultContext
}
if in == nil {
return ctx
}
return &outputContext{parent: ctx, in: in}
}
func WithOutput(ctx Context, out io.Writer) Context {
if ctx == nil {
ctx = DefaultContext
}
if out == nil {
return ctx
}
return &outputContext{parent: ctx, out: out}
}
func WithErrorOutput(ctx Context, err io.Writer) Context {
if ctx == nil {
ctx = DefaultContext
}
if err == nil {
return ctx
}
return &outputContext{parent: ctx, err: err}
}
func WithStdIO(ctx Context, r io.Reader, o io.Writer, e io.Writer) Context {
return &outputContext{parent: ctx, in: r, out: o, err: e}
}