...

Source file src/github.com/bazelbuild/buildtools/buildifier/config/validation.go

Documentation: github.com/bazelbuild/buildtools/buildifier/config

     1  /*
     2  Copyright 2020 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  )
    23  
    24  // ValidateInputType validates the value of --type
    25  func ValidateInputType(inputType *string) error {
    26  	switch *inputType {
    27  	case "build", "bzl", "workspace", "default", "module", "auto":
    28  		return nil
    29  
    30  	default:
    31  		return fmt.Errorf("unrecognized input type %s; valid types are build, bzl, workspace, default, module, auto", *inputType)
    32  	}
    33  }
    34  
    35  // ValidateFormat validates the value of --format
    36  func ValidateFormat(format, mode *string) error {
    37  	switch *format {
    38  	case "":
    39  		return nil
    40  
    41  	case "text", "json":
    42  		if *mode != "check" {
    43  			return fmt.Errorf("cannot specify --format without --mode=check")
    44  		}
    45  
    46  	default:
    47  		return fmt.Errorf("unrecognized format %s; valid types are text, json", *format)
    48  	}
    49  	return nil
    50  }
    51  
    52  // isRecognizedMode checks whether the given mode is one of the valid modes.
    53  func isRecognizedMode(validModes []string, mode string) bool {
    54  	for _, m := range validModes {
    55  		if mode == m {
    56  			return true
    57  		}
    58  	}
    59  	return false
    60  }
    61  
    62  // ValidateModes validates flags --mode, --lint, and -d
    63  func ValidateModes(mode, lint *string, dflag *bool, additionalModes ...string) error {
    64  	if *dflag {
    65  		if *mode != "" {
    66  			return fmt.Errorf("cannot specify both -d and -mode flags")
    67  		}
    68  		*mode = "diff"
    69  	}
    70  
    71  	// Check mode.
    72  	validModes := []string{"check", "diff", "fix", "print_if_changed"}
    73  	validModes = append(validModes, additionalModes...)
    74  
    75  	if *mode == "" {
    76  		*mode = "fix"
    77  	} else if !isRecognizedMode(validModes, *mode) {
    78  		return fmt.Errorf("unrecognized mode %s; valid modes are %s", *mode, strings.Join(validModes, ", "))
    79  	}
    80  
    81  	// Check lint mode.
    82  	switch *lint {
    83  	case "":
    84  		*lint = "off"
    85  
    86  	case "off", "warn":
    87  		// ok
    88  
    89  	case "fix":
    90  		if *mode != "fix" {
    91  			return fmt.Errorf("--lint=fix is only compatible with --mode=fix")
    92  		}
    93  
    94  	default:
    95  		return fmt.Errorf("unrecognized lint mode %s; valid modes are warn and fix", *lint)
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  // ValidateWarnings validates the value of the --warnings flag
   102  func ValidateWarnings(warnings *string, allWarnings, defaultWarnings *[]string) ([]string, error) {
   103  
   104  	// Check lint warnings
   105  	var warningsList []string
   106  	switch *warnings {
   107  	case "", "default":
   108  		warningsList = *defaultWarnings
   109  	case "all":
   110  		warningsList = *allWarnings
   111  	default:
   112  		// Either all or no warning categories should start with "+" or "-".
   113  		// If all of them start with "+" or "-", the semantics is
   114  		// "default set of warnings + something - something".
   115  		plus := map[string]bool{}
   116  		minus := map[string]bool{}
   117  		for _, warning := range strings.Split(*warnings, ",") {
   118  			if strings.HasPrefix(warning, "+") {
   119  				plus[warning[1:]] = true
   120  			} else if strings.HasPrefix(warning, "-") {
   121  				minus[warning[1:]] = true
   122  			} else {
   123  				warningsList = append(warningsList, warning)
   124  			}
   125  		}
   126  		if len(warningsList) > 0 && (len(plus) > 0 || len(minus) > 0) {
   127  			return []string{}, fmt.Errorf("warning categories with modifiers (\"+\" or \"-\") can't be mixed with raw warning categories")
   128  		}
   129  		if len(warningsList) == 0 {
   130  			for _, warning := range *defaultWarnings {
   131  				if !minus[warning] {
   132  					warningsList = append(warningsList, warning)
   133  				}
   134  			}
   135  			for warning := range plus {
   136  				warningsList = append(warningsList, warning)
   137  			}
   138  		}
   139  	}
   140  	return warningsList, nil
   141  }
   142  

View as plain text