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
5package debug
6
7import (
8	"fmt"
9	"io"
10	"os/exec"
11	"strings"
12)
13
14type PrintMode int
15
16const (
17	PlainText = PrintMode(iota)
18	Markdown
19	HTML
20)
21
22// Version is a manually-updated mechanism for tracking versions.
23var Version = "v0.1.1"
24
25// This writes the version and environment information to a writer.
26func PrintVersionInfo(w io.Writer, verbose bool, mode PrintMode) {
27	if !verbose {
28		printBuildInfo(w, false, mode)
29		return
30	}
31	section(w, mode, "Build info", func() {
32		printBuildInfo(w, true, mode)
33	})
34	fmt.Fprint(w, "\n")
35	section(w, mode, "Go info", func() {
36		cmd := exec.Command("go", "version")
37		cmd.Stdout = w
38		cmd.Run()
39		fmt.Fprint(w, "\n")
40		cmd = exec.Command("go", "env")
41		cmd.Stdout = w
42		cmd.Run()
43	})
44}
45
46func section(w io.Writer, mode PrintMode, title string, body func()) {
47	switch mode {
48	case PlainText:
49		fmt.Fprintln(w, title)
50		fmt.Fprintln(w, strings.Repeat("-", len(title)))
51		body()
52	case Markdown:
53		fmt.Fprintf(w, "#### %s\n\n```\n", title)
54		body()
55		fmt.Fprintf(w, "```\n")
56	case HTML:
57		fmt.Fprintf(w, "<h3>%s</h3>\n<pre>\n", title)
58		body()
59		fmt.Fprint(w, "</pre>\n")
60	}
61}
62