Skip to content

Commit 2aa093c

Browse files
committed
Go: Move getImportPath to shared util package
1 parent 92e91f5 commit 2aa093c

File tree

3 files changed

+61
-60
lines changed

3 files changed

+61
-60
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55
"log"
6-
"net/url"
76
"os"
87
"os/exec"
98
"path/filepath"
@@ -56,63 +55,6 @@ Build behavior:
5655
fmt.Fprintf(os.Stderr, "Usage:\n\n %s\n", os.Args[0])
5756
}
5857

59-
// Returns the import path of the package being built, or "" if it cannot be determined.
60-
func getImportPath() (importpath string) {
61-
importpath = os.Getenv("LGTM_INDEX_IMPORT_PATH")
62-
if importpath == "" {
63-
repourl := os.Getenv("SEMMLE_REPO_URL")
64-
if repourl == "" {
65-
githubrepo := os.Getenv("GITHUB_REPOSITORY")
66-
if githubrepo == "" {
67-
log.Printf("Unable to determine import path, as neither LGTM_INDEX_IMPORT_PATH nor GITHUB_REPOSITORY is set\n")
68-
return ""
69-
} else {
70-
importpath = "github.com/" + githubrepo
71-
}
72-
} else {
73-
importpath = getImportPathFromRepoURL(repourl)
74-
if importpath == "" {
75-
log.Printf("Failed to determine import path from SEMMLE_REPO_URL '%s'\n", repourl)
76-
return
77-
}
78-
}
79-
}
80-
log.Printf("Import path is '%s'\n", importpath)
81-
return
82-
}
83-
84-
// Returns the import path of the package being built from `repourl`, or "" if it cannot be
85-
// determined.
86-
func getImportPathFromRepoURL(repourl string) string {
87-
// check for scp-like URL as in "[email protected]:github/codeql-go.git"
88-
shorturl := regexp.MustCompile(`^([^@]+@)?([^:]+):([^/].*?)(\.git)?$`)
89-
m := shorturl.FindStringSubmatch(repourl)
90-
if m != nil {
91-
return m[2] + "/" + m[3]
92-
}
93-
94-
// otherwise parse as proper URL
95-
u, err := url.Parse(repourl)
96-
if err != nil {
97-
log.Fatalf("Malformed repository URL '%s'\n", repourl)
98-
}
99-
100-
if u.Scheme == "file" {
101-
// we can't determine import paths from file paths
102-
return ""
103-
}
104-
105-
if u.Hostname() == "" || u.Path == "" {
106-
return ""
107-
}
108-
109-
host := u.Hostname()
110-
path := u.Path
111-
// strip off leading slashes and trailing `.git` if present
112-
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
113-
return host + "/" + path
114-
}
115-
11658
func restoreRepoLayout(fromDir string, dirEntries []string, scratchDirName string, toDir string) {
11759
for _, dirEntry := range dirEntries {
11860
if dirEntry != scratchDirName {
@@ -568,7 +510,7 @@ func installDependenciesAndBuild() {
568510
if len(workspaces) == 1 {
569511
workspace := workspaces[0]
570512

571-
importpath := getImportPath()
513+
importpath := util.GetImportPath()
572514
needGopath := getNeedGopath(workspace, importpath)
573515

574516
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""

go/extractor/util/util.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"io"
77
"io/fs"
88
"log"
9+
"net/url"
910
"os"
1011
"os/exec"
1112
"path/filepath"
13+
"regexp"
1214
"runtime"
1315
"slices"
1416
"strings"
@@ -350,3 +352,60 @@ func GetParentDirs(paths []string) []string {
350352
}
351353
return dirs
352354
}
355+
356+
// Returns the import path of the package being built, or "" if it cannot be determined.
357+
func GetImportPath() (importpath string) {
358+
importpath = os.Getenv("LGTM_INDEX_IMPORT_PATH")
359+
if importpath == "" {
360+
repourl := os.Getenv("SEMMLE_REPO_URL")
361+
if repourl == "" {
362+
githubrepo := os.Getenv("GITHUB_REPOSITORY")
363+
if githubrepo == "" {
364+
log.Printf("Unable to determine import path, as neither LGTM_INDEX_IMPORT_PATH nor GITHUB_REPOSITORY is set\n")
365+
return ""
366+
} else {
367+
importpath = "github.com/" + githubrepo
368+
}
369+
} else {
370+
importpath = getImportPathFromRepoURL(repourl)
371+
if importpath == "" {
372+
log.Printf("Failed to determine import path from SEMMLE_REPO_URL '%s'\n", repourl)
373+
return
374+
}
375+
}
376+
}
377+
log.Printf("Import path is '%s'\n", importpath)
378+
return
379+
}
380+
381+
// Returns the import path of the package being built from `repourl`, or "" if it cannot be
382+
// determined.
383+
func getImportPathFromRepoURL(repourl string) string {
384+
// check for scp-like URL as in "[email protected]:github/codeql-go.git"
385+
shorturl := regexp.MustCompile(`^([^@]+@)?([^:]+):([^/].*?)(\.git)?$`)
386+
m := shorturl.FindStringSubmatch(repourl)
387+
if m != nil {
388+
return m[2] + "/" + m[3]
389+
}
390+
391+
// otherwise parse as proper URL
392+
u, err := url.Parse(repourl)
393+
if err != nil {
394+
log.Fatalf("Malformed repository URL '%s'\n", repourl)
395+
}
396+
397+
if u.Scheme == "file" {
398+
// we can't determine import paths from file paths
399+
return ""
400+
}
401+
402+
if u.Hostname() == "" || u.Path == "" {
403+
return ""
404+
}
405+
406+
host := u.Hostname()
407+
path := u.Path
408+
// strip off leading slashes and trailing `.git` if present
409+
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
410+
return host + "/" + path
411+
}

go/extractor/cli/go-autobuilder/go-autobuilder_test.go renamed to go/extractor/util/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package util
22

33
import "testing"
44

0 commit comments

Comments
 (0)