Skip to content

Commit e3e7900

Browse files
makoscafeerbren
andauthored
Add checks flag to fix specific checks (#797)
* add checks to fix and fix-all-checks flags * only use one flag * add example Co-authored-by: Robert Brennan <[email protected]>
1 parent 50319fb commit e3e7900

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

cmd/polaris/fix.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ import (
3333
"sigs.k8s.io/yaml"
3434
)
3535

36-
var filesPath string
36+
var (
37+
filesPath string
38+
checksToFix []string
39+
fixAll bool
40+
)
3741

3842
func init() {
3943
rootCmd.AddCommand(fixCommand)
4044
fixCommand.PersistentFlags().StringVar(&filesPath, "files-path", "", "mutate and fix one or more YAML files in a specified folder")
45+
fixCommand.PersistentFlags().StringSliceVar(&checksToFix, "checks", []string{}, "Optional flag to specify specific checks to fix eg. checks=hostIPCSet,hostPIDSet and checks=all applies fix to all defined checks mutations")
4146
}
4247

4348
var fixCommand = &cobra.Command{
@@ -73,6 +78,19 @@ var fixCommand = &cobra.Command{
7378
}
7479
var contentStr string
7580
isFirstResource := true
81+
82+
if len(checksToFix) > 0 {
83+
if len(checksToFix) == 1 && checksToFix[0] == "all" {
84+
allchecks := []string{}
85+
for key := range config.Checks {
86+
allchecks = append(allchecks, key)
87+
}
88+
config.Mutations = allchecks
89+
} else {
90+
config.Mutations = checksToFix
91+
}
92+
}
93+
7694
for _, fullFilePath := range yamlFiles {
7795

7896
yamlFile, err := ioutil.ReadFile(fullFilePath)

pkg/validator/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func applySchemaCheck(conf *config.Configuration, checkID string, test schemaTes
325325
}
326326
result := makeResult(conf, check, passes, issues)
327327
if !passes {
328-
if funk.Contains(conf.Mutations, checkID) {
328+
if funk.Contains(conf.Mutations, checkID) && len(check.Mutations) > 0 {
329329
mutations := funk.Map(check.Mutations, func(mutation jsonpatch.Operation) jsonpatch.Operation {
330330
mutationCopy := deepCopyMutation(mutation)
331331
mutationCopy.Path = prefix + mutationCopy.Path

0 commit comments

Comments
 (0)