forked from sourcegraph/srclib
/
tool.go
52 lines (45 loc) · 1.46 KB
/
tool.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 "sourcegraph.com/sourcegraph/srclib"
// ToolInfo describes a tool in a toolchain.
type ToolInfo struct {
// Subcmd is the subcommand name of this tool.
//
// By convention, this is the same as Op in toolchains that only have one
// tool that performs this operation (e.g., a toolchain's "graph" subcommand
// performs the "graph" operation).
Subcmd string
// Op is the operation that this tool performs (e.g., "scan", "graph",
// "deplist", etc.).
Op string
// SourceUnitTypes is a list of source unit types (e.g., "GoPackage") that
// this tool can operate on.
//
// If this tool doesn't operate on source units (for example, it operates on
// directories or repositories, such as the "blame" tools), then this will
// be empty.
//
// TODO(sqs): determine how repository- or directory-level tools will be
// defined.
SourceUnitTypes []string `json:",omitempty"`
}
// ListTools lists all tools in all available toolchains (returned by List). If
// op is non-empty, only tools that perform that operation are returned.
func ListTools(op string) ([]*srclib.ToolRef, error) {
tcs, err := List()
if err != nil {
return nil, err
}
var tools []*srclib.ToolRef
for _, tc := range tcs {
c, err := tc.ReadConfig()
if err != nil {
return nil, err
}
for _, tool := range c.Tools {
if op == "" || tool.Op == op {
tools = append(tools, &srclib.ToolRef{Toolchain: tc.Path, Subcmd: tool.Subcmd})
}
}
}
return tools, nil
}