Skip to content

Commit 59b15c7

Browse files
authored
Merge pull request #57 from yangcao77/309-relativeURI
relative uri support
2 parents 95e7515 + fb8405c commit 59b15c7

File tree

5 files changed

+304
-42
lines changed

5 files changed

+304
-42
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/devfile/library
33
go 1.13
44

55
require (
6-
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4
6+
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7
77
github.com/fatih/color v1.7.0
88
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
99
github.com/gobwas/glob v0.2.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
4444
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4545
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4646
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
47-
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4 h1:jDzWFpF/BoyaemoPjAzw5SmlX172JsSsh+Frujm5Ww4=
48-
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
47+
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7 h1:bQGUVLEGQtVkvS94K4gQbu57Rk/npcZQmgORmCWYNy8=
48+
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
4949
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5050
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
5151
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=

pkg/devfile/parser/context/context.go

Lines changed: 17 additions & 1 deletion
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 URIMap[d.absPath] {
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 URIMap[d.url] {
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

Lines changed: 39 additions & 11 deletions
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"
@@ -13,11 +16,10 @@ import (
1316

1417
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
1518
apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
19+
"github.com/devfile/api/v2/pkg/validation"
1620
"github.com/pkg/errors"
1721
)
1822

19-
var URLMap = make(map[string]bool)
20-
2123
// ParseDevfile func validates the devfile integrity.
2224
// Creates devfile context and runtime objects
2325
func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
@@ -46,8 +48,8 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
4648
return DevfileObj{}, err
4749
}
4850
}
49-
for url := range URLMap {
50-
delete(URLMap, url)
51+
for uri := range devfileCtx.URIMap {
52+
delete(devfileCtx.URIMap, uri)
5153
}
5254
// Successful
5355
return d, nil
@@ -84,11 +86,6 @@ func ParseRawDevfile(path string) (d DevfileObj, err error) {
8486
// ParseFromURL func parses and validates the devfile integrity.
8587
// Creates devfile context and runtime objects
8688
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-
}
9289
d.Ctx = devfileCtx.NewURLDevfileCtx(url)
9390
// Fill the fields of DevfileCtx struct
9491
err = d.Ctx.PopulateFromURL()
@@ -122,7 +119,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
122119
parent := d.Data.GetParent()
123120
var parentDevfileObj DevfileObj
124121
if d.Data.GetParent().Uri != "" {
125-
parentDevfileObj, err = ParseFromURL(parent.Uri)
122+
parentDevfileObj, err = parseFromURI(parent.Uri, d.Ctx)
126123
if err != nil {
127124
return err
128125
}
@@ -153,7 +150,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
153150
plugin := component.Plugin
154151
var pluginDevfileObj DevfileObj
155152
if plugin.Uri != "" {
156-
pluginDevfileObj, err = ParseFromURL(plugin.Uri)
153+
pluginDevfileObj, err = parseFromURI(plugin.Uri, d.Ctx)
157154
if err != nil {
158155
return err
159156
}
@@ -181,3 +178,34 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
181178

182179
return nil
183180
}
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

Comments
 (0)