bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/util/util.go (about)

     1  // Package util defines utilities for bosun.
     2  package util // import "bosun.org/util"
     3  
     4  import (
     5  	"bosun.org/host"
     6  	"bosun.org/slog"
     7  
     8  	"regexp"
     9  )
    10  
    11  // This is here only until we manage to refactor more of the system, allowing us to pass a host.Manager around
    12  // the system, rather than holding onto global state
    13  var hostManager host.Manager
    14  
    15  func InitHostManager(customHostname string, useFullHostName bool) {
    16  	var hm host.Manager
    17  	var err error
    18  
    19  	if customHostname != "" {
    20  		hm, err = host.NewManagerForHostname(customHostname, useFullHostName)
    21  	} else {
    22  		hm, err = host.NewManager(useFullHostName)
    23  	}
    24  
    25  	if err != nil {
    26  		slog.Fatalf("couldn't initialise host factory: %v", err)
    27  	}
    28  
    29  	SetHostManager(hm)
    30  }
    31  
    32  func SetHostManager(hm host.Manager) {
    33  	hostManager = hm
    34  }
    35  
    36  func GetHostManager() host.Manager {
    37  	return hostManager
    38  }
    39  
    40  func NameMatches(name string, regexes []*regexp.Regexp) bool {
    41  	for _, r := range regexes {
    42  		if r.MatchString(name) {
    43  			return true
    44  		}
    45  	}
    46  	return false
    47  }
    48  
    49  func Btoi(b bool) int {
    50  	if b {
    51  		return 1
    52  	}
    53  	return 0
    54  }