blob: e8bae36b4181b0eb4de881eb79cfcf0dcf0d969d [file] [log] [blame]
Ian Cottrell44dd5712019-05-03 14:31:03 -04001// 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// +build go1.12
6
Ian Cottrell08e0b302019-05-29 19:58:27 -04007package debug
Ian Cottrell44dd5712019-05-03 14:31:03 -04008
9import (
10 "fmt"
11 "io"
12 "runtime/debug"
13)
14
Ian Cottrell08e0b302019-05-29 19:58:27 -040015func printBuildInfo(w io.Writer, verbose bool, mode PrintMode) {
Ian Cottrell44dd5712019-05-03 14:31:03 -040016 if info, ok := debug.ReadBuildInfo(); ok {
Rebecca Stamblera99d5a72019-06-07 15:55:45 -040017 fmt.Fprintf(w, "%v %v\n", info.Path, Version)
Ian Cottrell08e0b302019-05-29 19:58:27 -040018 printModuleInfo(w, &info.Main, mode)
Ian Cottrell44dd5712019-05-03 14:31:03 -040019 if verbose {
20 for _, dep := range info.Deps {
Ian Cottrell08e0b302019-05-29 19:58:27 -040021 printModuleInfo(w, dep, mode)
Ian Cottrell44dd5712019-05-03 14:31:03 -040022 }
23 }
24 } else {
Rebecca Stambler7db4ac62019-06-12 12:38:51 -040025 fmt.Fprintf(w, "version %s, built in $GOPATH mode\n", Version)
Ian Cottrell44dd5712019-05-03 14:31:03 -040026 }
27}
28
Ian Cottrell08e0b302019-05-29 19:58:27 -040029func printModuleInfo(w io.Writer, m *debug.Module, mode PrintMode) {
Ian Cottrell44dd5712019-05-03 14:31:03 -040030 fmt.Fprintf(w, " %s@%s", m.Path, m.Version)
31 if m.Sum != "" {
32 fmt.Fprintf(w, " %s", m.Sum)
33 }
34 if m.Replace != nil {
35 fmt.Fprintf(w, " => %v", m.Replace.Path)
36 }
37 fmt.Fprintf(w, "\n")
38}