Skip to content

Commit 5b0a856

Browse files
committed
add parseURI func
Signed-off-by: Stephanie <[email protected]>
1 parent 741af7b commit 5b0a856

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

pkg/devfile/parser/context/context.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package parser
22

33
import (
4+
"fmt"
45
"net/url"
56

67
"github.com/devfile/library/pkg/testingutil/filesystem"
78
"github.com/devfile/library/pkg/util"
89
"k8s.io/klog"
910
)
1011

12+
var URIMap = make(map[string]bool)
13+
1114
// DevfileCtx stores context info regarding devfile
1215
type DevfileCtx struct {
1316

@@ -67,6 +70,10 @@ func (d *DevfileCtx) Populate() (err error) {
6770
return err
6871
}
6972
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)
73+
if _, exist := URIMap[d.absPath]; exist {
74+
return fmt.Errorf("URI %v is recursively referenced", d.absPath)
75+
}
76+
URIMap[d.absPath] = true
7077
// Read and save devfile content
7178
if err := d.SetDevfileContent(); err != nil {
7279
return err
@@ -81,7 +88,10 @@ func (d *DevfileCtx) PopulateFromURL() (err error) {
8188
if err != nil {
8289
return err
8390
}
84-
91+
if _, exist := URIMap[d.url]; exist {
92+
return fmt.Errorf("URI %v is recursively referenced", d.url)
93+
}
94+
URIMap[d.url] = true
8595
// Read and save devfile content
8696
if err := d.SetDevfileContent(); err != nil {
8797
return err
@@ -101,10 +111,16 @@ func (d *DevfileCtx) Validate() error {
101111
return d.ValidateDevfileSchema()
102112
}
103113

114+
// GetAbsPath func returns current devfile absolute path
104115
func (d *DevfileCtx) GetAbsPath() string {
105116
return d.absPath
106117
}
107118

119+
// GetURL func returns current devfile absolute URL address
120+
func (d *DevfileCtx) GetURL() string {
121+
return d.url
122+
}
123+
108124
// SetAbsPath sets absolute file path for devfile
109125
func (d *DevfileCtx) SetAbsPath() (err error) {
110126
// Set devfile absolute path

pkg/devfile/parser/parse.go

+42-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package parser
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/url"
7+
"path"
8+
"strings"
69

710
devfileCtx "github.com/devfile/library/pkg/devfile/parser/context"
811
"github.com/devfile/library/pkg/devfile/parser/data"
@@ -16,7 +19,7 @@ import (
1619
"github.com/pkg/errors"
1720
)
1821

19-
var URLMap = make(map[string]bool)
22+
2023

2124
// ParseDevfile func validates the devfile integrity.
2225
// Creates devfile context and runtime objects
@@ -46,8 +49,8 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
4649
return DevfileObj{}, err
4750
}
4851
}
49-
for url := range URLMap {
50-
delete(URLMap, url)
52+
for uri := range devfileCtx.URIMap {
53+
delete(devfileCtx.URIMap, uri)
5154
}
5255
// Successful
5356
return d, nil
@@ -84,11 +87,6 @@ func ParseRawDevfile(path string) (d DevfileObj, err error) {
8487
// ParseFromURL func parses and validates the devfile integrity.
8588
// Creates devfile context and runtime objects
8689
func ParseFromURL(url string) (d DevfileObj, err error) {
87-
if _, exist := URLMap[url]; !exist {
88-
URLMap[url] = true
89-
} else {
90-
return d, fmt.Errorf("URI %v is recursively referenced", url)
91-
}
9290
d.Ctx = devfileCtx.NewURLDevfileCtx(url)
9391
// Fill the fields of DevfileCtx struct
9492
err = d.Ctx.PopulateFromURL()
@@ -122,7 +120,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
122120
parent := d.Data.GetParent()
123121
var parentDevfileObj DevfileObj
124122
if d.Data.GetParent().Uri != "" {
125-
parentDevfileObj, err = ParseFromURL(parent.Uri)
123+
parentDevfileObj, err = parseFromURI(parent.Uri, d)
126124
if err != nil {
127125
return err
128126
}
@@ -153,7 +151,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
153151
plugin := component.Plugin
154152
var pluginDevfileObj DevfileObj
155153
if plugin.Uri != "" {
156-
pluginDevfileObj, err = ParseFromURL(plugin.Uri)
154+
pluginDevfileObj, err = parseFromURI(plugin.Uri, d)
157155
if err != nil {
158156
return err
159157
}
@@ -181,3 +179,37 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
181179

182180
return nil
183181
}
182+
183+
184+
185+
func parseFromURI(uri string, curDevfile DevfileObj) (DevfileObj, error){
186+
// validate URI
187+
188+
// absolute URL address
189+
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
190+
return ParseFromURL(uri)
191+
}
192+
193+
// absolute path on disk
194+
if strings.HasPrefix(uri, "file://") {
195+
return Parse(strings.TrimPrefix(uri, "file://"))
196+
}
197+
198+
// relative path on disk
199+
if curDevfile.Ctx.GetAbsPath() != ""{
200+
// parse uri receives a relative path and resolves abspath
201+
return Parse(uri)
202+
}
203+
204+
if curDevfile.Ctx.GetURL() != ""{
205+
u, err := url.Parse(curDevfile.Ctx.GetURL())
206+
if err!= nil {
207+
return DevfileObj{}, err
208+
}
209+
u.Path = path.Join(u.Path, uri)
210+
jointURL := u.String()
211+
return ParseFromURL(jointURL)
212+
}
213+
214+
return DevfileObj{}, fmt.Errorf("fail to parse from uri: %s", uri)
215+
}

0 commit comments

Comments
 (0)