Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
56 lines
1003 B
Go
56 lines
1003 B
Go
package exec
|
|
|
|
import "io"
|
|
|
|
// Write to many streamings and if closed remove from list
|
|
type Writers struct {
|
|
Std []io.Writer
|
|
Closed bool
|
|
}
|
|
|
|
func (p *Writers) AddNewWriter(w io.Writer) {
|
|
if !p.Closed {
|
|
p.Std = append(p.Std, w)
|
|
}
|
|
}
|
|
|
|
func (p *Writers) Close() error {
|
|
p.Closed = true
|
|
return nil
|
|
}
|
|
|
|
func (p *Writers) Write(w []byte) (int, error) {
|
|
if p.Closed {
|
|
return 0, io.EOF
|
|
}
|
|
|
|
for indexWriter := range p.Std {
|
|
if p.Std[indexWriter] == nil {
|
|
continue
|
|
}
|
|
switch _, err := p.Std[indexWriter].Write(w); err {
|
|
case nil:
|
|
continue
|
|
case io.EOF, io.ErrUnexpectedEOF:
|
|
p.Std[indexWriter] = nil
|
|
default:
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
for writeIndex := 0; writeIndex < len(p.Std); writeIndex++ {
|
|
if p.Std[writeIndex] != nil {
|
|
continue
|
|
} else if writeIndex == len(p.Std) {
|
|
p.Std = p.Std[writeIndex-1:]
|
|
} else if writeIndex == 0 {
|
|
p.Std = p.Std[1:]
|
|
} else {
|
|
p.Std = append(p.Std[writeIndex:], p.Std[writeIndex+1:]...)
|
|
}
|
|
writeIndex -= 2
|
|
}
|
|
|
|
return len(w), nil
|
|
}
|