Skip to content

Commit

Permalink
Add the executable path to VersionInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Feb 12, 2024
1 parent 9f712f2 commit b948f0a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ type VersionInfo struct {
// Name is the base name of the running binary from os.Args.
Name string `json:"name"`

// Path is the import path of the main package.
Path string `json:"path"`
// BinaryPath is the path of the program binary as reported by os.Executable.
BinaryPath string `json:"binaryPath,omitempty"`

// ImportPath is the Go import path of the main package.
ImportPath string `json:"importPath"`

// Version, if available, is the version tag at which the binary was built.
// This is empty if no version label is available, e.g. an untagged commit.
Expand Down Expand Up @@ -83,9 +86,12 @@ func GetVersionInfo() VersionInfo {
return VersionInfo{Name: filepath.Base(os.Args[0])}
}
vi := VersionInfo{
Name: filepath.Base(os.Args[0]),
Path: bi.Path,
Toolchain: bi.GoVersion,
Name: filepath.Base(os.Args[0]),
ImportPath: bi.Path,
Toolchain: bi.GoVersion,
}
if p, err := os.Executable(); err == nil {
vi.BinaryPath = p
}
vi.parseModule(&bi.Main)

Expand Down Expand Up @@ -120,8 +126,8 @@ func (v VersionInfo) String() string {
if v.Version != "" {
fmt.Fprint(&sb, " version ", v.Version)
}
if v.Path != "" {
fmt.Fprint(&sb, " path ", v.Path)
if v.ImportPath != "" {
fmt.Fprint(&sb, " path ", v.ImportPath)
}
if v.Commit != "" {
fmt.Fprint(&sb, " commit ", v.Commit)
Expand Down

0 comments on commit b948f0a

Please sign in to comment.