-
Notifications
You must be signed in to change notification settings - Fork 70
/
integration_validator.go
101 lines (74 loc) · 2.26 KB
/
integration_validator.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package validation
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type integrationType struct {
Name string `yaml:"name"`
Env map[string]string `yaml:"env"`
}
type configType struct {
Integrations []integrationType `yaml:"integrations"`
}
// Validate an integration using its <integrationName>-config.yml
// by iterating over the defined integrations and running the
// integration command with its defined environment variables.
// <integrationName>-config.yml is determined to be valid if every
// defined integration exits without error (exits with exit code 0).
// The <integrationName>-config.yml is located in the
// default configuration directory, which may vary by GOOS.
func ValidateIntegration(integrationName string) (string, error) {
configBasename := fmt.Sprintf("%s-config.yml", integrationName)
configPath := filepath.Join(ConfigurationsDirname, configBasename)
config, err := readConfig(configPath)
if err != nil {
return "", err
}
for _, integration := range config.Integrations {
integrationBasename := integration.Name
binPath := filepath.Join(IntegrationsDirname, integrationBasename)
cmd := exec.Command(binPath)
for k, v := range integration.Env {
env := fmt.Sprintf("%s=%s", k, v)
cmd.Env = append(cmd.Env, env)
}
var e strings.Builder
cmd.Stderr = &e
if err := cmd.Run(); err != nil {
stderr := e.String()
if stderr != "" {
s := strings.TrimSpace(stderr)
return "", fmt.Errorf("%w: %s", err, s)
}
return "", err
}
}
return "", nil
}
// Reads and unmarshals an <integrationName>-config.yml from the
// given configPath, returning a configType{} containing the
// defined integrations and their respective environments.
// Returns an empty configType{} and an error if:
// 1.) The file does not exist
// 2.) The file can not be read
// 3.) The file cannot be unmarshalled to a configType{}
func readConfig(configPath string) (configType, error) {
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
return configType{}, err
}
b, err := os.ReadFile(configPath)
if err != nil {
return configType{}, err
}
config := configType{}
err = yaml.Unmarshal(b, &config)
if err != nil {
return configType{}, err
}
return config, nil
}