github.com/creativeprojects/go-selfupdate@v1.2.0/repository_slug.go (about)

     1  package selfupdate
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  type RepositorySlug struct {
     8  	owner string
     9  	repo  string
    10  }
    11  
    12  // Repository interface
    13  var _ Repository = RepositorySlug{}
    14  
    15  // ParseSlug is used to take a string "owner/repo" to make a RepositorySlug
    16  func ParseSlug(slug string) RepositorySlug {
    17  	var owner, repo string
    18  	couple := strings.Split(slug, "/")
    19  	if len(couple) != 2 {
    20  		// give it another try
    21  		couple = strings.Split(slug, "%2F")
    22  	}
    23  	if len(couple) == 2 {
    24  		owner = couple[0]
    25  		repo = couple[1]
    26  	}
    27  	return RepositorySlug{
    28  		owner: owner,
    29  		repo:  repo,
    30  	}
    31  }
    32  
    33  // NewRepositorySlug creates a RepositorySlug from owner and repo parameters
    34  func NewRepositorySlug(owner, repo string) RepositorySlug {
    35  	return RepositorySlug{
    36  		owner: owner,
    37  		repo:  repo,
    38  	}
    39  }
    40  
    41  func (r RepositorySlug) GetSlug() (string, string, error) {
    42  	if r.owner == "" && r.repo == "" {
    43  		return "", "", ErrInvalidSlug
    44  	}
    45  	if r.owner == "" {
    46  		return r.owner, r.repo, ErrIncorrectParameterOwner
    47  	}
    48  	if r.repo == "" {
    49  		return r.owner, r.repo, ErrIncorrectParameterRepo
    50  	}
    51  	return r.owner, r.repo, nil
    52  }
    53  
    54  func (r RepositorySlug) Get() (interface{}, error) {
    55  	_, _, err := r.GetSlug()
    56  	if err != nil {
    57  		return "", err
    58  	}
    59  	return r.owner + "/" + r.repo, nil
    60  }