| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cmd |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"errors" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"log" |
| 14 |
"os" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/lsprpc" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 18 |
) |
| 19 |
|
| 20 |
type remote struct { |
| 21 |
app *Application |
| 22 |
subcommands |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
alias string |
| 30 |
} |
| 31 |
|
| 32 |
func newRemote(app *Application, alias string) *remote { |
| 33 |
return &remote{ |
| 34 |
app: app, |
| 35 |
subcommands: subcommands{ |
| 36 |
&listSessions{app: app}, |
| 37 |
&startDebugging{app: app}, |
| 38 |
}, |
| 39 |
alias: alias, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (r *remote) Name() string { |
| 44 |
if r.alias != "" { |
| 45 |
return r.alias |
| 46 |
} |
| 47 |
return "remote" |
| 48 |
} |
| 49 |
|
| 50 |
func (r *remote) Parent() string { return r.app.Name() } |
| 51 |
|
| 52 |
func (r *remote) ShortHelp() string { |
| 53 |
short := "interact with the gopls daemon" |
| 54 |
if r.alias != "" { |
| 55 |
short += " (deprecated: use 'remote')" |
| 56 |
} |
| 57 |
return short |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
type listSessions struct { |
| 62 |
app *Application |
| 63 |
} |
| 64 |
|
| 65 |
func (c *listSessions) Name() string { return "sessions" } |
| 66 |
func (c *listSessions) Parent() string { return c.app.Name() } |
| 67 |
func (c *listSessions) Usage() string { return "" } |
| 68 |
func (c *listSessions) ShortHelp() string { |
| 69 |
return "print information about current gopls sessions" |
| 70 |
} |
| 71 |
|
| 72 |
const listSessionsExamples = ` |
| 73 |
Examples: |
| 74 |
|
| 75 |
1) list sessions for the default daemon: |
| 76 |
|
| 77 |
$ gopls -remote=auto remote sessions |
| 78 |
or just |
| 79 |
$ gopls remote sessions |
| 80 |
|
| 81 |
2) list sessions for a specific daemon: |
| 82 |
|
| 83 |
$ gopls -remote=localhost:8082 remote sessions |
| 84 |
` |
| 85 |
|
| 86 |
func (c *listSessions) DetailedHelp(f *flag.FlagSet) { |
| 87 |
fmt.Fprint(f.Output(), listSessionsExamples) |
| 88 |
printFlagDefaults(f) |
| 89 |
} |
| 90 |
|
| 91 |
func (c *listSessions) Run(ctx context.Context, args ...string) error { |
| 92 |
remote := c.app.Remote |
| 93 |
if remote == "" { |
| 94 |
remote = "auto" |
| 95 |
} |
| 96 |
state, err := lsprpc.QueryServerState(ctx, remote) |
| 97 |
if err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
v, err := json.MarshalIndent(state, "", "\t") |
| 101 |
if err != nil { |
| 102 |
log.Fatal(err) |
| 103 |
} |
| 104 |
os.Stdout.Write(v) |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
type startDebugging struct { |
| 109 |
app *Application |
| 110 |
} |
| 111 |
|
| 112 |
func (c *startDebugging) Name() string { return "debug" } |
| 113 |
func (c *startDebugging) Usage() string { return "[host:port]" } |
| 114 |
func (c *startDebugging) ShortHelp() string { |
| 115 |
return "start the debug server" |
| 116 |
} |
| 117 |
|
| 118 |
const startDebuggingExamples = ` |
| 119 |
Examples: |
| 120 |
|
| 121 |
1) start a debug server for the default daemon, on an arbitrary port: |
| 122 |
|
| 123 |
$ gopls -remote=auto remote debug |
| 124 |
or just |
| 125 |
$ gopls remote debug |
| 126 |
|
| 127 |
2) start for a specific daemon, on a specific port: |
| 128 |
|
| 129 |
$ gopls -remote=localhost:8082 remote debug localhost:8083 |
| 130 |
` |
| 131 |
|
| 132 |
func (c *startDebugging) DetailedHelp(f *flag.FlagSet) { |
| 133 |
fmt.Fprint(f.Output(), startDebuggingExamples) |
| 134 |
printFlagDefaults(f) |
| 135 |
} |
| 136 |
|
| 137 |
func (c *startDebugging) Run(ctx context.Context, args ...string) error { |
| 138 |
if len(args) > 1 { |
| 139 |
fmt.Fprintln(os.Stderr, c.Usage()) |
| 140 |
return errors.New("invalid usage") |
| 141 |
} |
| 142 |
remote := c.app.Remote |
| 143 |
if remote == "" { |
| 144 |
remote = "auto" |
| 145 |
} |
| 146 |
debugAddr := "" |
| 147 |
if len(args) > 0 { |
| 148 |
debugAddr = args[0] |
| 149 |
} |
| 150 |
debugArgs := command.DebuggingArgs{ |
| 151 |
Addr: debugAddr, |
| 152 |
} |
| 153 |
var result command.DebuggingResult |
| 154 |
if err := lsprpc.ExecuteCommand(ctx, remote, command.StartDebugging.String(), debugArgs, &result); err != nil { |
| 155 |
return err |
| 156 |
} |
| 157 |
if len(result.URLs) == 0 { |
| 158 |
return errors.New("no debugging URLs") |
| 159 |
} |
| 160 |
for _, url := range result.URLs { |
| 161 |
fmt.Printf("debugging on %s\n", url) |
| 162 |
} |
| 163 |
return nil |
| 164 |
} |