@@ -3,6 +3,9 @@ package parser
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "net/url"
7
+ "path"
8
+ "strings"
6
9
7
10
devfileCtx "github.com/devfile/library/pkg/devfile/parser/context"
8
11
"github.com/devfile/library/pkg/devfile/parser/data"
@@ -13,11 +16,10 @@ import (
13
16
14
17
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
15
18
apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
19
+ "github.com/devfile/api/v2/pkg/validation"
16
20
"github.com/pkg/errors"
17
21
)
18
22
19
- var URLMap = make (map [string ]bool )
20
-
21
23
// ParseDevfile func validates the devfile integrity.
22
24
// Creates devfile context and runtime objects
23
25
func parseDevfile (d DevfileObj , flattenedDevfile bool ) (DevfileObj , error ) {
@@ -46,8 +48,8 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
46
48
return DevfileObj {}, err
47
49
}
48
50
}
49
- for url := range URLMap {
50
- delete (URLMap , url )
51
+ for uri := range devfileCtx . URIMap {
52
+ delete (devfileCtx . URIMap , uri )
51
53
}
52
54
// Successful
53
55
return d , nil
@@ -84,11 +86,6 @@ func ParseRawDevfile(path string) (d DevfileObj, err error) {
84
86
// ParseFromURL func parses and validates the devfile integrity.
85
87
// Creates devfile context and runtime objects
86
88
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
- }
92
89
d .Ctx = devfileCtx .NewURLDevfileCtx (url )
93
90
// Fill the fields of DevfileCtx struct
94
91
err = d .Ctx .PopulateFromURL ()
@@ -122,7 +119,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
122
119
parent := d .Data .GetParent ()
123
120
var parentDevfileObj DevfileObj
124
121
if d .Data .GetParent ().Uri != "" {
125
- parentDevfileObj , err = ParseFromURL (parent .Uri )
122
+ parentDevfileObj , err = parseFromURI (parent .Uri , d . Ctx )
126
123
if err != nil {
127
124
return err
128
125
}
@@ -153,7 +150,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
153
150
plugin := component .Plugin
154
151
var pluginDevfileObj DevfileObj
155
152
if plugin .Uri != "" {
156
- pluginDevfileObj , err = ParseFromURL (plugin .Uri )
153
+ pluginDevfileObj , err = parseFromURI (plugin .Uri , d . Ctx )
157
154
if err != nil {
158
155
return err
159
156
}
@@ -181,3 +178,34 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
181
178
182
179
return nil
183
180
}
181
+
182
+ func parseFromURI (uri string , curDevfileCtx devfileCtx.DevfileCtx ) (DevfileObj , error ) {
183
+ // validate URI
184
+ err := validation .ValidateURI (uri )
185
+ if err != nil {
186
+ return DevfileObj {}, err
187
+ }
188
+
189
+ // absolute URL address
190
+ if strings .HasPrefix (uri , "http://" ) || strings .HasPrefix (uri , "https://" ) {
191
+ return ParseFromURL (uri )
192
+ }
193
+
194
+ // relative path on disk
195
+ if curDevfileCtx .GetAbsPath () != "" {
196
+ return Parse (path .Join (path .Dir (curDevfileCtx .GetAbsPath ()), uri ))
197
+ }
198
+
199
+ if curDevfileCtx .GetURL () != "" {
200
+ u , err := url .Parse (curDevfileCtx .GetURL ())
201
+ if err != nil {
202
+ return DevfileObj {}, err
203
+ }
204
+
205
+ u .Path = path .Join (path .Dir (u .Path ), uri )
206
+ // u.String() is the joint absolute URL path
207
+ return ParseFromURL (u .String ())
208
+ }
209
+
210
+ return DevfileObj {}, fmt .Errorf ("fail to parse from uri: %s" , uri )
211
+ }
0 commit comments