Skip to content

Commit

Permalink
feat: better support for Go-Module aware binaries
Browse files Browse the repository at this point in the history
fix: tons.
  • Loading branch information
christophberger committed Feb 4, 2022
1 parent 93a1e61 commit 94ff0eb
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 117 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ In its current state, `goman` is little more than a proof of concept. Bugs certa

## Changelog

### v0.2.3

Large update to implement a simpler and more reliable method of getting the readme path for binaries built with Module support.

TODOs:
- Support vanity module paths
- Determine semver tag, in order to fetch the readme version that matches with the compiled version

### v0.2.2 (2021-04-23)

- Add goreleaser.yml
- Various fixes
### v0.2.1 (2017-07-04)

Fix stripping prefix from absolute path on Windows (PR #5)
Expand Down
12 changes: 8 additions & 4 deletions dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,17 @@ func getTable(file string) (*gosym.Table, error) {
return t, nil
}

func getMainPath(file string) (string, error) {
func getMainPathDwarf(file string) (string, string, error) {
table, err := getTable(file)
if err != nil {
return "", errors.Wrap(err, "main path not found")
return "", "", errors.Wrap(err, "main path not found (getTable)")
}
path, _, _ := table.PCToLine(table.LookupFunc("main.main").Entry)
return stripPath(filepath.Dir(path)), nil
gosymFunc := table.LookupFunc("main.main")
if gosymFunc == nil {
return "", "", errors.Wrap(err, "main path not found (LookupFunc)")
}
path, _, _ := table.PCToLine(gosymFunc.Entry)
return stripPath(filepath.Dir(path)), "", nil
}

// strip path strips the GOPATH prefix from the raw source code path
Expand Down
29 changes: 17 additions & 12 deletions dwarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@ import (
"testing"
)

func Test_getMainPath(t *testing.T) {
func Test_getMainPathDwarf(t *testing.T) {
type args struct {
file string
}
tests := []struct {
name string
args args
want string
name string
args args
want struct {
path, ver string
}
wantErr bool
}{
{"ELF", args{"testdata/goman_linux"}, pwd(), false},
{"Mach-O", args{"testdata/goman_macos"}, pwd(), false},
{"PE", args{"testdata/goman.exe"}, pwd(), false},
{"ELF", args{"testdata/goman_linux"}, struct{ path, ver string }{path: pwd(), ver: ""}, false},
{"Mach-O", args{"testdata/goman_macos"}, struct{ path, ver string }{path: pwd(), ver: ""}, false},
{"PE", args{"testdata/goman.exe"}, struct{ path, ver string }{path: pwd(), ver: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getMainPath(tt.args.file)
path, ver, err := getMainPathDwarf(tt.args.file)
if (err != nil) != tt.wantErr {
t.Errorf("getMainPath() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("getMainPathDwarf() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getMainPath() = %v, want %v", got, tt.want)
if path != tt.want.path {
t.Errorf("getMainPathDwarf() path = %v, want %v", path, tt.want.path)
}
if ver != tt.want.ver {
t.Errorf("getMainPathDwarf() ver = %v, want %v", ver, tt.want.ver)
}
})
}
Expand All @@ -37,4 +42,4 @@ func Test_getMainPath(t *testing.T) {
func pwd() string {
wd, _ := os.Getwd()
return wd
}
}
Loading

0 comments on commit 94ff0eb

Please sign in to comment.