switch to go vendoring
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
||||
# Stdio
|
||||
|
||||
`stdio` provide some standard IO util functions.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
go get github.com/gookit/goutil/stdio
|
||||
```
|
||||
|
||||
## Go docs
|
||||
|
||||
- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/stdio)
|
||||
|
||||
## Usage
|
||||
|
||||
Please see tests.
|
||||
|
||||
## Testings
|
||||
|
||||
```shell
|
||||
go test -v ./stdio/...
|
||||
```
|
||||
|
||||
Test limit by regexp:
|
||||
|
||||
```shell
|
||||
go test -v -run ^TestSetByKeys ./stdio/...
|
||||
```
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package stdio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// QuietFprint to writer, will ignore error
|
||||
func QuietFprint(w io.Writer, ss ...string) {
|
||||
_, _ = fmt.Fprint(w, strings.Join(ss, ""))
|
||||
}
|
||||
|
||||
// QuietFprintf to writer, will ignore error
|
||||
func QuietFprintf(w io.Writer, tpl string, vs ...any) {
|
||||
_, _ = fmt.Fprintf(w, tpl, vs...)
|
||||
}
|
||||
|
||||
// QuietFprintln to writer, will ignore error
|
||||
func QuietFprintln(w io.Writer, ss ...string) {
|
||||
_, _ = fmt.Fprintln(w, strings.Join(ss, ""))
|
||||
}
|
||||
|
||||
// QuietWriteString to writer, will ignore error
|
||||
func QuietWriteString(w io.Writer, ss ...string) {
|
||||
_, _ = io.WriteString(w, strings.Join(ss, ""))
|
||||
}
|
||||
|
||||
// DiscardReader anything from the reader
|
||||
func DiscardReader(src io.Reader) {
|
||||
_, _ = io.Copy(io.Discard, src)
|
||||
}
|
||||
|
||||
// MustReadReader read contents from io.Reader, will panic on error
|
||||
func MustReadReader(r io.Reader) []byte {
|
||||
bs, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bs
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// Package stdio provide some standard IO util functions.
|
||||
package stdio
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package stdio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// WriteWrapper warp io.Writer support more operate methods.
|
||||
type WriteWrapper struct {
|
||||
Out io.Writer
|
||||
}
|
||||
|
||||
// NewWriteWrapper instance
|
||||
func NewWriteWrapper(w io.Writer) *WriteWrapper {
|
||||
return &WriteWrapper{Out: w}
|
||||
}
|
||||
|
||||
// Write bytes data
|
||||
func (w *WriteWrapper) Write(p []byte) (n int, err error) {
|
||||
return w.Out.Write(p)
|
||||
}
|
||||
|
||||
// Writef data to output
|
||||
func (w *WriteWrapper) Writef(tpl string, vs ...any) (n int, err error) {
|
||||
return fmt.Fprintf(w.Out, tpl, vs...)
|
||||
}
|
||||
|
||||
// WriteByte data
|
||||
func (w *WriteWrapper) WriteByte(c byte) error {
|
||||
_, err := w.Out.Write([]byte{c})
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteString data
|
||||
func (w *WriteWrapper) WriteString(s string) (n int, err error) {
|
||||
return w.Out.Write([]byte(s))
|
||||
}
|
||||
|
||||
// String get write data string
|
||||
func (w *WriteWrapper) String() string {
|
||||
if sw, ok := w.Out.(fmt.Stringer); ok {
|
||||
return sw.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user