gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/golang.org/x/tools/go/buildutil/tags.go (about)

     1  package buildutil
     2  
     3  // This logic was copied from stringsFlag from $GOROOT/src/cmd/go/build.go.
     4  
     5  import "fmt"
     6  
     7  const TagsFlagDoc = "a list of `build tags` to consider satisfied during the build. " +
     8  	"For more information about build tags, see the description of " +
     9  	"build constraints in the documentation for the go/build package"
    10  
    11  // TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses
    12  // a flag value in the same manner as go build's -tags flag and
    13  // populates a []string slice.
    14  //
    15  // See $GOROOT/src/go/build/doc.go for description of build tags.
    16  // See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag.
    17  //
    18  // Example:
    19  // 	flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
    20  type TagsFlag []string
    21  
    22  func (v *TagsFlag) Set(s string) error {
    23  	var err error
    24  	*v, err = splitQuotedFields(s)
    25  	if *v == nil {
    26  		*v = []string{}
    27  	}
    28  	return err
    29  }
    30  
    31  func (v *TagsFlag) Get() interface{} { return *v }
    32  
    33  func splitQuotedFields(s string) ([]string, error) {
    34  	// Split fields allowing '' or "" around elements.
    35  	// Quotes further inside the string do not count.
    36  	var f []string
    37  	for len(s) > 0 {
    38  		for len(s) > 0 && isSpaceByte(s[0]) {
    39  			s = s[1:]
    40  		}
    41  		if len(s) == 0 {
    42  			break
    43  		}
    44  		// Accepted quoted string. No unescaping inside.
    45  		if s[0] == '"' || s[0] == '\'' {
    46  			quote := s[0]
    47  			s = s[1:]
    48  			i := 0
    49  			for i < len(s) && s[i] != quote {
    50  				i++
    51  			}
    52  			if i >= len(s) {
    53  				return nil, fmt.Errorf("unterminated %c string", quote)
    54  			}
    55  			f = append(f, s[:i])
    56  			s = s[i+1:]
    57  			continue
    58  		}
    59  		i := 0
    60  		for i < len(s) && !isSpaceByte(s[i]) {
    61  			i++
    62  		}
    63  		f = append(f, s[:i])
    64  		s = s[i:]
    65  	}
    66  	return f, nil
    67  }
    68  
    69  func (v *TagsFlag) String() string {
    70  	return "<tagsFlag>"
    71  }
    72  
    73  func isSpaceByte(c byte) bool {
    74  	return c == ' ' || c == '\t' || c == '\n' || c == '\r'
    75  }