...
1 package helper
2
3 import (
4 "fmt"
5
6 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
7 "github.com/devfile/api/v2/pkg/attributes"
8 "github.com/devfile/library/v2/pkg/devfile/parser"
9 parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
10 . "github.com/onsi/gomega"
11 apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
12 "k8s.io/utils/pointer"
13
14 "github.com/redhat-developer/odo/pkg/devfile"
15 )
16
17
18
19 type DevfileUpdater func(*parser.DevfileObj) error
20
21
22 var DevfileMetadataNameSetter = func(name string) DevfileUpdater {
23 return func(d *parser.DevfileObj) error {
24 return d.SetMetadataName(name)
25 }
26 }
27
28
29 var DevfileMetadataNameRemover = DevfileMetadataNameSetter("")
30
31
32
33 var DevfileCommandGroupUpdater = func(cmdName string, cmdType v1alpha2.CommandType, group *v1alpha2.CommandGroup) DevfileUpdater {
34 return func(d *parser.DevfileObj) error {
35 cmds, err := d.Data.GetCommands(parsercommon.DevfileOptions{
36 CommandOptions: parsercommon.CommandOptions{
37 CommandType: cmdType,
38 },
39 FilterByName: cmdName,
40 })
41 if err != nil {
42 return err
43 }
44 if len(cmds) != 1 {
45 return fmt.Errorf("found %v command(s) with name %q", len(cmds), cmdName)
46 }
47 cmd := cmds[0]
48 switch cmdType {
49 case v1alpha2.ApplyCommandType:
50 cmd.Apply.Group = group
51 case v1alpha2.CompositeCommandType:
52 cmd.Composite.Group = group
53 case v1alpha2.CustomCommandType:
54 cmd.Custom.Group = group
55 case v1alpha2.ExecCommandType:
56 cmd.Exec.Group = group
57 default:
58 return fmt.Errorf("command type not handled: %q", cmdType)
59 }
60 return nil
61 }
62 }
63
64
65
66
67
68
69
70
71 func UpdateDevfileContent(path string, handlers []DevfileUpdater) {
72 if len(handlers) == 0 {
73
74 return
75 }
76
77 d, err := parser.ParseDevfile(parser.ParserArgs{
78 Path: path,
79 FlattenedDevfile: pointer.Bool(false),
80 SetBooleanDefaults: pointer.Bool(false),
81 })
82 Expect(err).NotTo(HaveOccurred())
83 for _, h := range handlers {
84 err = h(&d)
85 Expect(err).NotTo(HaveOccurred())
86 }
87 err = d.WriteYamlDevfile()
88 Expect(err).NotTo(HaveOccurred())
89 }
90
91
92
93 func SetFsGroup(containerName string, fsGroup int) DevfileUpdater {
94 return func(d *parser.DevfileObj) error {
95 containers, err := d.Data.GetComponents(parsercommon.DevfileOptions{
96 FilterByName: containerName,
97 })
98 Expect(err).NotTo(HaveOccurred())
99 Expect(len(containers)).To(Equal(1))
100 containers[0].Attributes = attributes.Attributes{
101 "pod-overrides": apiext.JSON{
102 Raw: []byte(fmt.Sprintf(`{"spec": {"securityContext": {"fsGroup": %d}}}`, fsGroup)),
103 },
104 }
105 err = d.Data.UpdateComponent(containers[0])
106 Expect(err).NotTo(HaveOccurred())
107 return nil
108 }
109 }
110
111
112 func ReadRawDevfile(devfilePath string) parser.DevfileObj {
113 d, err := devfile.ParseAndValidateFromFile(devfilePath, "", false)
114 Expect(err).ToNot(HaveOccurred())
115 return d
116 }
117
View as plain text