Files
x/internal/cli/info.go
T
sneak 4d45aa4ca3 Restore config loading, keep exported Version, keep cgo off
Bring back the in-progress viper and go-homedir configuration loading
from the original root command as a persistent pre-run on the root
command, with a --config flag and a config line in sutil info. Restore
the exported main.Version with a nolint directive instead of renaming
it. Export CGO_ENABLED=0 from the Makefile, drop -race from make test,
and remove the C toolchain from the Docker builder stage.
2026-10-31 10:11:47 -07:00

65 lines
1.5 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")
// noConfigFile is what `sutil info` prints when no config file was loaded.
const noConfigFile = "(none)"
// newInfoCommand returns `sutil info`, which prints the build version, the
// Go toolchain version, the module path, the configuration file in use,
// and the VCS metadata recorded by the Go toolchain at build time.
func newInfoCommand(a *app) *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(), a.params.Version, a.configFileUsed(),
)
},
}
}
// writeBuildInfo writes one "key: value" line per fact to w.
func writeBuildInfo(w io.Writer, version, configFile string) error {
info, ok := debug.ReadBuildInfo()
if !ok {
return ErrNoBuildInfo
}
if configFile == "" {
configFile = noConfigFile
}
lines := []string{
"version: " + version,
"go: " + info.GoVersion,
"module: " + info.Main.Path,
"config: " + configFile,
}
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
}