diff --git a/Documentation/usage/dlv_version.md b/Documentation/usage/dlv_version.md index a4f48420..6fec6111 100644 --- a/Documentation/usage/dlv_version.md +++ b/Documentation/usage/dlv_version.md @@ -9,7 +9,8 @@ dlv version [flags] ### Options ``` - -h, --help help for version + -h, --help help for version + -v, --verbose print verbose version info ``` ### Options inherited from parent commands diff --git a/cmd/dlv/cmds/commands.go b/cmd/dlv/cmds/commands.go index 3f2a2f1a..9bf1e5b4 100644 --- a/cmd/dlv/cmds/commands.go +++ b/cmd/dlv/cmds/commands.go @@ -308,13 +308,18 @@ Currently supports linux/amd64 and linux/arm64 core files, windows/amd64 minidum rootCommand.AddCommand(coreCommand) // 'version' subcommand. + var versionVerbose = false versionCommand := &cobra.Command{ Use: "version", Short: "Prints version.", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Delve Debugger\n%s\n", version.DelveVersion) + if versionVerbose { + fmt.Printf("Build Details: %s\n", version.BuildInfo()) + } }, } + versionCommand.Flags().BoolVarP(&versionVerbose, "verbose", "v", false, "print verbose version info") rootCommand.AddCommand(versionCommand) if path, _ := exec.LookPath("rr"); path != "" || docCall { diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index 479ac40e..63ee20ec 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -781,3 +781,18 @@ func TestDlvTestChdir(t *testing.T) { t.Errorf("output did not contain expected string %q", tgt) } } + +func TestVersion(t *testing.T) { + dlvbin, tmpdir := getDlvBin(t) + defer os.RemoveAll(tmpdir) + + got, err := exec.Command(dlvbin, "version", "-v").CombinedOutput() + if err != nil { + t.Fatalf("error executing `dlv version`: %v\n%s\n", err, got) + } + want1 := []byte("mod\tgithub.com/go-delve/delve") + want2 := []byte("dep\tgithub.com/google/go-dap") + if !bytes.Contains(got, want1) || !bytes.Contains(got, want2) { + t.Errorf("got %s\nwant %v and %v in the output", got, want1, want2) + } +} diff --git a/pkg/version/buildinfo.go b/pkg/version/buildinfo.go new file mode 100644 index 00000000..4a34bd56 --- /dev/null +++ b/pkg/version/buildinfo.go @@ -0,0 +1,32 @@ +// +build go1.12 + +package version + +import ( + "bytes" + "runtime/debug" + "text/template" +) + +func init() { + buildInfo = moduleBuildInfo +} + +var buildInfoTmpl = ` mod {{.Main.Path}} {{.Main.Version}} {{.Main.Sum}} +{{range .Deps}} dep {{.Path}} {{.Version}} {{.Sum}}{{if .Replace}} + => {{.Replace.Path}} {{.Replace.Version}} {{.Replace.Sum}}{{end}} +{{end}}` + +func moduleBuildInfo() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return "not built in module mode" + } + + buf := new(bytes.Buffer) + err := template.Must(template.New("buildinfo").Parse(buildInfoTmpl)).Execute(buf, info) + if err != nil { + panic(err) + } + return buf.String() +} diff --git a/pkg/version/version.go b/pkg/version/version.go index e353a651..03c66357 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,6 +1,9 @@ package version -import "fmt" +import ( + "fmt" + "runtime" +) // Version represents the current version of Delve. type Version struct { @@ -26,3 +29,11 @@ func (v Version) String() string { } return fmt.Sprintf("%s\nBuild: %s", ver, v.Build) } + +var buildInfo = func() string { + return "" +} + +func BuildInfo() string { + return fmt.Sprintf("%s\n%s", runtime.Version(), buildInfo()) +}