-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (44 loc) · 962 Bytes
/
main.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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"github.com/akerl/prospectus/v3/plugin"
)
type config struct {
Command string `json:"command"`
Env map[string]string `json:"env"`
}
func run(cfg config) error {
if cfg.Command == "" {
return fmt.Errorf("required arg not provided: command")
}
cmd := exec.Command(cfg.Command)
for k, v := range cfg.Env {
cmd.Env = append(cmd.Environ(), fmt.Sprintf("%s=%s", k, v))
}
var stdoutBytes bytes.Buffer
var stderrBytes bytes.Buffer
cmd.Stdout = &stdoutBytes
cmd.Stderr = &stderrBytes
err := cmd.Run()
if err != nil {
return fmt.Errorf("%s: %s", err, stderrBytes.String())
}
fmt.Printf(stdoutBytes.String())
return nil
}
func main() {
cfg := config{}
_, err := plugin.ParseConfig(&cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %s", err)
os.Exit(1)
}
err = run(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
}