forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent.go
More file actions
80 lines (66 loc) · 2.05 KB
/
agent.go
File metadata and controls
80 lines (66 loc) · 2.05 KB
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
75
76
77
78
79
80
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package agents
import (
"fmt"
"io"
"io/ioutil"
"os/exec"
"path"
"syscall"
log "github.com/sirupsen/logrus"
)
// AgentProcess is the common interface exposed by both internal and external agent processes
type AgentProcess interface {
Name() string
}
// ExternalAgentProcess represents an external agent process
type ExternalAgentProcess struct {
cmd *exec.Cmd
}
// NewExternalAgentProcess returns a new external agent process
func NewExternalAgentProcess(path string, env []string, stdoutWriter io.Writer, stderrWriter io.Writer) ExternalAgentProcess {
command := exec.Command(path)
command.Env = env
command.Stdout = NewNewlineSplitWriter(stdoutWriter)
command.Stderr = NewNewlineSplitWriter(stderrWriter)
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return ExternalAgentProcess{
cmd: command,
}
}
// Name returns the name of the agent
// For external agents is the executable name
func (a *ExternalAgentProcess) Name() string {
return path.Base(a.cmd.Path)
}
func (a *ExternalAgentProcess) Pid() int {
return a.cmd.Process.Pid
}
// Start starts an external agent process
func (a *ExternalAgentProcess) Start() error {
return a.cmd.Start()
}
// Wait waits for the external agent process to exit
func (a *ExternalAgentProcess) Wait() error {
return a.cmd.Wait()
}
// String is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print.
func (a *ExternalAgentProcess) String() string {
return fmt.Sprintf("%s (%s)", a.Name(), a.cmd.Path)
}
// ListExternalAgentPaths return a list of external agents found in a given directory
func ListExternalAgentPaths(root string) []string {
var agentPaths []string
files, err := ioutil.ReadDir(root)
if err != nil {
log.WithError(err).Warning("Cannot list external agents")
return agentPaths
}
for _, file := range files {
if !file.IsDir() {
agentPaths = append(agentPaths, path.Join(root, file.Name()))
}
}
return agentPaths
}