forked from getantibody/antibody
/
git.go
47 lines (40 loc) · 962 Bytes
/
git.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
package git
import (
"os"
"os/exec"
"path/filepath"
"strings"
)
// Repo represents a git repository
type Repo struct {
url, name, folder string
}
// NewGithubRepo creates a new Github Repo with the fullName and local folder.
func NewGithubRepo(fullName, folder string) Repo {
return Repo{
url: "https://github.com/" + fullName,
name: fullName,
folder: filepath.Join(folder, strings.Replace(fullName, "/", "-", -1)),
}
}
// Folder where the repo was cloned
func (r Repo) Folder() string {
return r.folder
}
// Name of the repo
func (r Repo) Name() string {
return r.name
}
// Download clones a repository
func (r Repo) Download() error {
if _, err := os.Stat(r.folder); os.IsNotExist(err) {
return exec.Command(
"git", "clone", "--depth", "1", r.url, r.folder,
).Run()
}
return nil
}
// Update updates a repository
func (r Repo) Update() error {
return exec.Command("git", "-C", r.folder, "pull", "origin", "master").Run()
}