...
1 package libdevfile
2
3 import (
4 "errors"
5 "net/url"
6 "sort"
7 "strings"
8
9 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
10 "github.com/devfile/api/v2/pkg/attributes"
11 "github.com/devfile/api/v2/pkg/validation"
12 "github.com/devfile/library/v2/pkg/devfile/parser"
13 "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
14 )
15
16 const _importSourceAttributeUriPrefix = "uri: "
17
18
19
20
21
22
23
24 func GetReferencedLocalFiles(devfileObj parser.DevfileObj) (result []string, err error) {
25
26 setResult := map[string]struct{}{}
27
28 parent := devfileObj.Data.GetParent()
29 if parent != nil {
30 return nil, errors.New("devfile must be flattened")
31 }
32
33 components, err := devfileObj.Data.GetComponents(common.DevfileOptions{})
34 if err != nil {
35 return nil, err
36 }
37
38 for _, component := range components {
39 var componentType v1alpha2.ComponentType
40 componentType, err = common.GetComponentType(component)
41 if err != nil {
42 return nil, err
43 }
44
45 switch componentType {
46 case v1alpha2.KubernetesComponentType:
47 setResult, err = appendUriIfFile(setResult, component.Kubernetes.Uri)
48 if err != nil {
49 return nil, err
50 }
51
52 case v1alpha2.OpenshiftComponentType:
53 setResult, err = appendUriIfFile(setResult, component.Openshift.Uri)
54 if err != nil {
55 return nil, err
56 }
57
58 case v1alpha2.ImageComponentType:
59 if component.Image.Dockerfile != nil {
60 setResult, err = appendUriIfFile(setResult, component.Image.Dockerfile.Uri)
61 if err != nil {
62 return nil, err
63 }
64 }
65 }
66
67 setResult, err = getFromAttributes(setResult, component.Attributes)
68 if err != nil {
69 return nil, err
70 }
71 }
72
73 commands, err := devfileObj.Data.GetCommands(common.DevfileOptions{})
74 if err != nil {
75 return nil, err
76 }
77 for _, command := range commands {
78 setResult, err = getFromAttributes(setResult, command.Attributes)
79 if err != nil {
80 return nil, err
81 }
82 }
83
84 result = make([]string, 0, len(setResult))
85 for k := range setResult {
86 result = append(result, k)
87 }
88 sort.Strings(result)
89 return result, nil
90 }
91
92
93 func appendUriIfFile(result map[string]struct{}, uri string) (map[string]struct{}, error) {
94 if uri != "" {
95 u, err := url.Parse(uri)
96 if err != nil {
97 return nil, err
98 }
99 if u.Scheme == "" {
100 result[uri] = struct{}{}
101 }
102 }
103 return result, nil
104 }
105
106
107
108 func getFromAttributes(result map[string]struct{}, attributes attributes.Attributes) (map[string]struct{}, error) {
109 if val, ok := attributes[validation.ImportSourceAttribute]; ok {
110 strVal := string(val.Raw)
111 strVal = strings.Trim(strVal, `"`)
112 if strings.HasPrefix(strVal, _importSourceAttributeUriPrefix) {
113 parentUri := strings.TrimLeft(strVal, _importSourceAttributeUriPrefix)
114 var err error
115 result, err = appendUriIfFile(result, parentUri)
116 if err != nil {
117 return nil, err
118 }
119 }
120 }
121
122 return result, nil
123 }
124
View as plain text