Bladeren bron

Reworked config structure for script data to ensure only one dataset can be provided per script

Brendan Abolivier 7 jaren geleden
bovenliggende
commit
f7395f5e06
Getekend door: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 gewijzigde bestanden met toevoegingen van 22 en 13 verwijderingen
  1. 22
    13
      src/metrics-alerting/config/config.go

+ 22
- 13
src/metrics-alerting/config/config.go Bestand weergeven

@@ -2,6 +2,7 @@ package config
2 2
 
3 3
 import (
4 4
 	"bufio"
5
+	"fmt"
5 6
 	"io"
6 7
 	"io/ioutil"
7 8
 	"os"
@@ -28,11 +29,13 @@ type SMTPSettings struct {
28 29
 }
29 30
 
30 31
 type ScriptDataSource struct {
31
-	// Data to load from a file containing the content for the slide, one
32
-	// element per line
33
-	FromFile map[string]string `yaml:"from_file,omitempty"`
34
-	// Plain data
35
-	Plain map[string][]string `yaml:"plain,omitempty"`
32
+	// Type of the data source (either "plain" or "file")
33
+	Source string `yaml:"source"`
34
+	// Key of the data
35
+	Key string `yaml:"key"`
36
+	// Data value (or 1-element slice containing the path to the file containing
37
+	// the values)
38
+	Value []string `yaml:"value"`
36 39
 }
37 40
 
38 41
 type Script struct {
@@ -52,8 +55,8 @@ type Script struct {
52 55
 	// The labels that will be mentioned in the email subject, only required if
53 56
 	// the action is "email"
54 57
 	IdentifyingLabels []string `yaml:"identifying_labels,omitempty"`
55
-	// Data to use in the script
56
-	DataSource ScriptDataSource `yaml:"script_data,omitempty"`
58
+	// Source/value of the data to use in the script
59
+	ScriptDataSource ScriptDataSource `yaml:"script_data,omitempty"`
57 60
 	// Loaded data
58 61
 	ScriptData map[string][]string
59 62
 }
@@ -90,8 +93,9 @@ func (cfg *Config) loadData() error {
90 93
 	var isPrefix bool
91 94
 	for i, script := range cfg.Scripts {
92 95
 		script.ScriptData = make(map[string][]string)
93
-		for key, fileName := range script.DataSource.FromFile {
94
-			fp, err := os.Open(fileName)
96
+		switch script.ScriptDataSource.Source {
97
+		case "file":
98
+			fp, err := os.Open(script.ScriptDataSource.Value[0])
95 99
 			if err != nil {
96 100
 				return err
97 101
 			}
@@ -114,12 +118,17 @@ func (cfg *Config) loadData() error {
114 118
 
115 119
 				// Prevent processing empty line at the end of file
116 120
 				if len(line) > 0 {
117
-					script.ScriptData[key] = append(script.ScriptData[key], line)
121
+					script.ScriptData[script.ScriptDataSource.Key] = append(
122
+						script.ScriptData[script.ScriptDataSource.Key], line,
123
+					)
118 124
 				}
119 125
 			}
120
-		}
121
-		for key, slice := range script.DataSource.Plain {
122
-			script.ScriptData[key] = slice
126
+			break
127
+		case "plain":
128
+			script.ScriptData[script.ScriptDataSource.Key] = script.ScriptDataSource.Value
129
+			break
130
+		default:
131
+			return fmt.Errorf("invalid data source: %s")
123 132
 		}
124 133
 
125 134
 		cfg.Scripts[i] = script