Skip to content

Commit fce19d0

Browse files
committed
Cleanup & Update metadata func
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 3066a89 commit fce19d0

13 files changed

+106
-299
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g
1616
1717
// To get all the components from the devfile
1818
components, err := devfile.Data.GetComponents(DevfileOptions{})
19+
20+
// To get all the components from the devfile with attributes tagged - tool: console-import & import: {strategy: Dockerfile}
21+
components, err := devfile.Data.GetComponents(DevfileOptions{
22+
Filter: map[string]interface{}{
23+
"tool": "console-import",
24+
"import": map[string]interface{}{
25+
"strategy": "Dockerfile",
26+
},
27+
},
28+
})
1929
```
2030
2. To get the Kubernetes objects from the devfile, visit pkg/devfile/generator/generators.go
2131
```

go.mod

+1-1
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-20210211160219-33a78aec06af
6+
github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835
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

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ 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=
4747
github.com/devfile/api/v2 v2.0.0-20210211160219-33a78aec06af h1:egbFAAS/CWJMAqa4zIm8Cik3+iCqTAfLPfCj6PLEG5Y=
4848
github.com/devfile/api/v2 v2.0.0-20210211160219-33a78aec06af/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
49+
github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835 h1:PalHtpqhvX/yu5DKFqwTWa1h7UJBd0H7+veJStur/wg=
50+
github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
4951
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5052
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
5153
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=

pkg/devfile/generator/generators_test.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"reflect"
55
"testing"
66

7+
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
78
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
89
"github.com/devfile/api/v2/pkg/attributes"
910
"github.com/devfile/library/pkg/devfile/parser"
11+
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
1012
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1113
"github.com/devfile/library/pkg/testingutil"
1214

