-
Notifications
You must be signed in to change notification settings - Fork 4
/
source_exec.go
48 lines (40 loc) · 899 Bytes
/
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
package source
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
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)),
)
}
func (s SourceExec) Load(ctx context.Context) ([]*SourceEntry, error) {
var (
command = s.Command[0]
args = s.Command[1:]
)
cmd := exec.CommandContext(ctx, command, args...)
cmd.Stderr = os.Stderr // allow stderr output
var output bytes.Buffer
cmd.Stdout = &output
err := cmd.Run()
if err != nil {
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
}