github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/streams/tee.go (about)

     1  package streams
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/lmorg/murex/config"
     9  	"github.com/lmorg/murex/lang/stdio"
    10  )
    11  
    12  /*func init() {
    13  	stdio.RegisterPipe("tee", func(string) (stdio.Io, error) {
    14  		return nil, errors.New("`tee` is a system device used for `test`. It's user creation isn't yet supported but might be included in a future release")
    15  	})
    16  }*/
    17  
    18  // Tee is a stream interface with two output streams
    19  // (like the `tee` command on UNIX/Linux)
    20  type Tee struct {
    21  	primary   stdio.Io
    22  	secondary Stdin
    23  }
    24  
    25  // NewTee creates a new tee stdio interface
    26  func NewTee(primary stdio.Io) (primaryTee *Tee, secondaryTee *Stdin) {
    27  	primaryTee = new(Tee)
    28  	primaryTee.primary = primary
    29  	primaryTee.secondary.max = 0
    30  	primaryTee.secondary.ctx = context.Background()
    31  	secondaryTee = &primaryTee.secondary
    32  	return
    33  }
    34  
    35  // IsTTY calls the primary STDOUT stream in tee to see if it's a TTY
    36  func (tee *Tee) IsTTY() bool {
    37  	return tee.primary.IsTTY()
    38  }
    39  
    40  func (tee *Tee) File() *os.File {
    41  	return tee.primary.File()
    42  }
    43  
    44  // Stats is stored against the primary STDOUT stream in tee
    45  func (tee *Tee) Stats() (uint64, uint64) {
    46  	return tee.primary.Stats()
    47  }
    48  
    49  // Read from STDIN (uses primary tee stream)
    50  func (tee *Tee) Read(p []byte) (int, error) {
    51  	return tee.primary.Read(p)
    52  }
    53  
    54  // ReadLine reads a line from STDIN (uses the primary tee stream)
    55  func (tee *Tee) ReadLine(callback func([]byte)) error {
    56  	return tee.primary.ReadLine(callback)
    57  }
    58  
    59  // ReadArray reads an array from STDIN (uses the primary tee stream)
    60  func (tee *Tee) ReadArray(ctx context.Context, callback func([]byte)) error {
    61  	return tee.primary.ReadArray(ctx, callback)
    62  }
    63  
    64  // ReadArrayWithType reads an array from STDIN (uses the primary tee stream)
    65  func (tee *Tee) ReadArrayWithType(ctx context.Context, callback func(interface{}, string)) error {
    66  	return tee.primary.ReadArrayWithType(ctx, callback)
    67  }
    68  
    69  // ReadMap reads a hash table from STDIN (uses the primary tee stream)
    70  func (tee *Tee) ReadMap(config *config.Config, callback func(*stdio.Map)) error {
    71  	return tee.primary.ReadMap(config, callback)
    72  }
    73  
    74  // ReadAll from STDIN (uses the primary tee stream)
    75  func (tee *Tee) ReadAll() ([]byte, error) {
    76  	return tee.primary.ReadAll()
    77  }
    78  
    79  // Write is the standard Writer interface Write() method.
    80  func (tee *Tee) Write(p []byte) (int, error) {
    81  	tee.secondary.Write(p)
    82  	return tee.primary.Write(p)
    83  }
    84  
    85  // Writeln just calls Write() but with an appended, OS specific, new line.
    86  func (tee *Tee) Writeln(p []byte) (int, error) {
    87  	tee.secondary.Writeln(p)
    88  	return tee.primary.Writeln(p)
    89  }
    90  
    91  // WriteArray performs data type specific buffered writes to an stdio.Io interface
    92  func (tee *Tee) WriteArray(dataType string) (stdio.ArrayWriter, error) {
    93  	return stdio.WriteArray(tee, dataType)
    94  }
    95  
    96  // Open the stream.Io interface for another dependant
    97  func (tee *Tee) Open() {
    98  	tee.primary.Open()
    99  }
   100  
   101  // Close the stream.Io interface
   102  func (tee *Tee) Close() {
   103  	tee.primary.Close()
   104  }
   105  
   106  // ForceClose forces the stream.Io interface to close. This should only be called by a STDIN reader
   107  func (tee *Tee) ForceClose() {
   108  	tee.primary.ForceClose()
   109  }
   110  
   111  // WriteTo reads from the stream.Io interface and writes to a destination
   112  // io.Writer interface
   113  func (tee *Tee) WriteTo(w io.Writer) (n int64, err error) {
   114  	return tee.primary.WriteTo(w)
   115  }
   116  
   117  // GetDataType returns the murex data type for the stream.Io interface
   118  func (tee *Tee) GetDataType() (dt string) {
   119  	return tee.primary.GetDataType()
   120  }
   121  
   122  // SetDataType defines the murex data type for the stream.Io interface
   123  func (tee *Tee) SetDataType(dt string) {
   124  	tee.secondary.SetDataType(dt)
   125  	tee.primary.SetDataType(dt)
   126  }