forked from imjerrybao/apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
57 lines (46 loc) · 1.18 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
// Package exec proxies all commands.
package exec
import (
"fmt"
"os"
"os/exec"
"github.com/apex/apex/function"
"github.com/apex/log"
)
// Proxy is a wrapper around commands.
type Proxy struct {
Functions []*function.Function
Environment string
Region string
Role string
Dir string
}
// Run command in specified directory.
func (p *Proxy) Run(command string, args ...string) error {
log.WithFields(log.Fields{
"command": command,
"args": args,
}).Debug("exec")
cmd := exec.Command(command, args...)
cmd.Env = append(os.Environ(), p.functionEnvVars()...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = p.Dir
return cmd.Run()
}
func (p *Proxy) functionEnvVars() (args []string) {
args = append(args, fmt.Sprintf("apex_environment=%s", p.Environment))
if p.Role != "" {
args = append(args, fmt.Sprintf("apex_function_role=%s", p.Role))
}
for _, fn := range p.Functions {
config, err := fn.GetConfig()
if err != nil {
log.Debugf("can't fetch function config: %s", err.Error())
continue
}
args = append(args, fmt.Sprintf("apex_function_%s=%s", fn.Name, *config.Configuration.FunctionArn))
}
return args
}