github.com/netdata/go.d.plugin@v0.58.1/agent/discovery/sd/pipeline/funcmap.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package pipeline
     4  
     5  import (
     6  	"regexp"
     7  	"text/template"
     8  
     9  	"github.com/Masterminds/sprig/v3"
    10  	"github.com/bmatcuk/doublestar/v4"
    11  )
    12  
    13  func newFuncMap() template.FuncMap {
    14  	custom := map[string]interface{}{
    15  		"glob": globAny,
    16  		"re":   regexpAny,
    17  	}
    18  
    19  	fm := sprig.HermeticTxtFuncMap()
    20  	for name, fn := range custom {
    21  		fm[name] = fn
    22  	}
    23  
    24  	return fm
    25  }
    26  
    27  func globAny(value, pattern string, rest ...string) bool {
    28  	switch len(rest) {
    29  	case 0:
    30  		return globOnce(value, pattern)
    31  	default:
    32  		return globOnce(value, pattern) || globAny(value, rest[0], rest[1:]...)
    33  	}
    34  }
    35  
    36  func regexpAny(value, pattern string, rest ...string) bool {
    37  	switch len(rest) {
    38  	case 0:
    39  		return regexpOnce(value, pattern)
    40  	default:
    41  		return regexpOnce(value, pattern) || regexpAny(value, rest[0], rest[1:]...)
    42  	}
    43  }
    44  
    45  func globOnce(value, pattern string) bool {
    46  	ok, err := doublestar.Match(pattern, value)
    47  	return err == nil && ok
    48  }
    49  
    50  func regexpOnce(value, pattern string) bool {
    51  	ok, err := regexp.MatchString(pattern, value)
    52  	return err == nil && ok
    53  }