/
get.go
58 lines (52 loc) · 1.65 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package toolchain
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/jingweno/ccat/Godeps/_workspace/src/sourcegraph.com/sourcegraph/srclib"
)
// Get downloads the toolchain named by the toolchain path (if it does not
// already exist in the SRCLIBPATH). If update is true, it uses the network to
// update the toolchain.
//
// Assumes that the clone URL is "https://" + path + ".git".
func Get(path string, update bool) (*Info, error) {
path = filepath.Clean(path)
if tc, err := Lookup(path); !os.IsNotExist(err) {
return tc, err
}
dir := strings.SplitN(srclib.Path, ":", 2)[0]
toolchainDir := filepath.Join(dir, path)
if fi, err := os.Stat(toolchainDir); os.IsNotExist(err) {
// older gits don't heed git https redirects, so manually substitute in
// the github.com clone url for sourcegraph.com clone urls
var substitutedPath string
if strings.HasPrefix(path, "sourcegraph.com/") {
substitutedPath = "github.com/" + strings.TrimPrefix(path, "sourcegraph.com/")
} else {
substitutedPath = path
}
cloneURL := "https://" + substitutedPath + ".git"
cmd := exec.Command("git", "clone", cloneURL, toolchainDir)
cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
} else if fi.Mode().IsDir() {
cmd := exec.Command("git", "pull", "origin", "master")
cmd.Dir = toolchainDir
cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
} else {
return nil, err
}
tc, err := Lookup(path)
if err != nil {
return nil, fmt.Errorf("get toolchain failed: %s (is %s a srclib toolchain repository?)", err, path)
}
return tc, nil
}