/
stream.go
42 lines (37 loc) · 1006 Bytes
/
stream.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
package app
import (
"bytes"
"github.com/TNK-Studio/lazykube/pkg/log"
"io"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
"strings"
)
func newStream() genericclioptions.IOStreams {
return genericclioptions.IOStreams{
In: &bytes.Buffer{},
Out: &bytes.Buffer{},
ErrOut: &bytes.Buffer{},
}
}
func newStdStream() genericclioptions.IOStreams {
return genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
}
func streamCopyTo(streams genericclioptions.IOStreams, writer io.Writer) {
if _, err := io.Copy(writer, (streams.Out).(io.Reader)); err != nil {
log.Logger.Warningf("streamCopyTo - streams.Out copy error %s", err)
}
if _, err := io.Copy(writer, (streams.ErrOut).(io.Reader)); err != nil {
log.Logger.Warningf("streamCopyTo - streams.ErrOut copy error %s", err)
}
}
func streamToString(streams genericclioptions.IOStreams) string {
buf := new(strings.Builder)
streamCopyTo(streams, buf)
// check errors
return buf.String()
}