2 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
下面是使用 net、net/url 和字符串包的簡(jiǎn)單方法。
package main
import (
"fmt"
"net"
"net/url"
"strings"
)
func isGitHubURL(input string) bool {
u, err := url.Parse(input)
if err != nil {
return false
}
host := u.Host
if strings.Contains(host, ":") {
host, _, err = net.SplitHostPort(host)
if err != nil {
return false
}
}
return host == "github.com"
}
func main() {
urls := []string{
"https://github.com/foo/bar",
"http://github.com/bar/foo",
"http://github.com.evil.com",
"http://github.com:8080/nonstandard/port",
"http://other.com",
"not a valid URL",
}
for _, url := range urls {
fmt.Printf("URL: \"%s\", is GitHub URL: %v\n", url, isGitHubURL(url))
}
}
輸出:
URL: "https://github.com/foo/bar", is GitHub URL: true
URL: "http://github.com/bar/foo", is GitHub URL: true
URL: "http://github.com.evil.com", is GitHub URL: false
URL: "http://github.com:8080/nonstandard/port", is GitHub URL: true
URL: "http://other.com", is GitHub URL: false
URL: "not a valid URL", is GitHub URL: false

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用專用的 git url 解析器,如下所示:
package utils
import (
"os/exec"
giturl "github.com/armosec/go-git-url"
)
func isGitURL(repo string) bool {
_, err := giturl.NewGitURL(repo) // parse URL, returns error if none git url
return err == nil
}
//CloneRepo clones a repo lol
func CloneRepo(args []string) {
//repo URL
repo := args[0]
//verify that is an actual github repo URL
if !isGitURL(repo) {
// return
}
//Clones Repo
exec.Command("git", "clone", repo).Run()
}
這將為您提供優(yōu)勢(shì),不僅可以驗(yàn)證它是否是git存儲(chǔ)庫(kù),而且您可以運(yùn)行更多驗(yàn)證,以便作為所有者(),存儲(chǔ)庫(kù)()等。GetOwner()GetRepo()
- 2 回答
- 0 關(guān)注
- 110 瀏覽
添加回答
舉報(bào)