github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/debug/info.go (about)

     1  // Copyright 2019 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 debug exports debug information for gopls.
     6  package debug
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"io"
    12  	"strings"
    13  
    14  	"github.com/april1989/origin-go-tools/internal/gocommand"
    15  )
    16  
    17  type PrintMode int
    18  
    19  const (
    20  	PlainText = PrintMode(iota)
    21  	Markdown
    22  	HTML
    23  )
    24  
    25  // Version is a manually-updated mechanism for tracking versions.
    26  var Version = "master"
    27  
    28  // PrintServerInfo writes HTML debug info to w for the Instance.
    29  func (i *Instance) PrintServerInfo(ctx context.Context, w io.Writer) {
    30  	section(w, HTML, "Server Instance", func() {
    31  		fmt.Fprintf(w, "Start time: %v\n", i.StartTime)
    32  		fmt.Fprintf(w, "LogFile: %s\n", i.Logfile)
    33  		fmt.Fprintf(w, "Working directory: %s\n", i.Workdir)
    34  		fmt.Fprintf(w, "Address: %s\n", i.ServerAddress)
    35  		fmt.Fprintf(w, "Debug address: %s\n", i.DebugAddress)
    36  	})
    37  	PrintVersionInfo(ctx, w, true, HTML)
    38  }
    39  
    40  // PrintVersionInfo writes version information to w, using the output format
    41  // specified by mode. verbose controls whether additional information is
    42  // written, including section headers.
    43  func PrintVersionInfo(ctx context.Context, w io.Writer, verbose bool, mode PrintMode) {
    44  	if !verbose {
    45  		printBuildInfo(w, false, mode)
    46  		return
    47  	}
    48  	section(w, mode, "Build info", func() {
    49  		printBuildInfo(w, true, mode)
    50  	})
    51  	fmt.Fprint(w, "\n")
    52  	section(w, mode, "Go info", func() {
    53  		gocmdRunner := &gocommand.Runner{}
    54  		version, err := gocmdRunner.Run(ctx, gocommand.Invocation{
    55  			Verb: "version",
    56  		})
    57  		if err != nil {
    58  			panic(err)
    59  		}
    60  		fmt.Fprintln(w, version.String())
    61  	})
    62  }
    63  
    64  func section(w io.Writer, mode PrintMode, title string, body func()) {
    65  	switch mode {
    66  	case PlainText:
    67  		fmt.Fprintln(w, title)
    68  		fmt.Fprintln(w, strings.Repeat("-", len(title)))
    69  		body()
    70  	case Markdown:
    71  		fmt.Fprintf(w, "#### %s\n\n```\n", title)
    72  		body()
    73  		fmt.Fprintf(w, "```\n")
    74  	case HTML:
    75  		fmt.Fprintf(w, "<h3>%s</h3>\n<pre>\n", title)
    76  		body()
    77  		fmt.Fprint(w, "</pre>\n")
    78  	}
    79  }