Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Use buildinfo" because debug/buildinfo is not released #9

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions internal/goutil/goutil.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package goutil

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

"github.com/nao1215/gup/internal/print"
)
Expand Down Expand Up @@ -36,11 +36,7 @@ func Install(importPath string) error {

// GoPath return GOPATH environment variable.
func GoPath() string {
gopath := os.Getenv("GOPATH")
if gopath != "" {
return gopath
}
return build.Default.GOPATH
return os.Getenv("GOPATH")
}

// GoBin return $GOPATH/bin directory path.
Expand All @@ -52,6 +48,15 @@ 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("go", "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 @@ -74,16 +79,30 @@ func BinaryPathList(path string) ([]string, error) {
func GetPackageInformation(binList []string) []Package {
pkgs := []Package{}
for _, v := range binList {
info, err := buildinfo.ReadFile(v)
out, err := GoVersionWithOptionM(v)
if err != nil {
print.Warn(fmt.Errorf("%s: %w", "can not get package path", err))
continue
}
path := extractPackagePath(out)
pkg := Package{
Name: filepath.Base(v),
ImportPath: info.Path,
ImportPath: path,
}
pkgs = append(pkgs, pkg)
}
return pkgs
}

// extractPackagePath extract package path from result of "$ go version -m".
func extractPackagePath(lines []string) string {
r := regexp.MustCompile(`\s+?path`)
for _, v := range lines {
if r.MatchString(v) {
v = r.ReplaceAllString(v, "")
v = strings.TrimSpace(v)
return strings.TrimRight(v, "\n")
}
}
return ""
}
14 changes: 4 additions & 10 deletions internal/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@ import (
"os"

"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/nao1215/gup/internal/cmdinfo"
)

var (
stdout = colorable.NewColorableStdout()
stderr = colorable.NewColorableStderr()
)

// Info print information message at STDOUT.
func Info(msg string) {
fmt.Fprintf(stdout, "%s:%s: %s\n",
fmt.Fprintf(os.Stdout, "%s:%s: %s\n",
cmdinfo.Name(), color.GreenString("INFO"), msg)
}

// Warn print warning message at STDERR.
func Warn(err interface{}) {
fmt.Fprintf(stderr, "%s:%s: %v\n",
fmt.Fprintf(os.Stderr, "%s:%s: %v\n",
cmdinfo.Name(), color.YellowString("WARN"), err)
}

// Err print error message at STDERR.
func Err(err interface{}) {
fmt.Fprintf(stderr, "%s:%s: %v\n",
fmt.Fprintf(os.Stderr, "%s:%s: %v\n",
cmdinfo.Name(), color.HiYellowString("ERROR"), err)
}

// Fatal print dying message at STDERR.
func Fatal(err interface{}) {
fmt.Fprintf(stderr, "%s:%s: %v\n",
fmt.Fprintf(os.Stderr, "%s:%s: %v\n",
cmdinfo.Name(), color.RedString("FATAL"), err)
os.Exit(1)
}
Expand Down