-
Notifications
You must be signed in to change notification settings - Fork 53
/
metadata.go
74 lines (65 loc) · 1.37 KB
/
metadata.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package meta
import (
"debug/buildinfo"
"io"
"os"
"path/filepath"
"runtime/debug"
)
type PluginMeta struct {
BinaryPath string
GoVersion string
Module string
// Extended metadata not populated from build info.
ExtendedMetadata *ExtendedPluginMeta
}
type ExtendedPluginMeta struct {
ModeList ModeList
}
func (pm PluginMeta) Filename() string {
return filepath.Base(pm.BinaryPath)
}
// Reads relevant metadata from the binary at the given path.
func ReadPath(path string) (PluginMeta, error) {
info, err := buildinfo.ReadFile(path)
if err != nil {
return PluginMeta{}, err
}
return PluginMeta{
BinaryPath: path,
GoVersion: info.GoVersion,
Module: info.Path,
}, nil
}
type file interface {
io.ReaderAt
Name() string
}
// Reads relevant metadata from an opened file. Does not change the i/o offset
// of the file.
func ReadFile(f file) (PluginMeta, error) {
info, err := buildinfo.Read(f)
if err != nil {
return PluginMeta{}, err
}
return PluginMeta{
BinaryPath: f.Name(),
GoVersion: info.GoVersion,
Module: info.Path,
}, nil
}
func ReadMetadata() PluginMeta {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
panic("could not read build info")
}
executable, err := os.Executable()
if err != nil {
panic(err)
}
return PluginMeta{
BinaryPath: executable,
GoVersion: buildInfo.GoVersion,
Module: buildInfo.Main.Path,
}
}