-
Notifications
You must be signed in to change notification settings - Fork 37
/
provider.go
188 lines (157 loc) · 5.73 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package builtin
import (
"context"
"os"
"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/provider"
"github.com/swaggest/openapi-go/openapi3"
"gopkg.in/yaml.v2"
)
const TAGS_FILE_INIT_OPTION = "tagsFile"
var (
filePathsDescription = "file pattern to search"
)
var capabilities = []provider.Capability{}
type builtinCondition struct {
Filecontent fileContentCondition `yaml:"filecontent"`
File fileCondition `yaml:"file"`
XML xmlCondition `yaml:"xml"`
XMLPublicID xmlPublicIDCondition `yaml:"xmlPublicID"`
JSON jsonCondition `yaml:"json"`
HasTags []string `yaml:"hasTags"`
provider.ProviderContext `yaml:",inline"`
}
type fileContentCondition struct {
FilePattern string `yaml:"filePattern" json:"filePattern,omitempty" title:"FilePattern" description:"Only search in files with names matching this pattern"`
Pattern string `yaml:"pattern" json:"pattern" title:"Pattern" description:"Regex pattern to match in content"`
}
type fileCondition struct {
Pattern string `yaml:"pattern" json:"pattern" title:"Pattern" description:"Find files with names matching this pattern"`
}
var _ provider.InternalProviderClient = &builtinProvider{}
type xmlCondition struct {
XPath string `yaml:"xpath" json:"xpath" title:"XPath" description:"Xpath query"`
Namespaces map[string]string `yaml:"namespaces" json:"namespace,omitempty" title:"Namespaces" description:"A map to scope down query to namespaces"`
Filepaths []string `yaml:"filepaths" json:"filepaths,omitempty" title:"Filepaths" description:"Optional list of files to scope down search"`
}
type xmlPublicIDCondition struct {
Regex string `yaml:"regex" json:"regex"`
Namespaces map[string]string `yaml:"namespaces" json:"namespaces" title:"Namespaces" description:"A map to scope down query to namespaces"`
Filepaths []string `yaml:"filepaths" json:"filepaths" title:"Filepaths" description:"Optional list of files to scope down search"`
}
type jsonCondition struct {
XPath string `yaml:"xpath" json:"xpath" title:"XPath" description:"Xpath query"`
Filepaths []string `yaml:"filepaths" json:"filepaths,omitempty" title:"Filepaths" description:"Optional list of files to scope down search"`
}
type builtinProvider struct {
ctx context.Context
log logr.Logger
config provider.Config
tags map[string]bool
provider.UnimplementedDependenciesComponent
clients []provider.ServiceClient
}
func NewBuiltinProvider(config provider.Config, log logr.Logger) *builtinProvider {
return &builtinProvider{
config: config,
log: log,
}
}
func (p *builtinProvider) Capabilities() []provider.Capability {
r := openapi3.NewReflector()
caps := []provider.Capability{}
jsonCap, err := provider.ToProviderCap(r, p.log, jsonCondition{}, "json")
if err != nil {
p.log.Error(err, "unable to get json capability")
} else {
caps = append(caps, jsonCap)
}
xmlCap, err := provider.ToProviderCap(r, p.log, xmlCondition{}, "xml")
if err != nil {
p.log.Error(err, "unable to get xml capability")
} else {
caps = append(caps, xmlCap)
}
filecontentCap, err := provider.ToProviderCap(r, p.log, fileContentCondition{}, "filecontent")
if err != nil {
p.log.Error(err, "unable to get filecontent capability")
} else {
caps = append(caps, filecontentCap)
}
fileCap, err := provider.ToProviderInputOutputCap(r, p.log, fileCondition{}, fileTemplateContext{}, "file")
if err != nil {
p.log.Error(err, "unable to get file capability")
} else {
caps = append(caps, fileCap)
}
xmlPublicIDCap, err := provider.ToProviderCap(r, p.log, xmlPublicIDCondition{}, "xmlPublicID")
if err != nil {
p.log.Error(err, "unable to get xmlPublicID capability")
} else {
caps = append(caps, xmlPublicIDCap)
}
hasTags, err := provider.ToProviderCap(r, p.log, []string{}, "hasTags")
if err != nil {
p.log.Error(err, "unable to get hasTags capability")
} else {
caps = append(caps, hasTags)
}
return caps
}
func (p *builtinProvider) ProviderInit(ctx context.Context) error {
// First load all the tags for all init configs.
for _, c := range p.config.InitConfig {
p.loadTags(c)
}
for _, c := range p.config.InitConfig {
client, err := p.Init(ctx, p.log, c)
if err != nil {
return nil
}
p.clients = append(p.clients, client)
}
return nil
}
// We don't need to init anything
func (p *builtinProvider) Init(ctx context.Context, log logr.Logger, config provider.InitConfig) (provider.ServiceClient, error) {
if config.AnalysisMode != provider.AnalysisMode("") {
p.log.V(5).Info("skipping analysis mode setting for builtin")
}
return &builtinServiceClient{
config: config,
tags: p.tags,
UnimplementedDependenciesComponent: provider.UnimplementedDependenciesComponent{},
locationCache: make(map[string]float64),
log: log,
}, nil
}
func (p *builtinProvider) loadTags(config provider.InitConfig) error {
tagsFile, ok := config.ProviderSpecificConfig[TAGS_FILE_INIT_OPTION].(string)
// for now, if the tags file is invalid, lets ignore
if !ok {
return nil
}
p.tags = make(map[string]bool)
if tagsFile == "" {
return nil
}
content, err := os.ReadFile(tagsFile)
if err != nil {
return err
}
var tags []string
err = yaml.Unmarshal(content, &tags)
if err != nil {
return err
}
for _, tag := range tags {
p.tags[tag] = true
}
return nil
}
func (p *builtinProvider) Evaluate(ctx context.Context, cap string, conditionInfo []byte) (provider.ProviderEvaluateResponse, error) {
return provider.FullResponseFromServiceClients(ctx, p.clients, cap, conditionInfo)
}
func (p *builtinProvider) Stop() {
return
}