使用`os.exec(“git”, “clone”)`克隆一个仓库。

huangapple go评论86阅读模式
英文:

cloning a repo with os.exec("git", "clone")

问题

运行下面的代码,我期望在访问clone路径时,将github托管的项目username/mysuperrepo克隆到此go项目所在的仓库中,但它不起作用。停止应用程序后,没有mysuperrepo目录,也没有我从命令行运行git clone https://github.com/username/mysuperrepo.git时所期望的任何文件。

问题:为什么下面的代码不会在go程序所在的目录中产生一个克隆的仓库?

func clone(w http.ResponseWriter, r *http.Request){
  var repo = "https://github.com/username/mysuperrepo.git"
  exec.Command("git", "clone", repo)
  w.Write([]byte(repo))
}

func main(){
  http.HandleFunc("/clone/", clone)
  log.Fatal(http.ListenAndServe(":8080", nil))
}
英文:

Running the code below, I expected the github hosted project username/mysuperrepo to be cloned (once I visit the clone path) into the repo where this go project is running, but it doesn't work. After stopping the application, there's no directory for mysuperrepo no any of the files that I would expect from running git clone https://github.com/username/mysuperrepo.git from the command line

Question: Why wouldn't the code below produce a clone of the repo in the directory where the go program is running?

func clone(w http.ResponseWriter, r *http.Request){
  var repo = "https://github.com/username/mysuperrepo.git"
  exec.Command("git", "clone", repo)
  w.Write([]byte(repo))
}
func main(){
  http.HandleFunc("/clone/", clone)
  log.Fatal(http.ListenAndServe(":8080", nil))
}

答案1

得分: 8

你需要调用Run来实际执行该命令。

cmd := exec.Command("git", "clone", repo)
err := cmd.Run()
if err != nil {
    // 出现错误
}
英文:

You need to call Run to actually execute the command.

cmd := exec.Command("git", "clone", repo)
err := cmd.Run()
if err != nil {
    // something went wrong
}

huangapple
  • 本文由 发表于 2016年1月19日 09:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/34867203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定