-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextractor.go
More file actions
206 lines (172 loc) · 4.98 KB
/
Copy pathextractor.go
File metadata and controls
206 lines (172 loc) · 4.98 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package managers
import (
"encoding/json"
"fmt"
"path/filepath"
"regexp"
"slices"
"strings"
"github.com/git-pkgs/managers/definitions"
)
func ExtractPath(output string, extract *definitions.Extract, pkg string) (string, error) {
if extract == nil || extract.Type == "" || extract.Type == "raw" {
return strings.TrimSpace(output), nil
}
var result string
var err error
switch extract.Type {
case "json":
result, err = extractJSON(output, extract.Field)
case "line_prefix":
result, err = extractLinePrefix(output, extract.Prefix)
case "regex":
result, err = extractRegex(output, extract.Pattern)
case "json_array":
result, err = extractJSONArray(output, extract.ArrayField, extract.MatchField, extract.ExtractField, pkg)
case "template":
result, err = extractTemplate(extract.Pattern, pkg)
case "python_distribution":
result, _, err = extractPythonDistribution(output)
default:
return "", fmt.Errorf("unknown extract type: %s", extract.Type)
}
if err != nil {
return "", err
}
if extract.StripFilename {
result = filepath.Dir(result)
}
return result, nil
}
func extractPathResult(output string, extract *definitions.Extract, pkg string) (*PathResult, error) {
if extract != nil && extract.Type == "python_distribution" {
path, files, err := extractPythonDistribution(output)
if err != nil {
return nil, err
}
return &PathResult{Path: path, Files: files}, nil
}
path, err := ExtractPath(output, extract, pkg)
if err != nil {
return nil, err
}
return &PathResult{Path: path}, nil
}
func extractPythonDistribution(output string) (string, []string, error) {
location, err := extractLinePrefix(output, "Location: ")
if err != nil {
return "", nil, err
}
lines := strings.Split(output, "\n")
files := make([]string, 0)
inFiles := false
for _, line := range lines {
if strings.TrimSpace(line) == "Files:" {
inFiles = true
continue
}
if !inFiles {
continue
}
file := strings.TrimSpace(line)
if file == "" {
continue
}
if !startsWithWhitespace(line) {
break
}
if !filepath.IsAbs(file) {
file = filepath.Join(location, filepath.FromSlash(file))
}
files = append(files, filepath.Clean(file))
}
slices.Sort(files)
files = slices.Compact(files)
return location, files, nil
}
func startsWithWhitespace(value string) bool {
return strings.HasPrefix(value, " ") || strings.HasPrefix(value, "\t")
}
func extractJSON(output string, field string) (string, error) {
if field == "" {
return "", fmt.Errorf("json extraction requires field name")
}
var data map[string]any
if err := json.Unmarshal([]byte(output), &data); err != nil {
return "", fmt.Errorf("failed to parse JSON: %w", err)
}
value, ok := data[field]
if !ok {
return "", fmt.Errorf("field %q not found in JSON", field)
}
str, ok := value.(string)
if !ok {
return "", fmt.Errorf("field %q is not a string", field)
}
return str, nil
}
func extractLinePrefix(output string, prefix string) (string, error) {
if prefix == "" {
return "", fmt.Errorf("line_prefix extraction requires prefix")
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.HasPrefix(line, prefix) {
return strings.TrimSpace(strings.TrimPrefix(line, prefix)), nil
}
}
return "", fmt.Errorf("no line found with prefix %q", prefix)
}
func extractRegex(output string, pattern string) (string, error) {
if pattern == "" {
return "", fmt.Errorf("regex extraction requires pattern")
}
re, err := regexp.Compile(pattern)
if err != nil {
return "", fmt.Errorf("invalid regex pattern: %w", err)
}
const minSubmatchLen = 2
matches := re.FindStringSubmatch(output)
if len(matches) < minSubmatchLen {
return "", fmt.Errorf("pattern did not match or no capture group found")
}
return strings.TrimSpace(matches[1]), nil
}
func extractTemplate(pattern, pkg string) (string, error) {
if pattern == "" {
return "", fmt.Errorf("template extraction requires pattern")
}
if pkg == "" {
return "", fmt.Errorf("template extraction requires package name")
}
return strings.ReplaceAll(pattern, "{package}", pkg), nil
}
func extractJSONArray(output, arrayField, matchField, extractField, pkg string) (string, error) {
if arrayField == "" || matchField == "" || extractField == "" {
return "", fmt.Errorf("json_array extraction requires array_field, match_field, and extract_field")
}
var data map[string]any
if err := json.Unmarshal([]byte(output), &data); err != nil {
return "", fmt.Errorf("failed to parse JSON: %w", err)
}
arr, ok := data[arrayField].([]any)
if !ok {
return "", fmt.Errorf("field %q is not an array", arrayField)
}
for _, item := range arr {
obj, ok := item.(map[string]any)
if !ok {
continue
}
name, ok := obj[matchField].(string)
if !ok || name != pkg {
continue
}
value, ok := obj[extractField].(string)
if !ok {
return "", fmt.Errorf("field %q is not a string in matched element", extractField)
}
return strings.TrimSpace(value), nil
}
return "", fmt.Errorf("no element found with %s=%q", matchField, pkg)
}