forked from sourcegraph/srclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
choose.go
52 lines (46 loc) · 1.53 KB
/
choose.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
package toolchain
import (
"fmt"
"sourcegraph.com/sourcegraph/srclib"
)
// ChooseTool determines which toolchain and tool to use to run op (graph,
// depresolve, etc.) on a source unit of the given type. If no tools fit the
// criteria, an error is returned.
//
// The selection algorithm is currently very simplistic: if exactly one tool is
// found that can perform op on the source unit type, it is returned. If zero or
// more than 1 are found, then an error is returned. TODO(sqs): extend this to
// choose the "best" tool when multiple tools would suffice.
func ChooseTool(op, unitType string) (*srclib.ToolRef, error) {
tcs, err := List()
if err != nil {
return nil, err
}
return chooseTool(op, unitType, tcs)
}
// chooseTool is like ChooseTool but the list of tools is provided as an
// argument instead of being obtained by calling List.
func chooseTool(op, unitType string, tcs []*Info) (*srclib.ToolRef, error) {
var satisfying []*srclib.ToolRef
for _, tc := range tcs {
cfg, err := tc.ReadConfig()
if err != nil {
return nil, err
}
for _, tool := range cfg.Tools {
if tool.Op == op {
for _, u := range tool.SourceUnitTypes {
if u == unitType {
satisfying = append(satisfying, &srclib.ToolRef{Toolchain: tc.Path, Subcmd: tool.Subcmd})
}
}
}
}
}
if n := len(satisfying); n > 1 {
return nil, fmt.Errorf("%d tools satisfy op %q for source unit type %q (refusing to choose between multiple possibilities)", n, op, unitType)
} else if n == 0 {
return nil, nil
}
return satisfying[0], nil
}