forked from thomaspoignant/go-feature-flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinter.go
49 lines (42 loc) · 1.01 KB
/
linter.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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/thomaspoignant/go-feature-flag/model/dto"
"k8s.io/apimachinery/pkg/util/yaml"
)
type Linter struct {
InputFile string
InputFormat string
}
func (l *Linter) Lint() []error {
dat, err := os.ReadFile(l.InputFile)
if err != nil {
return []error{err}
}
var flags map[string]dto.DTO
switch strings.ToLower(l.InputFormat) {
case "toml":
err = toml.Unmarshal(dat, &flags)
case "json":
err = json.Unmarshal(dat, &flags)
case "yaml":
err = yaml.Unmarshal(dat, &flags)
default:
return []error{fmt.Errorf("%s: invalid input format: %s", l.InputFile, l.InputFormat)}
}
if err != nil {
return []error{fmt.Errorf("%s: could not parse file: %w", l.InputFile, err)}
}
errs := make([]error, 0)
for key, flagDto := range flags {
flag := flagDto.Convert()
if err := flag.IsValid(); err != nil {
errs = append(errs, fmt.Errorf("%s: invalid flag %s: %w", l.InputFile, key, err))
}
}
return errs
}