github.com/jd-ly/tools@v0.5.7/internal/lsp/cmd/inspect.go (about)

     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.
     4  
     5  package cmd
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"flag"
    11  	"fmt"
    12  	"log"
    13  	"os"
    14  
    15  	"github.com/jd-ly/tools/internal/lsp/lsprpc"
    16  	"github.com/jd-ly/tools/internal/tool"
    17  )
    18  
    19  type inspect struct {
    20  	app *Application
    21  }
    22  
    23  func (i *inspect) subCommands() []tool.Application {
    24  	return []tool.Application{
    25  		&listSessions{app: i.app},
    26  	}
    27  }
    28  
    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)"
    33  }
    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())
    38  	}
    39  	f.PrintDefaults()
    40  }
    41  
    42  func (i *inspect) Run(ctx context.Context, args ...string) error {
    43  	if len(args) == 0 {
    44  		return tool.CommandLineErrorf("must provide subcommand to %q", i.Name())
    45  	}
    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)
    50  		}
    51  	}
    52  	return tool.CommandLineErrorf("unknown command %v", command)
    53  }
    54  
    55  // listSessions is an inspect subcommand to list current sessions.
    56  type listSessions struct {
    57  	app *Application
    58  }
    59  
    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"
    64  }
    65  
    66  const listSessionsExamples = `
    67  Examples:
    68  
    69  1) list sessions for the default daemon:
    70  
    71  $ gopls -remote=auto inspect sessions
    72  or just
    73  $ gopls inspect sessions
    74  
    75  2) list sessions for a specific daemon:
    76  
    77  $ gopls -remote=localhost:8082 inspect sessions
    78  `
    79  
    80  func (c *listSessions) DetailedHelp(f *flag.FlagSet) {
    81  	fmt.Fprint(f.Output(), listSessionsExamples)
    82  	f.PrintDefaults()
    83  }
    84  
    85  func (c *listSessions) Run(ctx context.Context, args ...string) error {
    86  	remote := c.app.Remote
    87  	if remote == "" {
    88  		remote = "auto"
    89  	}
    90  	network, address := parseAddr(remote)
    91  	state, err := lsprpc.QueryServerState(ctx, network, address)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	v, err := json.MarshalIndent(state, "", "\t")
    96  	if err != nil {
    97  		log.Fatal(err)
    98  	}
    99  	os.Stdout.Write(v)
   100  	return nil
   101  }