/
source_exec.go
74 lines (60 loc) · 1.92 KB
/
source_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
62
63
64
65
66
67
68
69
70
71
72
73
74
package source
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
kitlog "github.com/go-kit/kit/log"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/pkg/errors"
)
type SourceExec struct {
Command []string `json:"command"`
}
func (s SourceExec) Validate() error {
return validation.ValidateStruct(&s,
validation.Field(&s.Command, validation.Length(1, 0).
Error("must provide at least a command, if no args")),
)
}
func (s SourceExec) String() string {
return fmt.Sprintf("exec (command=%s)", strings.Join(s.Command, ","))
}
func (s SourceExec) Load(ctx context.Context, logger kitlog.Logger) ([]*SourceEntry, error) {
var (
command = s.Command[0]
args = s.Command[1:]
)
cmd := exec.CommandContext(ctx, command, args...)
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = os.Stderr // stderr is streamed to the parent terminal
err := cmd.Run()
if err != nil {
// If the exec'd command fails, then it's sometimes useful to see the standard output
// that it produced. This is especially relevant when the program doesn't behave
// according to conventions, and the error message isn't actually present in stderr.
// In some cases though, the stream of data could be very large, so we deliberately
// only show the last 1KiB, to avoid filling up the logs/terminal.
// We write this output back onto stderr, to play nice with any downstream tooling.
limit := 1024
stdout := output.Bytes()
if len(stdout) > limit {
fmt.Fprintln(os.Stderr, "last 1KiB of failing command's stdout:")
stdout = stdout[len(stdout)-limit:]
} else if len(stdout) > 0 {
fmt.Fprintln(os.Stderr, "failing command's stdout:")
}
fmt.Fprintln(os.Stderr, string(stdout))
return nil, errors.Wrap(err, "error running exec command")
}
entries := []*SourceEntry{
{
Origin: fmt.Sprintf("exec: %s", strings.Join(s.Command, " ")),
Content: output.Bytes(),
},
}
return entries, nil
}