identifier.go raw

   1  package api
   2  
   3  import (
   4  	"cmp"
   5  	"maps"
   6  	"net"
   7  	"slices"
   8  
   9  	"github.com/go-acme/lego/v4/acme"
  10  )
  11  
  12  func createIdentifiers(domains []string) []acme.Identifier {
  13  	uniqIdentifiers := make(map[string]acme.Identifier)
  14  
  15  	for _, domain := range domains {
  16  		if _, ok := uniqIdentifiers[domain]; ok {
  17  			continue
  18  		}
  19  
  20  		ident := acme.Identifier{Value: domain, Type: "dns"}
  21  
  22  		if net.ParseIP(domain) != nil {
  23  			ident.Type = "ip"
  24  		}
  25  
  26  		uniqIdentifiers[domain] = ident
  27  	}
  28  
  29  	return slices.AppendSeq(make([]acme.Identifier, 0, len(uniqIdentifiers)), maps.Values(uniqIdentifiers))
  30  }
  31  
  32  // compareIdentifiers compares 2 slices of [acme.Identifier].
  33  func compareIdentifiers(a, b []acme.Identifier) int {
  34  	// Clones slices to avoid modifying original slices.
  35  	right := slices.Clone(a)
  36  	left := slices.Clone(b)
  37  
  38  	slices.SortStableFunc(right, compareIdentifier)
  39  	slices.SortStableFunc(left, compareIdentifier)
  40  
  41  	return slices.CompareFunc(right, left, compareIdentifier)
  42  }
  43  
  44  func compareIdentifier(right, left acme.Identifier) int {
  45  	return cmp.Or(
  46  		cmp.Compare(right.Type, left.Type),
  47  		cmp.Compare(right.Value, left.Value),
  48  	)
  49  }
  50