-
Notifications
You must be signed in to change notification settings - Fork 53
/
mode.go
64 lines (55 loc) · 1.25 KB
/
mode.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
61
62
63
64
package meta
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"time"
)
const (
PluginModeEnvVar = "OPNI_PLUGIN_MODE"
)
type PluginMode string
const (
ModeGateway PluginMode = "gateway"
ModeAgent PluginMode = "agent"
ModeListModes PluginMode = "__list_modes__"
)
func (m PluginMode) IsValid() bool {
return m == ModeGateway || m == ModeAgent || m == ModeListModes
}
type ModeSet map[PluginMode]SchemeFunc
type ModeList struct {
Modes []PluginMode `json:"modes"`
}
func (m ModeSet) MarshalJSON() ([]byte, error) {
var modes []PluginMode
for mode := range m {
if mode == ModeListModes {
continue
}
modes = append(modes, mode)
}
return json.Marshal(ModeList{
Modes: modes,
})
}
func QueryPluginModes(pluginFilename string) (ModeList, error) {
outBuf := new(bytes.Buffer)
ctx, ca := context.WithTimeout(context.Background(), 1*time.Second)
defer ca()
cmd := exec.CommandContext(ctx, pluginFilename)
cmd.Env = []string{fmt.Sprintf("%s=%s", PluginModeEnvVar, ModeListModes)}
cmd.Stdout = outBuf
cmd.Stderr = nil
cmd.Stdin = nil
if err := cmd.Run(); err != nil {
return ModeList{}, err
}
var info ModeList
if err := json.NewDecoder(outBuf).Decode(&info); err != nil {
return ModeList{}, err
}
return info, nil
}