Quellcode durchsuchen

Generate the local repoName

To support other URLs than standard github URLs, the repoName
is now being generated based on the given URL.

Currently supported are formats for HTTP, SSH and local paths.

Fixes #4
Matthias Derer vor 8 Jahren
Ursprung
Commit
9c1ea12d77
2 geänderte Dateien mit 66 neuen und 1 gelöschten Zeilen
  1. 12 1
      leaks.go
  2. 54 0
      leaks_test.go

+ 12 - 1
leaks.go

@@ -27,7 +27,7 @@ func start(opts *Options, repoUrl string) {
 	if err != nil {
 		log.Fatalf("failed to clone repo %v", err)
 	}
-	repoName := strings.Split(repoUrl, "/")[4]
+	repoName := getLocalRepoName(repoUrl)
 	if err := os.Chdir(repoName); err != nil {
 		log.Fatal(err)
 	}
@@ -43,6 +43,17 @@ func start(opts *Options, repoUrl string) {
 	err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJson, 0644)
 }
 
+// getLocalRepoName generates the name of the local clone folder based on the given URL
+func getLocalRepoName(url string) string {
+	splitSlashes := strings.Split(url, "/")
+	name := splitSlashes[len(splitSlashes)-1]
+	name = strings.TrimSuffix(name, ".git")
+	splitColons := strings.Split(name, ":")
+	name = splitColons[len(splitColons)-1]
+
+	return name
+}
+
 func cleanup(repoName string) {
 	if err := os.Chdir(appRoot); err != nil {
 		log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)

+ 54 - 0
leaks_test.go

@@ -0,0 +1,54 @@
+package main
+
+import "testing"
+
+func TestGetLocalRepoName(t *testing.T) {
+	cases := []struct{
+		name string
+		input string
+		expected string
+	}{
+		{
+			"Usual github url",
+			"https://github.com/usual/url",
+			"url",
+		},
+		{
+			"Usual github url with .git suffix",
+			"https://github.com/usual/url.git",
+			"url",
+		},
+		{
+			"personal git url",
+			"git@github.com:url.git",
+			"url",
+		},
+		{
+			"personal git url in sub folder",
+			"git@github.com:sub/url.git",
+			"url",
+		},
+		{
+			"ssh git url with port",
+			"ssh://git@github.com:2222/sub/url.git",
+			"url",
+		},
+		{
+			"local git in sub folder",
+			"local/url.git",
+			"url",
+		},
+		{
+			"local git in same folder",
+			"url.git",
+			"url",
+		},
+	}
+
+	for _, c := range cases {
+		actual := getLocalRepoName(c.input)
+		if actual != c.expected {
+			t.Errorf("'%s' failed. Input: '%s'; Expected: '%s'; Got: '%s'", c.input, c.name, c.expected, actual)
+		}
+	}
+}