-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
61 lines (53 loc) · 1.15 KB
/
exec.go
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
package cmdio
import (
"bytes"
"fmt"
"io"
"strings"
"golang.org/x/sync/errgroup"
)
func run(cmd io.Reader) error {
a, ok := cmd.(Attacher)
if !ok {
// If this command does not implement Attacher, stream it to stdout
// (and stderr, if applicable).
if l, ok := cmd.(Logger); ok {
l.Log(stderr)
}
fmt.Fprintln(Trace, strings.TrimRight(fmt.Sprintf("%v", cmd), "\n"))
_, err := io.Copy(stdout, cmd)
return err
}
if err := a.Attach(); err != nil {
return err
}
fmt.Fprintln(Trace, strings.TrimRight(fmt.Sprintf("%v", cmd), "\n"))
_, err := cmd.Read(nil)
if err == io.EOF {
err = nil
}
return err
}
func get(cmd io.Reader) (Result, error) {
fmt.Fprintln(Trace, strings.TrimRight(fmt.Sprintf("%v", cmd), "\n"))
var r Result
var wg errgroup.Group
var log bytes.Buffer
out := make(chan string)
if l, ok := cmd.(Logger); ok {
l.Log(&log)
}
wg.Go(func() error {
buf, err := io.ReadAll(cmd)
out <- strings.TrimRight(string(buf), "\n")
return err
})
r.Cmd = readWriter(cmd)
r.Out = <-out
err := wg.Wait()
r.Log = strings.TrimRight(log.String(), "\n")
if c, ok := cmd.(Coder); ok {
r.Code = c.Code()
}
return r, err
}