Преглед на файлове

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

Brendan Abolivier преди 7 години
родител
ревизия
f7395f5e06
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
променени са 1 файла, в които са добавени 22 реда и са изтрити 13 реда
  1. 22
    13
      src/metrics-alerting/config/config.go

+ 22
- 13
src/metrics-alerting/config/config.go Целия файл

2
 
2
 
3
 import (
3
 import (
4
 	"bufio"
4
 	"bufio"
5
+	"fmt"
5
 	"io"
6
 	"io"
6
 	"io/ioutil"
7
 	"io/ioutil"
7
 	"os"
8
 	"os"
28
 }
29
 }
29
 
30
 
30
 type ScriptDataSource struct {
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
 type Script struct {
41
 type Script struct {
52
 	// The labels that will be mentioned in the email subject, only required if
55
 	// The labels that will be mentioned in the email subject, only required if
53
 	// the action is "email"
56
 	// the action is "email"
54
 	IdentifyingLabels []string `yaml:"identifying_labels,omitempty"`
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
 	// Loaded data
60
 	// Loaded data
58
 	ScriptData map[string][]string
61
 	ScriptData map[string][]string
59
 }
62
 }
90
 	var isPrefix bool
93
 	var isPrefix bool
91
 	for i, script := range cfg.Scripts {
94
 	for i, script := range cfg.Scripts {
92
 		script.ScriptData = make(map[string][]string)
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
 			if err != nil {
99
 			if err != nil {
96
 				return err
100
 				return err
97
 			}
101
 			}
114
 
118
 
115
 				// Prevent processing empty line at the end of file
119
 				// Prevent processing empty line at the end of file
116
 				if len(line) > 0 {
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
 		cfg.Scripts[i] = script
134
 		cfg.Scripts[i] = script