-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommand.go
60 lines (49 loc) · 1.37 KB
/
command.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
package slss
import (
"bytes"
"fmt"
"os"
"os/exec"
"github.com/pkg/errors"
)
const (
awsAccessKeyIDTemplate = "AWS_ACCESS_KEY_ID=%v"
awsSecretAccessKeyTemplate = "AWS_SECRET_ACCESS_KEY=%v"
awsRegionTemplate = "AWS_REGION=%v"
commandErrorTemplate = "APEX commend failed: \n%v"
)
// APEXCommandExecutor represents the APEX command executor
type APEXCommandExecutor struct {
Config *Config
}
// Exec executes the specified APEX command
func (a *APEXCommandExecutor) Exec(command string, stdin *bytes.Buffer, args ...string) (string, error) {
var (
responseMessage bytes.Buffer
errorMessage bytes.Buffer
)
cmd := exec.Command(command, args...)
if stdin != nil {
cmd.Stdin = stdin
}
cmd.Stdout = &responseMessage
cmd.Stderr = &errorMessage
wd, err := os.Getwd()
if err != nil {
return "", errors.WithStack(err)
}
cmd.Dir = wd + "/lambda/"
cmd.Env = append(
os.Environ(),
fmt.Sprintf(awsAccessKeyIDTemplate, a.Config.AWS.AccessKeyID),
fmt.Sprintf(awsSecretAccessKeyTemplate, a.Config.AWS.SecretAccessKey),
fmt.Sprintf(awsRegionTemplate, a.Config.AWS.Region),
)
if err := cmd.Start(); err != nil {
return "", errors.Wrapf(err, commandErrorTemplate, errorMessage.String())
}
if err := cmd.Wait(); err != nil {
return "", errors.Wrapf(err, commandErrorTemplate, errorMessage.String())
}
return responseMessage.String(), nil
}