-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadditionalCommands.go
More file actions
51 lines (42 loc) · 1.17 KB
/
Copy pathadditionalCommands.go
File metadata and controls
51 lines (42 loc) · 1.17 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
package main
import (
"bytes"
"os"
"os/exec"
"strconv"
"strings"
)
type AdditionalCommand struct {
Command string `json:"command"`
Output []string `json:"output"`
}
func checkAdditionalCommands() []AdditionalCommand {
commands := []AdditionalCommand{}
// here we can check for commands that are set as ENV variables.
// The env variable should be named SIMPLESTATSCOMMANDX where X is 0 - 9
//
// if those env variables are set, this function will run them and populate
// the output in the JSON payload
for index := 0; index < 10; index++ {
envvar := os.Getenv("SIMPLESTATSCOMMAND" + strconv.Itoa(index))
if envvar == "" {
continue
}
commandParts := strings.Split(envvar, " ")
var result bytes.Buffer
// get the output of the command
commandRunner := exec.Command(commandParts[0])
if len(commandParts) > 1 {
commandRunner = exec.Command(commandParts[0], commandParts[1:]...)
}
commandRunner.Stdout = &result
commandRunner.Run()
output := strings.Split(result.String(), "\n")
currentCommand := AdditionalCommand{
Command: envvar,
Output: output[0 : len(output)-1],
}
commands = append(commands, currentCommand)
}
return commands
}