-
Notifications
You must be signed in to change notification settings - Fork 10
/
clone.go
57 lines (52 loc) · 1.6 KB
/
clone.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
package runner
import (
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/padok-team/burrito/internal/burrito/config"
log "github.com/sirupsen/logrus"
)
func clone(repository config.RepositoryConfig, URL, branch, path string) (*git.Repository, error) {
cloneOptions, err := getCloneOptions(repository, URL, branch, path)
if err != nil {
return &git.Repository{}, err
}
return git.PlainClone(WorkingDir, false, cloneOptions)
}
func getCloneOptions(repository config.RepositoryConfig, URL, branch, path string) (*git.CloneOptions, error) {
authMethod := "ssh"
cloneOptions := &git.CloneOptions{
ReferenceName: plumbing.NewBranchReferenceName(branch),
URL: URL,
}
if strings.Contains(URL, "https://") {
authMethod = "https"
}
log.Infof("clone method is %s", authMethod)
switch authMethod {
case "ssh":
if repository.SSHPrivateKey == "" {
log.Infof("detected keyless authentication")
return cloneOptions, nil
}
log.Infof("private key found")
publicKeys, err := ssh.NewPublicKeys("git", []byte(repository.SSHPrivateKey), "")
if err != nil {
return cloneOptions, err
}
cloneOptions.Auth = publicKeys
case "https":
if repository.Username != "" && repository.Password != "" {
log.Infof("username and password found")
cloneOptions.Auth = &http.BasicAuth{
Username: repository.Username,
Password: repository.Password,
}
} else {
log.Infof("passwordless authentication detected")
}
}
return cloneOptions, nil
}