github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/discovery/generator.go (about)

     1  package discovery // import "github.com/demonoid81/moby/pkg/discovery"
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  )
     8  
     9  // Generate takes care of IP generation
    10  func Generate(pattern string) []string {
    11  	re, _ := regexp.Compile(`\[(.+):(.+)\]`)
    12  	submatch := re.FindStringSubmatch(pattern)
    13  	if submatch == nil {
    14  		return []string{pattern}
    15  	}
    16  
    17  	from, err := strconv.Atoi(submatch[1])
    18  	if err != nil {
    19  		return []string{pattern}
    20  	}
    21  	to, err := strconv.Atoi(submatch[2])
    22  	if err != nil {
    23  		return []string{pattern}
    24  	}
    25  
    26  	template := re.ReplaceAllString(pattern, "%d")
    27  
    28  	var result []string
    29  	for val := from; val <= to; val++ {
    30  		entry := fmt.Sprintf(template, val)
    31  		result = append(result, entry)
    32  	}
    33  
    34  	return result
    35  }