forked from hyperjumptech/grule-rule-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitresource.go
56 lines (44 loc) · 1.21 KB
/
gitresource.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
//go:build go1.11
// +build go1.11
package pkg
import (
"fmt"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
http2 "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
// Load will load the file from your git repository
func (bundle *GITResourceBundle) Load() ([]Resource, error) {
fileSystem := memfs.New()
CloneOpts := &git.CloneOptions{}
if len(bundle.URL) == 0 {
return nil, fmt.Errorf("GIT URL is not specified")
}
CloneOpts.URL = bundle.URL
if len(bundle.RefName) == 0 {
CloneOpts.ReferenceName = plumbing.ReferenceName("refs/heads/master")
} else {
CloneOpts.ReferenceName = plumbing.ReferenceName(bundle.RefName)
}
if len(bundle.Remote) == 0 {
CloneOpts.RemoteName = "origin"
} else {
CloneOpts.RemoteName = bundle.Remote
}
if len(bundle.PathPattern) == 0 {
return nil, fmt.Errorf("no path pattern specified")
}
if len(bundle.User) != 0 {
CloneOpts.Auth = &http2.BasicAuth{
Username: bundle.User,
Password: bundle.Password,
}
}
_, err := git.Clone(memory.NewStorage(), fileSystem, CloneOpts)
if err != nil {
return nil, err
}
return bundle.loadPath(bundle.URL, "/", fileSystem)
}