-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.go
More file actions
73 lines (62 loc) · 1.73 KB
/
filter.go
File metadata and controls
73 lines (62 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package command
import (
"context"
"io"
)
// NewFilter creates a bidirectional command filter with full
// Read/Write/Close access.
//
// The returned io.ReadWriteCloser provides direct access to the command's
// stdin (Write), stdout (Read), and stdin close signal (Close).
//
// If the underlying command does not support writing (is read-only), Write()
// will return an error. Close() closes stdin if supported, otherwise it is
// a no-op.
//
// NewFilter is primarily useful with command.Copy for pipeline composition.
// For most use cases, prefer NewReader (read-only with cancellation) or
// NewWriter (write-only with completion wait).
func NewFilter(
ctx context.Context, m Machine, args ...string,
) io.ReadWriteCloser {
buf := m.Command(ctx, args...)
return &filter{buf: buf}
}
// Deprecated: Use NewFilter instead.
var NewStream = NewFilter
type filter struct {
buf Buffer
}
func (f *filter) Read(p []byte) (int, error) {
return f.buf.Read(p)
}
func (f *filter) Write(p []byte) (int, error) {
if wb, ok := f.buf.(WriteBuffer); ok {
return wb.Write(p)
}
return 0, ErrReadOnly
}
func (f *filter) Close() error {
if wb, ok := f.buf.(WriteBuffer); ok {
return wb.Close()
}
// Read-only commands - Close is a no-op
return nil
}
// ReadFrom implements io.ReaderFrom for optimized copying that auto-closes
// stdin when the source reaches EOF.
// This allows io.Copy to automatically close stdin in pipeline stages.
func (f *filter) ReadFrom(src io.Reader) (n int64, err error) {
wb, ok := f.buf.(WriteBuffer)
if !ok {
return 0, ErrReadOnly
}
// Copy from source to command stdin
n, err = io.Copy(wb, src)
// Auto-close stdin after copy completes
closeErr := wb.Close()
if err == nil {
err = closeErr
}
return n, err
}