github.com/weiwenhao/getter@v1.30.1/detect_gitlab.go (about)

     1  package getter
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  )
     8  
     9  // GitLabDetector implements Detector to detect GitLab URLs and turn
    10  // them into URLs that the Git Getter can understand.
    11  type GitLabDetector struct{}
    12  
    13  func (d *GitLabDetector) Detect(src, _ string) (string, bool, error) {
    14  	if len(src) == 0 {
    15  		return "", false, nil
    16  	}
    17  
    18  	if strings.HasPrefix(src, "gitlab.com/") {
    19  		return d.detectHTTP(src)
    20  	}
    21  
    22  	return "", false, nil
    23  }
    24  
    25  func (d *GitLabDetector) detectHTTP(src string) (string, bool, error) {
    26  	parts := strings.Split(src, "/")
    27  	if len(parts) < 3 {
    28  		return "", false, fmt.Errorf(
    29  			"GitLab URLs should be gitlab.com/username/repo")
    30  	}
    31  
    32  	urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
    33  	repoUrl, err := url.Parse(urlStr)
    34  	if err != nil {
    35  		return "", true, fmt.Errorf("error parsing GitLab URL: %s", err)
    36  	}
    37  
    38  	if !strings.HasSuffix(repoUrl.Path, ".git") {
    39  		repoUrl.Path += ".git"
    40  	}
    41  
    42  	if len(parts) > 3 {
    43  		repoUrl.Path += "//" + strings.Join(parts[3:], "/")
    44  	}
    45  
    46  	return "git::" + repoUrl.String(), true, nil
    47  }