From c111018fe6d84ec7524645c61bfee0acd18bb0e3 Mon Sep 17 00:00:00 2001 From: rkscv Date: Sat, 23 Mar 2024 17:22:21 +0800 Subject: [PATCH 1/2] Use buildinfo --- internal/goutil/examples_test.go | 24 ---------- internal/goutil/goutil.go | 75 +++----------------------------- internal/goutil/goutil_test.go | 20 +-------- 3 files changed, 8 insertions(+), 111 deletions(-) diff --git a/internal/goutil/examples_test.go b/internal/goutil/examples_test.go index 095b211..dde9457 100644 --- a/internal/goutil/examples_test.go +++ b/internal/goutil/examples_test.go @@ -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") diff --git a/internal/goutil/goutil.go b/internal/goutil/goutil.go index 25e920e..bcbbc87 100644 --- a/internal/goutil/goutil.go +++ b/internal/goutil/goutil.go @@ -2,12 +2,12 @@ package goutil import ( "bytes" + "debug/buildinfo" "fmt" "go/build" "os" "os/exec" "path/filepath" - "regexp" "strings" "github.com/fatih/color" @@ -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) @@ -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() @@ -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 } diff --git a/internal/goutil/goutil_test.go b/internal/goutil/goutil_test.go index 2d000cd..7366cba 100644 --- a/internal/goutil/goutil_test.go +++ b/internal/goutil/goutil_test.go @@ -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(".") @@ -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) From e43a5a840485a81ed1be49d957b807f960c04e44 Mon Sep 17 00:00:00 2001 From: rkscv Date: Sat, 23 Mar 2024 20:00:17 +0800 Subject: [PATCH 2/2] Fix tests --- cmd/export_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/export_test.go b/cmd/export_test.go index a071e4e..e75bb4e 100644 --- a/cmd/export_test.go +++ b/cmd/export_test.go @@ -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", "", },