-
Notifications
You must be signed in to change notification settings - Fork 3
/
github.go
56 lines (42 loc) · 859 Bytes
/
github.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
package main
import (
"fmt"
"log"
"path/filepath"
"gopkg.in/src-d/go-git.v4"
)
type GitHubDownloader struct {
path string
}
func GitHub(path string) *GitHubDownloader {
return &GitHubDownloader{path: path}
}
func (gh *GitHubDownloader) Update(folder, name string) error {
log.Printf("Updating github repo %s", name)
url := fmt.Sprintf("https://github.com/%s.git", name)
path := filepath.Join(gh.path, folder)
_, err := git.PlainClone(path, false, &git.CloneOptions{
URL: url,
})
if err == git.ErrRepositoryAlreadyExists {
return nil
}
if err != nil {
return err
}
repo, err := git.PlainOpen(path)
if err != nil {
return err
}
worktree, err := repo.Worktree()
if err != nil {
return err
}
err = worktree.Pull(&git.PullOptions{
Force: true,
})
if err == git.NoErrAlreadyUpToDate {
return nil
}
return err
}