-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathmodule_callers.go
57 lines (47 loc) · 1.29 KB
/
module_callers.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
53
54
55
56
57
package command
import (
"context"
"fmt"
"sort"
"github.com/creachadair/jrpc2/code"
"github.com/hashicorp/terraform-ls/internal/langserver/cmd"
"github.com/hashicorp/terraform-ls/internal/uri"
)
const moduleCallersVersion = 0
type moduleCallersResponse struct {
FormatVersion int `json:"v"`
Callers []moduleCaller `json:"callers"`
}
type moduleCaller struct {
URI string `json:"uri"`
}
func (h *CmdHandler) ModuleCallersHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) {
modUri, ok := args.GetString("uri")
if !ok || modUri == "" {
return nil, fmt.Errorf("%w: expected module uri argument to be set", code.InvalidParams.Err())
}
if !uri.IsURIValid(modUri) {
return nil, fmt.Errorf("URI %q is not valid", modUri)
}
modPath, err := uri.PathFromURI(modUri)
if err != nil {
return nil, err
}
modCallers, err := h.StateStore.Modules.CallersOfModule(modPath)
if err != nil {
return nil, err
}
callers := make([]moduleCaller, 0)
for _, caller := range modCallers {
callers = append(callers, moduleCaller{
URI: uri.FromPath(caller.Path),
})
}
sort.SliceStable(callers, func(i, j int) bool {
return callers[i].URI < callers[j].URI
})
return moduleCallersResponse{
FormatVersion: moduleCallersVersion,
Callers: callers,
}, nil
}