github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/pkg/model/api.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  )
     7  
     8  // If there are multiple tilt apiservers running,
     9  // we can refer to them by name.
    10  type APIServerName string
    11  
    12  var apiServerNameMatcher = regexp.MustCompile("^[a-z0-9-]+$")
    13  
    14  // Makes sure the apiserver name is well-formed.
    15  func ValidateAPIServerName(name APIServerName) error {
    16  	if !apiServerNameMatcher.MatchString(string(name)) {
    17  		return fmt.Errorf("malformed name, must match regexp /[a-z0-9-]+/. Actual: %s", name)
    18  	}
    19  	return nil
    20  }
    21  
    22  // Each apiserver has a name based on the web port.
    23  func DefaultAPIServerName(port WebPort) APIServerName {
    24  	if port == DefaultWebPort {
    25  		return "tilt-default"
    26  	}
    27  	return APIServerName(fmt.Sprintf("tilt-%d", port))
    28  }
    29  
    30  // Determines what the API server name should be
    31  // based on the --port flag.
    32  //
    33  // TODO(nick): Long-term, most tools in this space are moving
    34  // away from making users manage ports, and using names
    35  // to identify different instances.
    36  func ProvideAPIServerName(port WebPort) APIServerName {
    37  	return DefaultAPIServerName(port)
    38  }