| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package debug |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"os" |
| 14 |
"runtime" |
| 15 |
"runtime/debug" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"golang.org/x/tools/gopls/internal/version" |
| 19 |
) |
| 20 |
|
| 21 |
type PrintMode int |
| 22 |
|
| 23 |
const ( |
| 24 |
PlainText = PrintMode(iota) |
| 25 |
Markdown |
| 26 |
HTML |
| 27 |
JSON |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type ServerVersion struct { |
| 33 |
*debug.BuildInfo |
| 34 |
Version string |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func VersionInfo() *ServerVersion { |
| 41 |
if info, ok := debug.ReadBuildInfo(); ok { |
| 42 |
return &ServerVersion{ |
| 43 |
Version: version.Version(), |
| 44 |
BuildInfo: info, |
| 45 |
} |
| 46 |
} |
| 47 |
return &ServerVersion{ |
| 48 |
Version: version.Version(), |
| 49 |
BuildInfo: &debug.BuildInfo{ |
| 50 |
Path: "gopls, built in GOPATH mode", |
| 51 |
GoVersion: runtime.Version(), |
| 52 |
}, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func (i *Instance) writeServerInfo(out *bytes.Buffer) { |
| 58 |
workDir, _ := os.Getwd() |
| 59 |
section(out, HTML, "server instance", func() { |
| 60 |
fmt.Fprintf(out, "Start time: %v\n", i.StartTime) |
| 61 |
fmt.Fprintf(out, "LogFile: %s\n", i.Logfile) |
| 62 |
fmt.Fprintf(out, "pid: %d\n", os.Getpid()) |
| 63 |
fmt.Fprintf(out, "Working directory: %s\n", workDir) |
| 64 |
fmt.Fprintf(out, "Address: %s\n", i.ServerAddress) |
| 65 |
fmt.Fprintf(out, "Debug address: %s\n", i.DebugAddress()) |
| 66 |
}) |
| 67 |
WriteVersionInfo(out, true, HTML) |
| 68 |
section(out, HTML, "Command Line", func() { |
| 69 |
fmt.Fprintf(out, "<a href=/debug/pprof/cmdline>cmdline</a>") |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
func WriteVersionInfo(out *bytes.Buffer, verbose bool, mode PrintMode) { |
| 77 |
info := VersionInfo() |
| 78 |
if mode == JSON { |
| 79 |
writeVersionInfoJSON(out, info) |
| 80 |
return |
| 81 |
} |
| 82 |
|
| 83 |
if !verbose { |
| 84 |
writeBuildInfo(out, info, false, mode) |
| 85 |
return |
| 86 |
} |
| 87 |
section(out, mode, "Build info", func() { |
| 88 |
writeBuildInfo(out, info, true, mode) |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
func writeVersionInfoJSON(out *bytes.Buffer, info *ServerVersion) { |
| 93 |
data, err := json.MarshalIndent(info, "", "\t") |
| 94 |
if err != nil { |
| 95 |
panic(err) |
| 96 |
} |
| 97 |
out.Write(data) |
| 98 |
} |
| 99 |
|
| 100 |
func section(w io.Writer, mode PrintMode, title string, body func()) { |
| 101 |
switch mode { |
| 102 |
case PlainText: |
| 103 |
fmt.Fprintln(w, title) |
| 104 |
fmt.Fprintln(w, strings.Repeat("-", len(title))) |
| 105 |
body() |
| 106 |
case Markdown: |
| 107 |
fmt.Fprintf(w, "#### %s\n\n```\n", title) |
| 108 |
body() |
| 109 |
fmt.Fprintf(w, "```\n") |
| 110 |
case HTML: |
| 111 |
fmt.Fprintf(w, "<h3>%s</h3>\n<pre>\n", title) |
| 112 |
body() |
| 113 |
fmt.Fprint(w, "</pre>\n") |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func writeBuildInfo(w io.Writer, info *ServerVersion, verbose bool, mode PrintMode) { |
| 118 |
fmt.Fprintf(w, "%v %v\n", info.Path, version.Version()) |
| 119 |
if !verbose { |
| 120 |
return |
| 121 |
} |
| 122 |
printModuleInfo(w, info.Main, mode) |
| 123 |
for _, dep := range info.Deps { |
| 124 |
printModuleInfo(w, *dep, mode) |
| 125 |
} |
| 126 |
fmt.Fprintf(w, "go: %v\n", info.GoVersion) |
| 127 |
} |
| 128 |
|
| 129 |
func printModuleInfo(w io.Writer, m debug.Module, _ PrintMode) { |
| 130 |
fmt.Fprintf(w, " %s@%s", m.Path, m.Version) |
| 131 |
if m.Sum != "" { |
| 132 |
fmt.Fprintf(w, " %s", m.Sum) |
| 133 |
} |
| 134 |
if m.Replace != nil { |
| 135 |
fmt.Fprintf(w, " => %v", m.Replace.Path) |
| 136 |
} |
| 137 |
fmt.Fprintf(w, "\n") |
| 138 |
} |