-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
96 lines (81 loc) · 1.74 KB
/
fetcher.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gomodprivate
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
type iPackageFetcher interface {
Fetch() error
}
type GoFetcher struct {
name string
}
func (g *GoFetcher) Fetch() error {
eCmd := exec.Command("go", []string{
"get",
g.name,
}...)
eCmd.Stdout = os.Stdout
eCmd.Stderr = os.Stderr
return eCmd.Run()
}
func NewGoFetcher(name string) *GoFetcher {
instance := new(GoFetcher)
instance.name = name
return instance
}
type SshFetcher struct {
name string
connString string
}
func (s *SshFetcher) Fetch() error {
targetDir := fmt.Sprintf("./.vendor.gomp/%s", s.name)
targetDir, err := filepath.Abs(targetDir)
if err != nil {
return err
}
packageName, tag, err := _ExtractTag(s.name)
if err != nil {
return err
}
if _, err := os.Lstat(targetDir + "/.git"); err == nil {
return s.update(targetDir)
}
if err := s._Fetch(packageName, tag, targetDir); err != nil {
return err
}
return nil
}
func (s *SshFetcher) _Fetch(name, tag, targetDir string) error {
cmdParam := make([]string, 0, 6)
cmdParam = append(cmdParam,
"clone",
"--depth",
"1")
if len(tag) > 0 {
cmdParam = append(cmdParam,
"--branch", tag)
}
cmdParam = append(cmdParam, s.connString+name, targetDir)
eCmd := exec.Command("git", cmdParam...)
eCmd.Stdout = os.Stdout
eCmd.Stderr = os.Stderr
return eCmd.Run()
}
func (s *SshFetcher) update(dir string) error {
eCmd := exec.Command("git", []string{
"pull",
"--rebase",
}...)
eCmd.Stdout = os.Stdout
eCmd.Stderr = os.Stderr
eCmd.Dir = dir
return eCmd.Run()
}
func NewSshFetcher(name, username, host, basePath string) *SshFetcher {
instance := new(SshFetcher)
instance.name = name
instance.connString = fmt.Sprintf("%s@%s:%s/", username, host, basePath)
return instance
}