Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relative uri support #57

Merged
merged 6 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/devfile/library
go 1.13

require (
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4 h1:jDzWFpF/BoyaemoPjAzw5SmlX172JsSsh+Frujm5Ww4=
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7 h1:bQGUVLEGQtVkvS94K4gQbu57Rk/npcZQmgORmCWYNy8=
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down
18 changes: 17 additions & 1 deletion pkg/devfile/parser/context/context.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package parser

import (
"fmt"
"net/url"

"github.com/devfile/library/pkg/testingutil/filesystem"
"github.com/devfile/library/pkg/util"
"k8s.io/klog"
)

var URIMap = make(map[string]bool)

// DevfileCtx stores context info regarding devfile
type DevfileCtx struct {

Expand Down Expand Up @@ -67,6 +70,10 @@ func (d *DevfileCtx) Populate() (err error) {
return err
}
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)
if URIMap[d.absPath] {
return fmt.Errorf("URI %v is recursively referenced", d.absPath)
}
URIMap[d.absPath] = true
// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand All @@ -81,7 +88,10 @@ func (d *DevfileCtx) PopulateFromURL() (err error) {
if err != nil {
return err
}

if URIMap[d.url] {
return fmt.Errorf("URI %v is recursively referenced", d.url)
}
URIMap[d.url] = true
// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand All @@ -101,10 +111,16 @@ func (d *DevfileCtx) Validate() error {
return d.ValidateDevfileSchema()
}

// GetAbsPath func returns current devfile absolute path
func (d *DevfileCtx) GetAbsPath() string {
return d.absPath
}

// GetURL func returns current devfile absolute URL address
func (d *DevfileCtx) GetURL() string {
return d.url
}

// SetAbsPath sets absolute file path for devfile
func (d *DevfileCtx) SetAbsPath() (err error) {
// Set devfile absolute path
Expand Down
50 changes: 39 additions & 11 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package parser
import (
"encoding/json"
"fmt"
"net/url"
"path"
"strings"

devfileCtx "github.com/devfile/library/pkg/devfile/parser/context"
"github.com/devfile/library/pkg/devfile/parser/data"
Expand All @@ -13,11 +16,10 @@ import (

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
"github.com/devfile/api/v2/pkg/validation"
"github.com/pkg/errors"
)

var URLMap = make(map[string]bool)

// ParseDevfile func validates the devfile integrity.
// Creates devfile context and runtime objects
func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
Expand Down Expand Up @@ -46,8 +48,8 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
return DevfileObj{}, err
}
}
for url := range URLMap {
delete(URLMap, url)
for uri := range devfileCtx.URIMap {
delete(devfileCtx.URIMap, uri)
}
// Successful
return d, nil
Expand Down Expand Up @@ -84,11 +86,6 @@ func ParseRawDevfile(path string) (d DevfileObj, err error) {
// ParseFromURL func parses and validates the devfile integrity.
// Creates devfile context and runtime objects
func ParseFromURL(url string) (d DevfileObj, err error) {
if _, exist := URLMap[url]; !exist {
URLMap[url] = true
} else {
return d, fmt.Errorf("URI %v is recursively referenced", url)
}
d.Ctx = devfileCtx.NewURLDevfileCtx(url)
// Fill the fields of DevfileCtx struct
err = d.Ctx.PopulateFromURL()
Expand Down Expand Up @@ -122,7 +119,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
parent := d.Data.GetParent()
var parentDevfileObj DevfileObj
if d.Data.GetParent().Uri != "" {
parentDevfileObj, err = ParseFromURL(parent.Uri)
parentDevfileObj, err = parseFromURI(parent.Uri, d.Ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +150,7 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
plugin := component.Plugin
var pluginDevfileObj DevfileObj
if plugin.Uri != "" {
pluginDevfileObj, err = ParseFromURL(plugin.Uri)
pluginDevfileObj, err = parseFromURI(plugin.Uri, d.Ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,3 +178,34 @@ func parseParentAndPlugin(d DevfileObj) (err error) {

return nil
}

func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj, error) {
// validate URI
err := validation.ValidateURI(uri)
if err != nil {
return DevfileObj{}, err
}

// absolute URL address
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
return ParseFromURL(uri)
}

// relative path on disk
if curDevfileCtx.GetAbsPath() != "" {
return Parse(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri))
}

if curDevfileCtx.GetURL() != "" {
u, err := url.Parse(curDevfileCtx.GetURL())
if err != nil {
return DevfileObj{}, err
}

u.Path = path.Join(path.Dir(u.Path), uri)
// u.String() is the joint absolute URL path
return ParseFromURL(u.String())
}

return DevfileObj{}, fmt.Errorf("fail to parse from uri: %s", uri)
}
Loading