From 5f371ed1ebc70bf2da11433224571816b25e60df Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Wed, 12 Jun 2024 10:39:19 +0100 Subject: [PATCH] remove unused files --- pkg/utils/capture.go | 40 ----------------------------- pkg/utils/capture_test.go | 18 ------------- pkg/utils/channel.go | 54 --------------------------------------- pkg/utils/mapreduce.go | 22 ---------------- pkg/utils/tee.go | 40 ----------------------------- 5 files changed, 174 deletions(-) delete mode 100644 pkg/utils/capture.go delete mode 100644 pkg/utils/capture_test.go delete mode 100644 pkg/utils/channel.go delete mode 100644 pkg/utils/mapreduce.go delete mode 100644 pkg/utils/tee.go diff --git a/pkg/utils/capture.go b/pkg/utils/capture.go deleted file mode 100644 index 3e19989..0000000 --- a/pkg/utils/capture.go +++ /dev/null @@ -1,40 +0,0 @@ -package utils - -import ( - "bytes" - "io" - "os" -) - -type StdoutCapture struct { - originalStdout *os.File - r *os.File - w *os.File - buffer bytes.Buffer -} - -func NewStdoutCapture() *StdoutCapture { - return &StdoutCapture{} -} - -func (oc *StdoutCapture) StartCapture() { - oc.originalStdout = os.Stdout - - r, w, _ := os.Pipe() - os.Stdout = w - - oc.r = r - oc.w = w -} - -func (oc *StdoutCapture) StopCapture() { - os.Stdout = oc.originalStdout - oc.w.Close() - - io.Copy(&oc.buffer, oc.r) - oc.r.Close() -} - -func (oc *StdoutCapture) GetCapturedOutput() string { - return oc.buffer.String() -} diff --git a/pkg/utils/capture_test.go b/pkg/utils/capture_test.go deleted file mode 100644 index ff3cc7b..0000000 --- a/pkg/utils/capture_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package utils_test - -import ( - "fmt" - "testing" - - "github.com/WangYihang/gojob/pkg/utils" -) - -func TestOutputCapture(t *testing.T) { - oc := utils.NewStdoutCapture() - oc.StartCapture() - fmt.Println("hello") - oc.StopCapture() - if oc.GetCapturedOutput() != "hello\n" { - t.Fail() - } -} diff --git a/pkg/utils/channel.go b/pkg/utils/channel.go deleted file mode 100644 index a4376da..0000000 --- a/pkg/utils/channel.go +++ /dev/null @@ -1,54 +0,0 @@ -package utils - -import "sync" - -// Fanin takes a slice of channels and returns a single channel that -func Fanin[T interface{}](cs []chan T) chan T { - var wg sync.WaitGroup - out := make(chan T) - output := func(c chan T) { - for n := range c { - out <- n - } - wg.Done() - } - wg.Add(len(cs)) - for _, c := range cs { - go output(c) - } - go func() { - wg.Wait() - close(out) - }() - return out -} - -// Fanout takes a channel and returns a slice of channels -// the item in the input channel will be distributed to the output channels -func Fanout[T interface{}](in chan *T, n int) []chan *T { - cs := make([]chan *T, n) - for i := 0; i < n; i++ { - cs[i] = make(chan *T) - go func(c chan *T) { - for n := range in { - c <- n - } - close(c) - }(cs[i]) - } - return cs -} - -// Filter takes a channel and returns a channel with the items that pass the filter -func Filter[T interface{}](in chan T, f func(T) bool) chan T { - out := make(chan T) - go func() { - defer close(out) - for line := range in { - if f(line) { - out <- line - } - } - }() - return out -} diff --git a/pkg/utils/mapreduce.go b/pkg/utils/mapreduce.go deleted file mode 100644 index 9b9a8ce..0000000 --- a/pkg/utils/mapreduce.go +++ /dev/null @@ -1,22 +0,0 @@ -package utils - -// Map takes a channel and returns a channel with the items that pass the filter -func Map[T interface{}, U interface{}](in chan T, f func(T) U) chan U { - out := make(chan U) - go func() { - defer close(out) - for line := range in { - out <- f(line) - } - }() - return out -} - -// Reduce takes a channel and returns a channel with the items that pass the filter -func Reduce[T interface{}](in chan T, f func(T, T) T) T { - var result T - for line := range in { - result = f(result, line) - } - return result -} diff --git a/pkg/utils/tee.go b/pkg/utils/tee.go deleted file mode 100644 index f7804b6..0000000 --- a/pkg/utils/tee.go +++ /dev/null @@ -1,40 +0,0 @@ -package utils - -import ( - "io" -) - -// TeeWriterCloser structure, used to write to and close multiple io.WriteClosers -type TeeWriterCloser struct { - writers []io.WriteCloser -} - -// NewTeeWriterCloser creates a new instance of TeeWriterCloser -func NewTeeWriterCloser(writers ...io.WriteCloser) *TeeWriterCloser { - return &TeeWriterCloser{ - writers: writers, - } -} - -// Write implements the io.Writer interface -// It writes the given byte slice to all the contained writers -func (t *TeeWriterCloser) Write(p []byte) (n int, err error) { - for _, w := range t.writers { - n, err = w.Write(p) - if err != nil { - return - } - } - return len(p), nil -} - -// Close implements the io.Closer interface -// It closes all the contained writers and returns the first encountered error -func (t *TeeWriterCloser) Close() (err error) { - for _, w := range t.writers { - if cerr := w.Close(); cerr != nil && err == nil { - err = cerr // Record the first encountered error - } - } - return -}