diff --git a/uio/linewriter.go b/uio/linewriter.go deleted file mode 100644 index a78835b..0000000 --- a/uio/linewriter.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2019 the u-root Authors. All rights reserved -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package uio - -import ( - "bytes" - "io" -) - -// LineWriter processes one line of log output at a time. -type LineWriter interface { - // OneLine is always called with exactly one line of output. - OneLine(b []byte) -} - -// FullLineWriter returns an io.Writer that waits for a full line of prints -// before calling w.Write on one line each. -func FullLineWriter(w LineWriter) io.WriteCloser { - return &fullLineWriter{w: w} -} - -type fullLineWriter struct { - w LineWriter - buffer []byte -} - -func (fsw *fullLineWriter) printBuf() { - bufs := bytes.Split(fsw.buffer, []byte{'\n'}) - for _, buf := range bufs { - if len(buf) != 0 { - fsw.w.OneLine(buf) - } - } - fsw.buffer = nil -} - -// Write implements io.Writer and buffers p until at least one full line is -// received. -func (fsw *fullLineWriter) Write(p []byte) (int, error) { - i := bytes.LastIndexByte(p, '\n') - if i == -1 { - fsw.buffer = append(fsw.buffer, p...) - } else { - fsw.buffer = append(fsw.buffer, p[:i]...) - fsw.printBuf() - fsw.buffer = append([]byte{}, p[i:]...) - } - return len(p), nil -} - -// Close implements io.Closer and flushes the buffer. -func (fsw *fullLineWriter) Close() error { - fsw.printBuf() - return nil -} diff --git a/uio/multiwriter.go b/uio/multiwriter.go deleted file mode 100644 index e05bc1b..0000000 --- a/uio/multiwriter.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2019 the u-root Authors. All rights reserved -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package uio - -import ( - "io" -) - -type multiCloser struct { - io.Writer - writers []io.Writer -} - -// Close implements io.Closer and closes any io.Writers that are also -// io.Closers. -func (mc *multiCloser) Close() error { - var allErr error - for _, w := range mc.writers { - if c, ok := w.(io.Closer); ok { - if err := c.Close(); err != nil { - allErr = err - } - } - } - return allErr -} - -// MultiWriteCloser is an io.MultiWriter that has an io.Closer and attempts to -// close those w's that have optional io.Closers. -func MultiWriteCloser(w ...io.Writer) io.WriteCloser { - return &multiCloser{ - Writer: io.MultiWriter(w...), - writers: w, - } -} diff --git a/uio/uiotest/uiotest.go b/uio/uiotest/uiotest.go deleted file mode 100644 index f5ae728..0000000 --- a/uio/uiotest/uiotest.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2020 the u-root Authors. All rights reserved -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package uiotest contains tests for uio functions. -package uiotest - -import ( - "io" - "testing" - "time" - - "github.com/u-root/uio/uio" -) - -// NowLog returns the current time formatted like the standard log package's -// timestamp. -func NowLog() string { - return time.Now().Format("2006/01/02 15:04:05") -} - -// TestLineWriter is an io.Writer that logs full lines of serial to tb. -func TestLineWriter(tb testing.TB, prefix string) io.WriteCloser { - tb.Helper() - if len(prefix) > 0 { - return uio.FullLineWriter(&testLinePrefixWriter{tb: tb, prefix: prefix}) - } - return uio.FullLineWriter(&testLineWriter{tb: tb}) -} - -// testLinePrefixWriter is an io.Writer that logs full lines of serial to tb. -type testLinePrefixWriter struct { - tb testing.TB - prefix string -} - -// OneLine implements a uio.LineWriter. -func (tsw *testLinePrefixWriter) OneLine(p []byte) { - tsw.tb.Logf("%s %s: %s", NowLog(), tsw.prefix, p) -} - -// testLineWriter is an io.Writer that logs full lines of serial to tb. -type testLineWriter struct { - tb testing.TB -} - -// OneLine implements a uio.LineWriter. -func (tsw *testLineWriter) OneLine(p []byte) { - tsw.tb.Logf("%s: %s", NowLog(), p) -}