github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/buildifier/utils/flags.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // ValidateInputType validates the value of --type 9 func ValidateInputType(inputType *string) error { 10 switch *inputType { 11 case "build", "bzl", "workspace", "default", "auto": 12 return nil 13 14 default: 15 return fmt.Errorf("unrecognized input type %s; valid types are build, bzl, workspace, default, auto", *inputType) 16 } 17 } 18 19 // ValidateFormat validates the value of --format 20 func ValidateFormat(format, mode *string) error { 21 switch *format { 22 case "": 23 return nil 24 25 case "text", "json": 26 if *mode != "check" { 27 return fmt.Errorf("cannot specify --format without --type=check") 28 } 29 30 default: 31 return fmt.Errorf("unrecognized format %s; valid types are text, json", *format) 32 } 33 return nil 34 } 35 36 // isRecognizedMode checks whether the given mode is one of the valid modes. 37 func isRecognizedMode(validModes []string, mode string) bool { 38 for _, m := range validModes { 39 if mode == m { 40 return true 41 } 42 } 43 return false 44 } 45 46 // ValidateModes validates flags --mode, --lint, and -d 47 func ValidateModes(mode, lint *string, dflag *bool, additionalModes ...string) error { 48 if *dflag { 49 if *mode != "" { 50 return fmt.Errorf("cannot specify both -d and -mode flags") 51 } 52 *mode = "diff" 53 } 54 55 // Check mode. 56 validModes := []string{"check", "diff", "fix", "print_if_changed"} 57 validModes = append(validModes, additionalModes...) 58 59 if *mode == "" { 60 *mode = "fix" 61 } else if !isRecognizedMode(validModes, *mode) { 62 return fmt.Errorf("unrecognized mode %s; valid modes are %s", *mode, strings.Join(validModes, ", ")) 63 } 64 65 // Check lint mode. 66 switch *lint { 67 case "": 68 *lint = "off" 69 70 case "off", "warn": 71 // ok 72 73 case "fix": 74 if *mode != "fix" { 75 return fmt.Errorf("--lint=fix is only compatible with --mode=fix") 76 } 77 78 default: 79 return fmt.Errorf("unrecognized lint mode %s; valid modes are warn and fix", *lint) 80 } 81 82 return nil 83 } 84 85 // ValidateWarnings validates the value of the --warnings flag 86 func ValidateWarnings(warnings *string, allWarnings, defaultWarnings *[]string) ([]string, error) { 87 88 // Check lint warnings 89 var warningsList []string 90 switch *warnings { 91 case "", "default": 92 warningsList = *defaultWarnings 93 case "all": 94 warningsList = *allWarnings 95 default: 96 // Either all or no warning categories should start with "+" or "-". 97 // If all of them start with "+" or "-", the semantics is 98 // "default set of warnings + something - something". 99 plus := map[string]bool{} 100 minus := map[string]bool{} 101 for _, warning := range strings.Split(*warnings, ",") { 102 if strings.HasPrefix(warning, "+") { 103 plus[warning[1:]] = true 104 } else if strings.HasPrefix(warning, "-") { 105 minus[warning[1:]] = true 106 } else { 107 warningsList = append(warningsList, warning) 108 } 109 } 110 if len(warningsList) > 0 && (len(plus) > 0 || len(minus) > 0) { 111 return []string{}, fmt.Errorf("warning categories with modifiers (\"+\" or \"-\") can't me mixed with raw warning categories") 112 } 113 if len(warningsList) == 0 { 114 for _, warning := range *defaultWarnings { 115 if !minus[warning] { 116 warningsList = append(warningsList, warning) 117 } 118 } 119 for warning := range plus { 120 warningsList = append(warningsList, warning) 121 } 122 } 123 } 124 return warningsList, nil 125 }