1 // Copyright 2020 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.
15 "golang.org/x/tools/internal/lsp/lsprpc"
16 "golang.org/x/tools/internal/tool"
23 func (i *inspect) subCommands() []tool.Application {
24 return []tool.Application{
25 &listSessions{app: i.app},
29 func (i *inspect) Name() string { return "inspect" }
30 func (i *inspect) Usage() string { return "<subcommand> [args...]" }
31 func (i *inspect) ShortHelp() string {
32 return "inspect server state (daemon mode only)"
34 func (i *inspect) DetailedHelp(f *flag.FlagSet) {
35 fmt.Fprint(f.Output(), "\nsubcommands:\n")
36 for _, c := range i.subCommands() {
37 fmt.Fprintf(f.Output(), " %s: %s\n", c.Name(), c.ShortHelp())
42 func (i *inspect) Run(ctx context.Context, args ...string) error {
44 return tool.CommandLineErrorf("must provide subcommand to %q", i.Name())
46 command, args := args[0], args[1:]
47 for _, c := range i.subCommands() {
48 if c.Name() == command {
49 return tool.Run(ctx, c, args)
52 return tool.CommandLineErrorf("unknown command %v", command)
55 // listSessions is an inspect subcommand to list current sessions.
56 type listSessions struct {
60 func (c *listSessions) Name() string { return "sessions" }
61 func (c *listSessions) Usage() string { return "" }
62 func (c *listSessions) ShortHelp() string {
63 return "print information about current gopls sessions"
66 const listSessionsExamples = `
69 1) list sessions for the default daemon:
71 $ gopls -remote=auto inspect sessions
73 $ gopls inspect sessions
75 2) list sessions for a specific daemon:
77 $ gopls -remote=localhost:8082 inspect sessions
80 func (c *listSessions) DetailedHelp(f *flag.FlagSet) {
81 fmt.Fprint(f.Output(), listSessionsExamples)
85 func (c *listSessions) Run(ctx context.Context, args ...string) error {
86 remote := c.app.Remote
90 network, address := parseAddr(remote)
91 state, err := lsprpc.QueryServerState(ctx, network, address)
95 v, err := json.MarshalIndent(state, "", "\t")