-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinfo.go
59 lines (54 loc) · 1.07 KB
/
binfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package binfo
import (
"context"
"debug/buildinfo"
"go/build"
"os"
"path/filepath"
"runtime/debug"
)
// Gobin returns GOBIN value.
func Gobin() string {
gobin := os.Getenv("GOBIN")
if gobin != "" {
return gobin
}
list := filepath.SplitList(build.Default.GOPATH)
if len(list) > 0 && list[0] != "" {
return filepath.Join(list[0], "bin")
}
return ""
}
// ExeInfo is information of executable file.
type ExeInfo struct {
Name string
Err error
debug.BuildInfo
}
func readExeInfo(name string) *ExeInfo {
bi, err := buildinfo.ReadFile(name)
if err != nil {
return &ExeInfo{Name: name, Err: err}
}
return &ExeInfo{Name: name, BuildInfo: *bi}
}
// List returns list of ExeInfo in GOBIN.
func List(ctx context.Context) ([]*ExeInfo, error) {
dir := Gobin()
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var list []*ExeInfo
for _, e := range entries {
if err := ctx.Err(); err != nil {
return nil, err
}
if e.IsDir() {
continue
}
name := filepath.Join(dir, e.Name())
list = append(list, readExeInfo(name))
}
return list, nil
}