mirror of
https://github.com/argoproj/argo-cd
synced 2026-04-21 17:07:16 +00:00
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package git
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// NormalizeGitURL normalizes a git URL for lookup and storage
|
|
func NormalizeGitURL(repo string) string {
|
|
// TODO: implement this
|
|
repo = strings.TrimSpace(repo)
|
|
return repo
|
|
}
|
|
|
|
// TestRepo tests if a repo exists and is accessible with the given credentials
|
|
func TestRepo(repo, username, password string) error {
|
|
repoURL, err := url.ParseRequestURI(repo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
repoURL.User = url.UserPassword(username, password)
|
|
cmd := exec.Command("git", "ls-remote", repoURL.String(), "HEAD")
|
|
env := os.Environ()
|
|
env = append(env, "GIT_ASKPASS=")
|
|
cmd.Env = env
|
|
_, err = cmd.Output()
|
|
if err != nil {
|
|
exErr := err.(*exec.ExitError)
|
|
errOutput := strings.Split(string(exErr.Stderr), "\n")[0]
|
|
errOutput = redactPassword(errOutput, password)
|
|
return fmt.Errorf("failed to test %s: %s", repo, errOutput)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func redactPassword(msg string, password string) string {
|
|
if password != "" {
|
|
passwordRegexp := regexp.MustCompile("\\b" + regexp.QuoteMeta(password) + "\\b")
|
|
msg = passwordRegexp.ReplaceAllString(msg, "*****")
|
|
}
|
|
return msg
|
|
}
|