Debian Codegraph @

gopls/internal/cmd/execute.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package cmd
6
7 import (
8 "context"
9 "encoding/json"
10 "flag"
11 "fmt"
12 "log"
13 "os"
14 "slices"
15
16 "golang.org/x/tools/gopls/internal/filecache"
17 "golang.org/x/tools/gopls/internal/protocol"
18 "golang.org/x/tools/gopls/internal/protocol/command"
19 "golang.org/x/tools/gopls/internal/tool"
20 "golang.org/x/tools/gopls/internal/util/bug"
21 )
22
23 // execute implements the LSP ExecuteCommand verb for gopls.
24 type execute struct {
25 EditFlags
26 app *Application
27 }
28
29 func (e *execute) Name() string { return "execute" }
30 func (e *execute) Parent() string { return e.app.Name() }
31 func (e *execute) Usage() string { return "[flags] command argument..." }
32 func (e *execute) ShortHelp() string { return "Execute a gopls custom LSP command" }
33 func (e *execute) DetailedHelp(f *flag.FlagSet) {
34 fmt.Fprint(f.Output(), `
35 The execute command sends an LSP ExecuteCommand request to gopls,
36 with a set of optional JSON argument values.
37 Some commands return a result, also JSON.
38
39 Gopls' command set is defined by the command.Interface type; see
40 https://pkg.go.dev/golang.org/x/tools/gopls/internal/protocol/command#Interface.
41 It is not a stable interface: commands may change or disappear without notice.
42
43 Examples:
44
45 $ gopls execute gopls.add_import '{"ImportPath": "fmt", "URI": "file:///hello.go"}'
46 $ gopls execute gopls.run_tests '{"URI": "file:///a_test.go", "Tests": ["Test"]}'
47 $ gopls execute gopls.list_known_packages '{"URI": "file:///hello.go"}'
48
49 execute-flags:
50 `)
51 printFlagDefaults(f)
52 }
53
54 func (e *execute) Run(ctx context.Context, args ...string) error {
55 // This undocumented environment variable allows
56 // the cmd integration test (and maintainers) to
57 // trigger a call to bug.Report.
58 if msg := os.Getenv("TEST_GOPLS_BUG"); msg != "" {
59 filecache.Start() // register bug handler
60 bug.Report(msg)
61 return nil
62 }
63
64 if len(args) == 0 {
65 return tool.CommandLineErrorf("execute requires a command name")
66 }
67 cmd := args[0]
68 if !slices.Contains(command.Commands, command.Command(cmd)) {
69 return tool.CommandLineErrorf("unrecognized command: %s", cmd)
70 }
71
72 // A command may have multiple arguments, though the only one
73 // that currently does so is the "legacy" gopls.test,
74 // so we don't show an example of it.
75 var jsonArgs []json.RawMessage
76 for i, arg := range args[1:] {
77 var dummy any
78 if err := json.Unmarshal([]byte(arg), &dummy); err != nil {
79 return fmt.Errorf("argument %d is not valid JSON: %v", i+1, err)
80 }
81 jsonArgs = append(jsonArgs, json.RawMessage(arg))
82 }
83
84 e.app.editFlags = &e.EditFlags // in case command performs an edit
85
86 cli, _, err := e.app.connect(ctx)
87 if err != nil {
88 return err
89 }
90 defer cli.terminate(ctx)
91
92 res, err := executeCommand(ctx, cli.server, &protocol.Command{
93 Command: cmd,
94 Arguments: jsonArgs,
95 })
96 if err != nil {
97 return err
98 }
99 if res != nil {
100 data, err := json.MarshalIndent(res, "", "\t")
101 if err != nil {
102 log.Fatal(err)
103 }
104 fmt.Printf("%s\n", data)
105 }
106 return nil
107 }
108
109 // executeCommand executes a protocol.Command, displaying progress
110 // messages and awaiting completion of asynchronous commands.
111 func executeCommand(ctx context.Context, server protocol.Server, cmd *protocol.Command) (any, error) {
112 return server.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
113 Command: cmd.Command,
114 Arguments: cmd.Arguments,
115 })
116 }