-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetector.go
More file actions
160 lines (132 loc) · 3.44 KB
/
detector.go
File metadata and controls
160 lines (132 loc) · 3.44 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
package managers
import (
"os"
"os/exec"
"regexp"
"sort"
"strings"
"github.com/git-pkgs/managers/definitions"
)
type DetectOptions struct {
RequireCLI bool
OnConflict ConflictBehavior
SearchParents bool
Manager string
}
type ConflictBehavior int
const (
ConflictError ConflictBehavior = iota
ConflictUseFirst
ConflictUseNewest
)
type Detector struct {
definitions []*definitions.Definition
translator *Translator
runner Runner
}
func NewDetector(translator *Translator, runner Runner) *Detector {
return &Detector{
translator: translator,
runner: runner,
}
}
func (d *Detector) Register(def *definitions.Definition) {
d.definitions = append(d.definitions, def)
d.translator.Register(def)
d.sortDefinitions()
}
func (d *Detector) sortDefinitions() {
sort.Slice(d.definitions, func(i, j int) bool {
return d.definitions[i].Detection.Priority > d.definitions[j].Detection.Priority
})
}
func (d *Detector) Detect(dir string, opts DetectOptions) (Manager, error) { //nolint:ireturn
if opts.Manager != "" {
return d.detectExplicit(dir, opts.Manager, opts.RequireCLI)
}
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
fileSet := make(map[string]bool)
for _, f := range files {
fileSet[f.Name()] = true
}
var lockfileMatches []*definitions.Definition
var lockfileNames []string
for _, def := range d.definitions {
for _, lockfile := range def.Detection.Lockfiles {
if fileSet[lockfile] {
lockfileMatches = append(lockfileMatches, def)
lockfileNames = append(lockfileNames, lockfile)
}
}
}
if len(lockfileMatches) > 1 && opts.OnConflict == ConflictError {
return nil, ErrConflictingLockfiles{
Dir: dir,
Lockfiles: lockfileNames,
}
}
if len(lockfileMatches) >= 1 {
def := lockfileMatches[0]
return d.buildManager(def, dir, lockfileNames[:1], opts.RequireCLI)
}
for _, def := range d.definitions {
for _, manifest := range def.Detection.Manifests {
if fileSet[manifest] {
return d.buildManager(def, dir, []string{manifest}, opts.RequireCLI)
}
}
}
return nil, ErrNoManifest{Dir: dir}
}
func (d *Detector) detectExplicit(dir, managerName string, requireCLI bool) (Manager, error) { //nolint:ireturn
for _, def := range d.definitions {
if def.Name == managerName {
return d.buildManager(def, dir, nil, requireCLI)
}
}
return nil, ErrNoManifest{Dir: dir}
}
func (d *Detector) buildManager(def *definitions.Definition, dir string, files []string, requireCLI bool) (Manager, error) { //nolint:ireturn
if requireCLI {
if _, err := exec.LookPath(def.Binary); err != nil {
return nil, ErrCLINotFound{
Manager: def.Name,
Binary: def.Binary,
Files: files,
}
}
}
return &GenericManager{
def: def,
dir: dir,
translator: d.translator,
runner: d.runner,
}, nil
}
func (d *Detector) DetectVersion(def *definitions.Definition) (string, error) {
if len(def.VersionDetection.Command) == 0 {
return "", nil
}
binary := def.Binary
args := def.VersionDetection.Command
cmd := exec.Command(binary, args...)
output, err := cmd.Output()
if err != nil {
return "", err
}
if def.VersionDetection.Pattern == "" {
return strings.TrimSpace(string(output)), nil
}
re, err := regexp.Compile(def.VersionDetection.Pattern)
if err != nil {
return "", err
}
matches := re.FindStringSubmatch(string(output))
if len(matches) > 1 {
return matches[1], nil
}
return strings.TrimSpace(string(output)), nil
}