github.com/jenkins-x/jx-api@v0.0.24/pkg/util/urls.go (about)

     1  package util
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  )
     7  
     8  // SanitizeURL sanitizes by stripping the user and password
     9  func SanitizeURL(unsanitizedURL string) string {
    10  	u, err := url.Parse(unsanitizedURL)
    11  	if err != nil {
    12  		return unsanitizedURL
    13  	}
    14  	return stripCredentialsFromURL(u)
    15  }
    16  
    17  // stripCredentialsFromURL strip credentials from URL
    18  func stripCredentialsFromURL(u *url.URL) string {
    19  	pass, hasPassword := u.User.Password()
    20  	userName := u.User.Username()
    21  	if hasPassword {
    22  		textToReplace := pass + "@"
    23  		textToReplace = ":" + textToReplace
    24  		if userName != "" {
    25  			textToReplace = userName + textToReplace
    26  		}
    27  		return strings.Replace(u.String(), textToReplace, "", 1)
    28  	}
    29  	return u.String()
    30  }