Skip to content

Commit 2da3260

Browse files
committed
Adds function to parse git url seperately
1 parent 4bf81ec commit 2da3260

File tree

1 file changed

+42
-10
lines changed
  • pkg/patterns/addon/pkg/loaders

1 file changed

+42
-10
lines changed

pkg/patterns/addon/pkg/loaders/git.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ import (
1414

1515
type GitRepository struct {
1616
baseURL string
17+
subDir string
18+
branch string
1719
}
1820

1921
var _ Repository = &GitRepository{}
2022

2123
// NewGitRepository constructs an GitRepository
2224
func NewGitRepository(baseurl string) *GitRepository{
23-
return &GitRepository{baseURL: baseurl}
25+
baseurl, subDir := parseGitUrl(baseurl)
26+
return &GitRepository{
27+
baseURL: baseurl,
28+
subDir: subDir,
29+
}
2430
}
2531

2632
func (r *GitRepository) LoadChannel(ctx context.Context, name string) (*Channel, error){
@@ -32,6 +38,9 @@ func (r *GitRepository) LoadChannel(ctx context.Context, name string) (*Channel
3238
log.WithValues("baseURL", r.baseURL).Info("loading channel")
3339
log.WithValues("baseURL", r.baseURL).Info("cloning git repository")
3440

41+
if r.subDir != "" {
42+
name = r.subDir + "/" + name
43+
}
3544
b, err := r.readURL(name)
3645
if err != nil{
3746
log.WithValues("path", name).Error(err, "error reading channel")
@@ -60,7 +69,13 @@ func (r *GitRepository) LoadManifest(ctx context.Context, packageName string, id
6069
log := log.Log
6170
log.WithValues("package", packageName).Info("loading package")
6271

63-
filePath := fmt.Sprintf("packages/%v/%v/manifest.yaml", packageName, id)
72+
var filePath string
73+
if r.subDir == "" {
74+
filePath = fmt.Sprintf("packages/%v/%v/manifest.yaml", packageName, id)
75+
} else {
76+
filePath = fmt.Sprintf("%v/packages/%v/%v/manifest.yaml", r.subDir, packageName, id)
77+
}
78+
6479
fullPath := fmt.Sprintf("%v/%v", r.baseURL, filePath)
6580
fmt.Println(fullPath)
6681
b, err := r.readURL(filePath)
@@ -76,16 +91,16 @@ func (r *GitRepository) LoadManifest(ctx context.Context, packageName string, id
7691
}
7792

7893
func (r *GitRepository) readURL(url string) ([]byte, error) {
79-
// Adds support for sub directory
80-
cloneUrl := r.baseURL
81-
if strings.Contains(r.baseURL, ".git//") {
82-
newURL := strings.Split(r.baseURL, ".git//")
83-
cloneUrl = newURL[0] + ".git"
84-
url = newURL[1] + "/" + url
85-
}
94+
//// Adds support for sub directory
95+
//cloneUrl := r.baseURL
96+
//if strings.Contains(r.baseURL, ".git//") {
97+
// newURL := strings.Split(r.baseURL, ".git//")
98+
// cloneUrl = newURL[0] + ".git"
99+
// url = newURL[1] + "/" + url
100+
//}
86101
fs := memfs.New()
87102
_, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
88-
URL: cloneUrl,
103+
URL: r.baseURL,
89104
})
90105
if err != nil {
91106
return nil, err
@@ -103,3 +118,20 @@ func (r *GitRepository) readURL(url string) ([]byte, error) {
103118

104119
return b, nil
105120
}
121+
122+
func parseGitUrl(url string) (string, string){
123+
// checks for git:: suffix
124+
var subdir string
125+
if strings.HasPrefix(url, "git::") {
126+
url = strings.TrimPrefix(url, "git::")
127+
}
128+
129+
// checks for subdirectories
130+
if strings.Contains(url, ".git//") {
131+
newURL := strings.Split(url, ".git//")
132+
url = newURL[0] + ".git"
133+
subdir = newURL[1]
134+
}
135+
136+
return url, subdir
137+
}

0 commit comments

Comments
 (0)