-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextractor.go
More file actions
149 lines (120 loc) · 3.62 KB
/
extractor.go
File metadata and controls
149 lines (120 loc) · 3.62 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
package managers
import (
"encoding/json"
"fmt"
"path/filepath"
"regexp"
"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)
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 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)
}