Skip to content

Commit

Permalink
Merge pull request #132 from rkscv/use-buildinfo
Browse files Browse the repository at this point in the history
Use buildinfo
  • Loading branch information
nao1215 authored Mar 23, 2024
2 parents e6be6e0 + e43a5a8 commit 7a4a982
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 112 deletions.
2 changes: 1 addition & 1 deletion cmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func Test_export(t *testing.T) {
gobin: filepath.Join("testdata", "text"),
want: 1,
stderr: []string{
"gup:WARN : can't get 'dummy.txt'package path information. old go version binary",
"gup:WARN : could not read Go build info from " + filepath.Join("testdata", "text", "dummy.txt") + ": unrecognized file format",
"gup:ERROR: no package information",
"",
},
Expand Down
24 changes: 0 additions & 24 deletions internal/goutil/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,6 @@ func ExampleGoBin() {
// Output: Example GoBin: OK
}

func ExampleGoVersionWithOptionM() {
// GoVersionWithOptionM returns the embedded module version information of
// the executable. `gal` in this case.
pathFileBin := filepath.Join("..", "..", "cmd", "testdata", "check_success", "gal")
if runtime.GOOS == "windows" {
pathFileBin = filepath.Join("..", "..", "cmd", "testdata", "check_success_for_windows", "gal.exe")
}

modInfo, err := goutil.GoVersionWithOptionM(pathFileBin)
if err != nil {
log.Fatal(err)
}

for _, info := range modInfo {
expectContains := "github.com/nao1215/gal"
if strings.Contains(info, expectContains) {
fmt.Println("Example GoVersionWithOptionM: OK")

break
}
}
// Output: Example GoVersionWithOptionM: OK
}

func ExampleInstall() {
// Install installs an executable from a Go package.
err := goutil.InstallLatest("example.com/unknown_user/unknown_package")
Expand Down
75 changes: 7 additions & 68 deletions internal/goutil/goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package goutil

import (
"bytes"
"debug/buildinfo"
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -266,15 +266,6 @@ func GoBin() (string, error) {
return filepath.Join(goPath, "bin"), nil
}

// GoVersionWithOptionM return result of "$ go version -m"
func GoVersionWithOptionM(bin string) ([]string, error) {
out, err := exec.Command(goExe, "version", "-m", bin).Output()
if err != nil {
return nil, err
}
return strings.Split(string(out), "\n"), nil
}

// BinaryPathList return list of binary paths.
func BinaryPathList(path string) ([]string, error) {
entries, err := os.ReadDir(path)
Expand All @@ -301,17 +292,15 @@ func BinaryPathList(path string) ([]string, error) {
func GetPackageInformation(binList []string) []Package {
pkgs := []Package{}
for _, v := range binList {
out, err := GoVersionWithOptionM(v)
info, err := buildinfo.ReadFile(v)
if err != nil {
print.Warn(fmt.Errorf("%s: %w", "can not get package path", err))
print.Warn(err)
continue
}
path := extractImportPath(out)
mod := extractModulePath(out)
pkg := Package{
Name: filepath.Base(v),
ImportPath: path,
ModulePath: mod,
ImportPath: info.Path,
ModulePath: info.Main.Path,
Version: NewVersion(),
}
pkg.SetCurrentVer()
Expand All @@ -326,59 +315,9 @@ func GetPackageVersion(cmdName string) string {
if err != nil {
return "unknown"
}

out, err := GoVersionWithOptionM(filepath.Join(goBin, cmdName))
info, err := buildinfo.ReadFile(filepath.Join(goBin, cmdName))
if err != nil {
return "unknown"
}

for _, v := range out {
vv := strings.TrimSpace(v)
if len(v) != len(vv) && strings.HasPrefix(vv, "mod") {
// mod github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
v = strings.TrimLeft(vv, "mod")
v = strings.TrimSpace(v)

//github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
r := regexp.MustCompile(`^[^\s]+(\s)`)
v = r.ReplaceAllString(v, "")

// v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
r = regexp.MustCompile(`(\s)[^\s]+$`)

// v1.0.2
return r.ReplaceAllString(v, "")
}
}
return "unknown"
}

// extractImportPath extract package import path from result of "$ go version -m".
func extractImportPath(lines []string) string {
for _, v := range lines {
vv := strings.TrimSpace(v)
if len(v) != len(vv) && strings.HasPrefix(vv, "path") {
vv = strings.TrimLeft(vv, "path")
vv = strings.TrimSpace(vv)
return strings.TrimRight(vv, "\n")
}
}
return ""
}

// extractModulePath extract package module path from result of "$ go version -m".
func extractModulePath(lines []string) string {
for _, v := range lines {
vv := strings.TrimSpace(v)
if len(v) != len(vv) && strings.HasPrefix(vv, "mod") {
// mod github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
v = strings.TrimLeft(vv, "mod")
v = strings.TrimSpace(v)

//github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
r := regexp.MustCompile(`(\s).*$`)
return r.ReplaceAllString(v, "")
}
}
return ""
return info.Main.Version
}
20 changes: 1 addition & 19 deletions internal/goutil/goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,6 @@ func TestBinaryPathList_exclusion(t *testing.T) {
}
}

func Test_extractImportPath_no_import_paths_to_extract(t *testing.T) {
// Assert to be empty
got := extractImportPath([]string{})

if got != "" {
t.Errorf("extractImportPath() should return empty string. got: %v", got)
}
}

func Test_extractModulePath_no_module_paths_to_extract(t *testing.T) {
// Assert to be empty
got := extractModulePath([]string{})

if got != "" {
t.Errorf("extractModulePath() should return empty string. got: %v", got)
}
}

func TestGetLatestVer_unknown_module(t *testing.T) {
out, err := GetLatestVer(".")

Expand Down Expand Up @@ -107,7 +89,7 @@ func TestGetPackageInformation_unknown_module(t *testing.T) {
}

// Assert to contain the expected error message
wantContain := "can not get package path"
wantContain := "could not read Go build info"
got := tmpBuff.String()
if !strings.Contains(got, wantContain) {
t.Errorf("it should print error message '%v'. got: %v", wantContain, got)
Expand Down

0 comments on commit 7a4a982

Please sign in to comment.