@@ -25,6 +27,21 @@ func TestGetContainers(t *testing.T) {
2527
containerImages := []string{"image1", "image2"}
2628
trueMountSources := true
2729
falseMountSources := false
30+
31+
project := v1alpha2.Project{
32+
ClonePath: "test-project/",
33+
Name: "project0",
34+
ProjectSource: v1.ProjectSource{
35+
Git: &v1.GitProjectSource{
36+
GitLikeProjectSource: v1.GitLikeProjectSource{
37+
Remotes: map[string]string{
38+
"origin": "repo",
39+
},
40+
},
41+
},
42+
},
43+
}
44+
2845
tests := []struct {
2946
name string
3047
containerComponents []v1.Component
@@ -167,8 +184,17 @@ func TestGetContainers(t *testing.T) {
167184
t.Run(tt.name, func(t *testing.T) {
168185

169186
devObj := parser.DevfileObj{
170-
Data: &testingutil.TestDevfileData{
171-
Components: tt.containerComponents,
187+
Data: &v2.DevfileV2{
188+
Devfile: v1.Devfile{
189+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
190+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
191+
Components: tt.containerComponents,
192+
Projects: []v1alpha2.Project{
193+
project,
194+
},
195+
},
196+
},
197+
},
172198
},
173199
}
174200

pkg/devfile/generator/utils_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/devfile/api/v2/pkg/attributes"
1010
"github.com/devfile/library/pkg/devfile/parser"
11+
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
1112
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1213
"github.com/devfile/library/pkg/testingutil"
1314
buildv1 "github.com/openshift/api/build/v1"
@@ -747,8 +748,14 @@ func TestGetServiceSpec(t *testing.T) {
747748
t.Run(tt.name, func(t *testing.T) {
748749

749750
devObj := parser.DevfileObj{
750-
Data: &testingutil.TestDevfileData{
751-
Components: tt.containerComponents,
751+
Data: &v2.DevfileV2{
752+
Devfile: v1.Devfile{
753+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
754+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
755+
Components: tt.containerComponents,
756+
},
757+
},
758+
},
752759
},
753760
}
754761

@@ -1076,10 +1083,17 @@ func TestGetPortExposure(t *testing.T) {
10761083
for _, tt := range tests {
10771084
t.Run(tt.name, func(t *testing.T) {
10781085
devObj := parser.DevfileObj{
1079-
Data: &testingutil.TestDevfileData{
1080-
Components: tt.containerComponents,
1086+
Data: &v2.DevfileV2{
1087+
Devfile: v1.Devfile{
1088+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
1089+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
1090+
Components: tt.containerComponents,
1091+
},
1092+
},
1093+
},
10811094
},
10821095
}
1096+
10831097
mapCreated, err := getPortExposure(devObj, tt.filterOptions)
10841098
if !tt.wantErr && err != nil {
10851099
t.Errorf("TestGetPortExposure unexpected error: %v", err)

pkg/devfile/parser/configurables.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const (
2222
// SetMetadataName set metadata name in a devfile
2323
func (d DevfileObj) SetMetadataName(name string) error {
2424
metadata := d.Data.GetMetadata()
25-
d.Data.SetMetadata(name, metadata.Version)
25+
metadata.Name = name
26+
d.Data.SetMetadata(metadata)
2627
return d.WriteYamlDevfile()
2728
}
2829

pkg/devfile/parser/data/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type DevfileData interface {
1111
GetSchemaVersion() string
1212
SetSchemaVersion(version string)
1313
GetMetadata() devfilepkg.DevfileMetadata
14-
SetMetadata(name, version string)
14+
SetMetadata(metadata devfilepkg.DevfileMetadata)
1515

1616
// parent related methods
1717
GetParent() *v1.Parent

pkg/devfile/parser/data/v2/common/command_helper.go

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ func GetGroup(dc v1.Command) *v1.CommandGroup {
1313
return dc.Exec.Group
1414
case dc.Apply != nil:
1515
return dc.Apply.Group
16-
case dc.VscodeLaunch != nil:
17-
return dc.VscodeLaunch.Group
18-
case dc.VscodeTask != nil:
19-
return dc.VscodeTask.Group
2016
case dc.Custom != nil:
2117
return dc.Custom.Group
2218

pkg/devfile/parser/data/v2/common/command_helper_test.go

+5-45
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestGetGroup(t *testing.T) {
1515
want *v1.CommandGroup
1616
}{
1717
{
18-
name: "Case 1: Exec command group",
18+
name: "Exec command group",
1919
command: v1.Command{
2020
Id: "exec1",
2121
CommandUnion: v1.CommandUnion{
@@ -37,7 +37,7 @@ func TestGetGroup(t *testing.T) {
3737
},
3838
},
3939
{
40-
name: "Case 2: Composite command group",
40+
name: "Composite command group",
4141
command: v1.Command{
4242
Id: "composite1",
4343
CommandUnion: v1.CommandUnion{
@@ -59,12 +59,12 @@ func TestGetGroup(t *testing.T) {
5959
},
6060
},
6161
{
62-
name: "Case 3: Empty command",
62+
name: "Empty command",
6363
command: v1.Command{},
6464
want: nil,
6565
},
6666
{
67-
name: "Case 4: Apply command group",
67+
name: "Apply command group",
6868
command: v1.Command{
6969
Id: "apply1",
7070
CommandUnion: v1.CommandUnion{
@@ -86,47 +86,7 @@ func TestGetGroup(t *testing.T) {
8686
},
8787
},
8888
{
89-
name: "Case 5: VscodeTaskcommand group",
90-
command: v1.Command{
91-
Id: "vscodetask1",
92-
CommandUnion: v1.CommandUnion{
93-
VscodeTask: &v1.VscodeConfigurationCommand{
94-
BaseCommand: v1.BaseCommand{
95-
Group: &v1.CommandGroup{
96-
IsDefault: true,
97-
Kind: v1.TestCommandGroupKind,
98-
},
99-
},
100-
},
101-
},
102-
},
103-
want: &v1.CommandGroup{
104-
IsDefault: true,
105-
Kind: v1.TestCommandGroupKind,
106-
},
107-
},
108-
{
109-
name: "Case 6: VscodeLaunchCommand group",
110-
command: v1.Command{
111-
Id: "vscodetask1",
112-
CommandUnion: v1.CommandUnion{
113-
VscodeLaunch: &v1.VscodeConfigurationCommand{
114-
BaseCommand: v1.BaseCommand{
115-
Group: &v1.CommandGroup{
116-
IsDefault: true,
117-
Kind: v1.DebugCommandGroupKind,
118-
},
119-
},
120-
},
121-
},
122-
},
123-
want: &v1.CommandGroup{
124-
IsDefault: true,
125-
Kind: v1.DebugCommandGroupKind,
126-
},
127-
},
128-
{
129-
name: "Case 7: Custom command group",
89+
name: "Custom command group",
13090
command: v1.Command{
13191
Id: "custom1",
13292
CommandUnion: v1.CommandUnion{

pkg/devfile/parser/data/v2/header.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ func (d *DevfileV2) GetMetadata() devfilepkg.DevfileMetadata {
2020
}
2121

2222
// SetMetadata sets the metadata for devfile
23-
func (d *DevfileV2) SetMetadata(name, version string) {
24-
d.Metadata = devfilepkg.DevfileMetadata{
25-
Name: name,
26-
Version: version,
27-
}
23+
func (d *DevfileV2) SetMetadata(metadata devfilepkg.DevfileMetadata) {
24+
d.Metadata = metadata
2825
}

pkg/devfile/parser/data/v2/header_test.go

+33-8
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ func TestDevfile200_SetSetMetadata(t *testing.T) {
153153
}
154154
tests := []struct {
155155
name string
156-
metadataName string
157-
metadataVersion string
156+
metadata devfilepkg.DevfileMetadata
158157
devfilev2 *DevfileV2
159158
expectedDevfilev2 *DevfileV2
160159
}{
@@ -165,8 +164,10 @@ func TestDevfile200_SetSetMetadata(t *testing.T) {
165164
DevfileHeader: devfilepkg.DevfileHeader{},
166165
},
167166
},
168-
metadataName: "nodejs",
169-
metadataVersion: "2.0.0",
167+
metadata: devfilepkg.DevfileMetadata{
168+
Name: "nodejs",
169+
Version: "2.0.0",
170+
},
170171
expectedDevfilev2: &DevfileV2{
171172
v1.Devfile{
172173
DevfileHeader: devfilepkg.DevfileHeader{
@@ -187,15 +188,39 @@ func TestDevfile200_SetSetMetadata(t *testing.T) {
187188
},
188189
},
189190
},
190-
metadataName: "nodejs",
191-
metadataVersion: "2.0.0",
191+
metadata: devfilepkg.DevfileMetadata{
192+
Name: "nodejs",
193+
Version: "2.1.0",
194+
Attributes: attributes.Attributes{}.FromMap(map[string]interface{}{
195+
"xyz": "xyz",
196+
}, nil),
197+
DisplayName: "display",
198+
Description: "decription",
199+
Tags: []string{"tag1"},
200+
Icon: "icon",
201+
GlobalMemoryLimit: "globalmemorylimit",
202+
ProjectType: "projectype",
203+
Language: "language",
204+
Website: "website",
205+
},
192206
expectedDevfilev2: &DevfileV2{
193207
v1.Devfile{
194208
DevfileHeader: devfilepkg.DevfileHeader{
195209
SchemaVersion: "2.0.0",
196210
Metadata: devfilepkg.DevfileMetadata{
197211
Name: "nodejs",
198-
Version: "2.0.0",
212+
Version: "2.1.0",
213+
Attributes: attributes.Attributes{}.FromMap(map[string]interface{}{
214+
"xyz": "xyz",
215+
}, nil),
216+
DisplayName: "display",
217+
Description: "decription",
218+
Tags: []string{"tag1"},
219+
Icon: "icon",
220+
GlobalMemoryLimit: "globalmemorylimit",
221+
ProjectType: "projectype",
222+
Language: "language",
223+
Website: "website",
199224
},
200225
},
201226
},
@@ -204,7 +229,7 @@ func TestDevfile200_SetSetMetadata(t *testing.T) {
204229
}
205230
for _, tt := range tests {
206231
t.Run(tt.name, func(t *testing.T) {
207-
tt.devfilev2.SetMetadata(tt.metadataName, tt.metadataVersion)
232+
tt.devfilev2.SetMetadata(tt.metadata)
208233
if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) {
209234
t.Errorf("TestDevfile200_SetSchemaVersion() expected %v, got %v", tt.expectedDevfilev2, tt.devfilev2)
210235
}

pkg/devfile/parser/parse.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ func ParseFromData(data []byte) (d DevfileObj, err error) {
113113

114114
func parseParentAndPlugin(d DevfileObj) (err error) {
115115
flattenedParent := &v1.DevWorkspaceTemplateSpecContent{}
116-
if d.Data.GetParent() != nil {
117-
if !reflect.DeepEqual(d.Data.GetParent(), &v1.Parent{}) {
116+
parent := d.Data.GetParent()
117+
if parent != nil {
118+
if !reflect.DeepEqual(parent, &v1.Parent{}) {
118119

119-
parent := d.Data.GetParent()
120120
var parentDevfileObj DevfileObj
121-
if d.Data.GetParent().Uri != "" {
121+
if parent.Uri != "" {
122122
parentDevfileObj, err = parseFromURI(parent.Uri, d.Ctx)
123123
if err != nil {
124124
return err

0 commit comments

Comments
 (0)