This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
validate.go
159 lines (124 loc) · 3.97 KB
/
validate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"io/ioutil"
"os"
"path"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/quay/config-tool/pkg/lib/config"
"github.com/quay/config-tool/pkg/lib/shared"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
// validateCmd represents the validate command
var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate your config bundle",
Run: func(cmd *cobra.Command, args []string) {
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
if os.Getenv("DEBUGLOG") == "true" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.WarnLevel)
}
isValid := true
// Read config file
configFilePath := path.Join(configDir, "config.yaml")
configBytes, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Fatalf(err.Error())
}
// Unmarshal from json
var conf map[string]interface{}
if err = yaml.Unmarshal(configBytes, &conf); err != nil {
log.Fatalf(err.Error())
}
// Clean config
conf = shared.RemoveNullValues(conf)
// Load into struct
configFieldGroups, err := config.NewConfig(conf)
if err != nil {
log.Fatalf("An error occurred during validation. Process could not marshal config.yaml. This is most likely due to an incorrect type. \nMore info: " + err.Error())
}
// Load certs
certs := shared.LoadCerts(configDir)
if err != nil {
log.Fatalf(err.Error())
}
// Sort keys
fgNames := []string{}
for fgName := range configFieldGroups {
fgNames = append(fgNames, fgName)
}
sort.Strings(fgNames)
// Initialize validaiton status grid
validationStatus := [][]string{}
for _, fgName := range fgNames {
// Get field group for key
fieldGroup := configFieldGroups[fgName]
// Set options
opts := shared.Options{
Mode: validationMode,
Certificates: certs,
}
// Validate
log.Debugf("Validating %s", fgName)
validationErrors := fieldGroup.Validate(opts)
// If no errors, append row
if len(validationErrors) == 0 {
validationStatus = append(validationStatus, []string{fgName, "-", "🟢"})
}
// Append error messages
for _, err := range validationErrors {
log.Debugf(err.FieldGroup, err.Message, err.Tags)
// Append field group policy violation
validationStatus = append(validationStatus, []string{fgName, err.Message, "🔴"})
isValid = false
}
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Field Group", "Error", "Status"})
table.SetBorder(true)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.AppendBulk(validationStatus)
table.SetColWidth(1000)
table.SetRowLine(true)
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.Render()
if !isValid {
os.Exit(1)
}
},
}
func init() {
// Add validation command
rootCmd.AddCommand(validateCmd)
// Add --config flag
validateCmd.Flags().StringVarP(&configDir, "configDir", "c", "", "A directory containing your config files")
validateCmd.MarkFlagRequired("configDir")
validateCmd.Flags().StringVarP(&validationMode, "mode", "m", "", "The mode to validate the config. Must be either online, offline, or testing (for development only)")
validateCmd.MarkFlagRequired("mode")
}
// removeFieldGroup removes the <fgName>FieldGroup. prefix to a tag name
func removeFieldGroupPrefix(input string) string {
if i := strings.Index(input, "."); i != -1 {
return input[i+1:]
}
return input
}