github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/lintcmd/version/buildinfo.go (about)

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  	"runtime/debug"
     6  )
     7  
     8  func printBuildInfo() {
     9  	if info, ok := debug.ReadBuildInfo(); ok {
    10  		fmt.Println("Main module:")
    11  		printModule(&info.Main)
    12  		fmt.Println("Dependencies:")
    13  		for _, dep := range info.Deps {
    14  			printModule(dep)
    15  		}
    16  	} else {
    17  		fmt.Println("Built without Go modules")
    18  	}
    19  }
    20  
    21  func buildInfoVersion() (string, bool) {
    22  	info, ok := debug.ReadBuildInfo()
    23  	if !ok {
    24  		return "", false
    25  	}
    26  	if info.Main.Version == "(devel)" {
    27  		return "", false
    28  	}
    29  	return info.Main.Version, true
    30  }
    31  
    32  func printModule(m *debug.Module) {
    33  	fmt.Printf("\t%s", m.Path)
    34  	if m.Version != "(devel)" {
    35  		fmt.Printf("@%s", m.Version)
    36  	}
    37  	if m.Sum != "" {
    38  		fmt.Printf(" (sum: %s)", m.Sum)
    39  	}
    40  	if m.Replace != nil {
    41  		fmt.Printf(" (replace: %s)", m.Replace.Path)
    42  	}
    43  	fmt.Println()
    44  }