github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/deployment/internal/repodiscovery/remote.go (about)

     1  package repodiscovery
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  	"strings"
     7  )
     8  
     9  // ExtractRepoURL from remote
    10  func ExtractRepoURL(remoteURL string) (repoURL string, err error) {
    11  	if len(remoteURL) == 0 {
    12  		return "", errors.New("empty remote")
    13  	}
    14  
    15  	if remoteURL[0] == '/' {
    16  		return "", errors.New("origin is on the same machine")
    17  	}
    18  
    19  	remoteURL = strings.TrimSuffix(remoteURL, ".git")
    20  
    21  	if strings.HasPrefix(remoteURL, "git+") && strings.Contains(remoteURL, "://") {
    22  		remoteURL = strings.TrimPrefix(remoteURL, "git+")
    23  	}
    24  
    25  	if !strings.Contains(remoteURL, "://") {
    26  		remoteURL = strings.Replace(remoteURL, ":", "/", 1)
    27  		remoteURL = "https://" + remoteURL
    28  	}
    29  
    30  	remoteURL = fixGitOrSSHRepoURL(remoteURL)
    31  
    32  	u, err := url.Parse(remoteURL)
    33  
    34  	if err != nil {
    35  		return "", err
    36  	}
    37  
    38  	u.User = nil
    39  
    40  	return u.String(), nil
    41  }
    42  
    43  func fixGitOrSSHRepoURL(remoteURL string) string {
    44  	var prefixes = []string{"git+https://", "git://", "ssh://", "git+ssh://"}
    45  
    46  	for _, p := range prefixes {
    47  		if !strings.HasPrefix(remoteURL, p) {
    48  			continue
    49  		}
    50  
    51  		remoteURL = strings.TrimPrefix(remoteURL, p)
    52  		remoteURL = "https://" + strings.Replace(remoteURL, ":", "/", 1)
    53  		break
    54  	}
    55  
    56  	return remoteURL
    57  }