mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-24 18:11:19 +01:00
v0.5.0-rc5: check release
This commit is contained in:
104
src/utils/io.go
104
src/utils/io.go
@@ -10,15 +10,8 @@ import (
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
)
|
||||
|
||||
// TODO: move to "utils/io"
|
||||
type (
|
||||
Reader interface {
|
||||
Read() ([]byte, E.NestedError)
|
||||
}
|
||||
|
||||
StdReader struct {
|
||||
r Reader
|
||||
}
|
||||
|
||||
FileReader struct {
|
||||
Path string
|
||||
}
|
||||
@@ -29,13 +22,6 @@ type (
|
||||
closed atomic.Bool
|
||||
}
|
||||
|
||||
StdReadCloser struct {
|
||||
r *ReadCloser
|
||||
}
|
||||
|
||||
ByteReader []byte
|
||||
NewByteReader = ByteReader
|
||||
|
||||
Pipe struct {
|
||||
r ReadCloser
|
||||
w io.WriteCloser
|
||||
@@ -44,49 +30,25 @@ type (
|
||||
}
|
||||
|
||||
BidirectionalPipe struct {
|
||||
pSrcDst Pipe
|
||||
pDstSrc Pipe
|
||||
pSrcDst *Pipe
|
||||
pDstSrc *Pipe
|
||||
}
|
||||
)
|
||||
|
||||
func NewFileReader(path string) *FileReader {
|
||||
return &FileReader{Path: path}
|
||||
}
|
||||
|
||||
func (r StdReader) Read() ([]byte, error) {
|
||||
return r.r.Read()
|
||||
}
|
||||
|
||||
func (r *FileReader) Read() ([]byte, E.NestedError) {
|
||||
return E.Check(os.ReadFile(r.Path))
|
||||
}
|
||||
|
||||
func (r ByteReader) Read() ([]byte, E.NestedError) {
|
||||
return r, E.Nil()
|
||||
}
|
||||
|
||||
func (r *ReadCloser) Read(p []byte) (int, E.NestedError) {
|
||||
func (r *ReadCloser) Read(p []byte) (int, error) {
|
||||
select {
|
||||
case <-r.ctx.Done():
|
||||
return 0, E.From(r.ctx.Err())
|
||||
return 0, r.ctx.Err()
|
||||
default:
|
||||
return E.Check(r.r.Read(p))
|
||||
return r.r.Read(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ReadCloser) Close() E.NestedError {
|
||||
func (r *ReadCloser) Close() error {
|
||||
if r.closed.Load() {
|
||||
return E.Nil()
|
||||
return nil
|
||||
}
|
||||
r.closed.Store(true)
|
||||
return E.From(r.r.Close())
|
||||
}
|
||||
|
||||
func (r StdReadCloser) Read(p []byte) (int, error) {
|
||||
return r.r.Read(p)
|
||||
}
|
||||
|
||||
func (r StdReadCloser) Close() error {
|
||||
return r.r.Close()
|
||||
}
|
||||
|
||||
@@ -100,35 +62,35 @@ func NewPipe(ctx context.Context, r io.ReadCloser, w io.WriteCloser) *Pipe {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pipe) Start() E.NestedError {
|
||||
return Copy(p.ctx, p.w, &StdReadCloser{&p.r})
|
||||
func (p *Pipe) Start() error {
|
||||
return Copy(p.ctx, p.w, &p.r)
|
||||
}
|
||||
|
||||
func (p *Pipe) Stop() E.NestedError {
|
||||
func (p *Pipe) Stop() error {
|
||||
p.cancel()
|
||||
return E.Join("error stopping pipe", p.r.Close(), p.w.Close())
|
||||
return E.JoinE("error stopping pipe", p.r.Close(), p.w.Close()).Error()
|
||||
}
|
||||
|
||||
func (p *Pipe) Write(b []byte) (int, E.NestedError) {
|
||||
return E.Check(p.w.Write(b))
|
||||
func (p *Pipe) Write(b []byte) (int, error) {
|
||||
return p.w.Write(b)
|
||||
}
|
||||
|
||||
func NewBidirectionalPipe(ctx context.Context, rw1 io.ReadWriteCloser, rw2 io.ReadWriteCloser) *BidirectionalPipe {
|
||||
return &BidirectionalPipe{
|
||||
pSrcDst: *NewPipe(ctx, rw1, rw2),
|
||||
pDstSrc: *NewPipe(ctx, rw2, rw1),
|
||||
pSrcDst: NewPipe(ctx, rw1, rw2),
|
||||
pDstSrc: NewPipe(ctx, rw2, rw1),
|
||||
}
|
||||
}
|
||||
|
||||
func NewBidirectionalPipeIntermediate(ctx context.Context, listener io.ReadCloser, client io.ReadWriteCloser, target io.ReadWriteCloser) *BidirectionalPipe {
|
||||
return &BidirectionalPipe{
|
||||
pSrcDst: *NewPipe(ctx, listener, client),
|
||||
pDstSrc: *NewPipe(ctx, client, target),
|
||||
pSrcDst: NewPipe(ctx, listener, client),
|
||||
pDstSrc: NewPipe(ctx, client, target),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *BidirectionalPipe) Start() E.NestedError {
|
||||
errCh := make(chan E.NestedError, 2)
|
||||
func (p *BidirectionalPipe) Start() error {
|
||||
errCh := make(chan error, 2)
|
||||
go func() {
|
||||
errCh <- p.pSrcDst.Start()
|
||||
}()
|
||||
@@ -136,34 +98,34 @@ func (p *BidirectionalPipe) Start() E.NestedError {
|
||||
errCh <- p.pDstSrc.Start()
|
||||
}()
|
||||
for err := range errCh {
|
||||
if err.HasError() {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return E.Nil()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *BidirectionalPipe) Stop() E.NestedError {
|
||||
return E.Join("error stopping pipe", p.pSrcDst.Stop(), p.pDstSrc.Stop())
|
||||
func (p *BidirectionalPipe) Stop() error {
|
||||
return E.JoinE("error stopping pipe", p.pSrcDst.Stop(), p.pDstSrc.Stop()).Error()
|
||||
}
|
||||
|
||||
func Copy(ctx context.Context, dst io.WriteCloser, src io.ReadCloser) E.NestedError {
|
||||
_, err := io.Copy(dst, StdReadCloser{&ReadCloser{ctx: ctx, r: src}})
|
||||
return E.From(err)
|
||||
func Copy(ctx context.Context, dst io.WriteCloser, src io.ReadCloser) error {
|
||||
_, err := io.Copy(dst, &ReadCloser{ctx: ctx, r: src})
|
||||
return err
|
||||
}
|
||||
|
||||
func LoadJson[T any](path string, pointer *T) E.NestedError {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return E.From(err)
|
||||
data, err := E.Check(os.ReadFile(path))
|
||||
if err.HasError() {
|
||||
return err
|
||||
}
|
||||
return E.From(json.Unmarshal(data, pointer))
|
||||
}
|
||||
|
||||
func SaveJson[T any](path string, pointer *T, perm os.FileMode) E.NestedError {
|
||||
data, err := json.Marshal(pointer)
|
||||
if err != nil {
|
||||
return E.From(err)
|
||||
data, err := E.Check(json.Marshal(pointer))
|
||||
if err.HasError() {
|
||||
return err
|
||||
}
|
||||
return E.From(os.WriteFile(path, data, perm))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user