Skip to content

Commit

Permalink
Merge pull request #6 from mattn/use-buildinfo
Browse files Browse the repository at this point in the history
Use buildinfo
  • Loading branch information
nao1215 committed Feb 22, 2022
2 parents 4d528ce + ec73fd7 commit 7d0adbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
37 changes: 9 additions & 28 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,7 +36,11 @@ func Install(importPath string) error {

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

// GoBin return $GOPATH/bin directory path.
Expand All @@ -48,15 +52,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("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 @@ -79,30 +74,16 @@ 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))
continue
}
path := extractPackagePath(out)
pkg := Package{
Name: filepath.Base(v),
ImportPath: path,
ImportPath: info.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: 10 additions & 4 deletions internal/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@ 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(os.Stdout, "%s:%s: %s\n",
fmt.Fprintf(stdout, "%s:%s: %s\n",
cmdinfo.Name(), color.GreenString("INFO"), msg)
}

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

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

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

0 comments on commit 7d0adbb

Please sign in to comment.