第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

驗(yàn)證我的存儲(chǔ)庫(kù)實(shí)際上是Go中的github存儲(chǔ)庫(kù)URL

驗(yàn)證我的存儲(chǔ)庫(kù)實(shí)際上是Go中的github存儲(chǔ)庫(kù)URL

Go
猛跑小豬 2022-08-15 10:52:46
Go 中是否有一種方法可以驗(yàn)證存儲(chǔ)庫(kù)類型字符串實(shí)際上是實(shí)際的 Github 存儲(chǔ)庫(kù) URL?我正在運(yùn)行克隆存儲(chǔ)庫(kù)的代碼,但在我運(yùn)行exec之前。Command(“git”,“clone”,repo)和我想確保repo是有效的。    package utils    import (        "os/exec"    )    //CloneRepo clones a repo lol    func CloneRepo(args []string) {        //repo URL        repo := args[0]        //verify that is an actual github repo URL        //Clones Repo        exec.Command("git", "clone", repo).Run()    }
查看完整描述

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


查看完整回答
反對(duì) 回復(fù) 2022-08-15
?
BIG陽(yáng)

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()


查看完整回答
反對(duì) 回復(fù) 2022-08-15
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)