Files
x/internal/cli/info.go
T

55 lines
1.3 KiB
Go

package cli
import (
"errors"
"fmt"
"io"
"runtime/debug"
"strings"
"github.com/spf13/cobra"
)
// ErrNoBuildInfo is returned by `sutil info` when the binary carries no
// Go build information, which only happens for binaries not built with
// module support.
var ErrNoBuildInfo = errors.New("sutil: no build information in this binary")
// newInfoCommand returns `sutil info`, which prints the build version, the
// Go toolchain version, the module path, and the VCS metadata recorded by
// the Go toolchain at build time.
func newInfoCommand(p Params) *cobra.Command {
return &cobra.Command{
Use: "info",
Short: "Print build information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return writeBuildInfo(cmd.OutOrStdout(), p.Version)
},
}
}
// writeBuildInfo writes one "key: value" line per fact to w.
func writeBuildInfo(w io.Writer, version string) error {
info, ok := debug.ReadBuildInfo()
if !ok {
return ErrNoBuildInfo
}
lines := []string{
"version: " + version,
"go: " + info.GoVersion,
"module: " + info.Main.Path,
}
for _, setting := range info.Settings {
if strings.HasPrefix(setting.Key, "vcs.") {
lines = append(lines, setting.Key+": "+setting.Value)
}
}
_, err := fmt.Fprintln(w, strings.Join(lines, "\n"))
return err
}