github.com/replicatedcom/ship@v0.50.0/pkg/specs/replicatedapp/selector.go (about)

     1  package replicatedapp
     2  
     3  import (
     4  	"net/url"
     5  	"regexp"
     6  	"strings"
     7  
     8  	"github.com/google/go-querystring/query"
     9  )
    10  
    11  // Selector selects a replicated.app spec from the Vendor's releases and channels.
    12  // See pkg/cli/root.go for some more info on which are required and why.
    13  //
    14  // note that `url` struct tags are only for serialize, they don't work for deserialize
    15  type Selector struct {
    16  	// required
    17  	CustomerID     string `url:"customer_id,omitempty"`
    18  	InstallationID string `url:"installation_id,omitempty"`
    19  	// OR
    20  	AppSlug string `url:"AppSlug,omitempty"`
    21  
    22  	// optional
    23  	Upstream      string `url:"upstream,omitempty"`
    24  	ReleaseID     string `url:"release_id,omitempty"` // NOTE: this is unused
    25  	ReleaseSemver string `url:"release_semver,omitempty"`
    26  	LicenseID     string `url:"license_id,omitempty"`
    27  }
    28  
    29  func (s *Selector) String() string {
    30  	sCopy := *s
    31  	sCopy.AppSlug = ""
    32  
    33  	v, err := query.Values(sCopy)
    34  	if err != nil {
    35  		return "Selector{(failed to parse)}"
    36  	}
    37  	return v.Encode()
    38  }
    39  
    40  var pathQuery = regexp.MustCompile(`replicated\.app/([\w_\-/]+[\w_\-]+)`)
    41  
    42  // this is less janky
    43  func (s *Selector) UnmarshalFrom(url *url.URL) *Selector {
    44  	for key, values := range url.Query() {
    45  		if len(values) == 0 {
    46  			continue
    47  		}
    48  		switch key {
    49  		case "customer_id":
    50  			s.CustomerID = values[0]
    51  		case "installation_id":
    52  			s.InstallationID = values[0]
    53  		case "release_id":
    54  			s.ReleaseID = values[0]
    55  		case "release_semver":
    56  			s.ReleaseSemver = values[0]
    57  		case "license_id":
    58  			s.LicenseID = values[0]
    59  		}
    60  	}
    61  
    62  	if pathQuery.MatchString(url.Path) && s.CustomerID == "" {
    63  		matches := pathQuery.FindStringSubmatch(url.Path)
    64  		if len(matches) == 2 {
    65  			s.AppSlug = matches[1]
    66  		}
    67  	}
    68  
    69  	if strings.HasPrefix(url.String(), "staging.replicated.app") {
    70  		s.Upstream = "https://pg.staging.replicated.com/graphql"
    71  	}
    72  	return s
    73  }
    74  
    75  func (s *Selector) GetBasicAuthUsername() string {
    76  	if s.CustomerID != "" {
    77  		return s.CustomerID
    78  	}
    79  	return s.LicenseID
    80  }