-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess.go
117 lines (90 loc) · 2.72 KB
/
process.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/ghodss/yaml"
"io/ioutil"
)
func processBytes(bytes []byte, params LinterParams) (CombinedResultMap, error) {
//preflight with optional conversion from YAMLs
err := preflightAsset(&bytes)
if err != nil {
return nil, errors.New(fmt.Sprintf("input failed preflight check: %v", err))
}
//make sure config objects are presented as a list
err = makeList(&bytes)
if err != nil {
return nil, err
}
var config Config
if err = json.Unmarshal(bytes, &config); err != nil {
return nil, errors.New(fmt.Sprintf("can't unmarshal data: %v", err))
}
//objects of type "Template" have "objects" not "items"
//standardize on "items"
if len(config.Objects) > 0 && len(config.Items) == 0 {
config.Items = config.Objects //copy pointer
}
//for POST req access, pick up custom settings from JSON obj
if config.CustomNamespacePattern != "" {
params.NamespacePattern = config.CustomNamespacePattern
}
if config.CustomNamePattern != "" {
params.NamePattern = config.CustomNamePattern
}
if config.CustomContainerPattern != "" {
params.ContainerPattern = config.CustomContainerPattern
}
if config.CustomEnvPattern != "" {
params.EnvPattern = config.CustomEnvPattern
}
combined := make(CombinedResultMap)
items := ItemsFiltered(params.CheckPattern)
if items == nil {
return nil, errors.New("no checks selected")
}
for _, item := range items {
result, err := item.Lint(&config, params)
if err != nil {
return nil, err
}
err = postprocessResult(&result, params)
if err != nil {
return nil, err
}
combined[item.Name()] = result
}
combined["summary"] = summary(&config, &combined, params)
return combined, nil
}
func processFile(path string, params LinterParams) (string, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return "", errors.New(fmt.Sprintf("can't read %s: %v", path, err))
}
combinedResultMap, err := processBytes(bytes, params)
if err != nil {
return "", errors.New(fmt.Sprintf("can't process %s: %s", path, err))
}
return assembleOutput(combinedResultMap, params.Output)
}
func assembleOutput(combinedResultMap CombinedResultMap, output string) (string, error) {
switch output {
case "json":
json, err := json.MarshalIndent(combinedResultMap, "", " ")
if err != nil {
return "", errors.New(fmt.Sprintf("can't marshal JSON %v", combinedResultMap))
}
return string(json), nil
case "yaml":
yaml, err := yaml.Marshal(combinedResultMap)
if err != nil {
return "", errors.New(fmt.Sprintf("can't marshal YAML %v", combinedResultMap))
}
return string(yaml), nil
case "md":
return markdown(&combinedResultMap)
}
return "", errors.New(fmt.Sprintf("unsupported output format %s", output))
}