-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (120 loc) · 3.32 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"debug/buildinfo"
"errors"
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
func main() {
if err := cmd().Execute(); err != nil {
var exitCode *exitCodeError
if errors.As(err, &exitCode) {
os.Exit(exitCode.code)
}
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
// exitCodeError wraps an error with a given exit code.
type exitCodeError struct {
code int
err error
}
func (e *exitCodeError) Error() string {
return e.err.Error()
}
func (e *exitCodeError) Unwrap() error {
return e.err
}
const long = `gobincache determines whether a Go binary is up-to-date relative to its module
in your go.mod.
It assumes the use of a "tools.go" approach to versioning binaries in your
project.
The command will return an exit code of 0 when the binary currently installed
is up-to-date. It will return an exit code of 2 when it is either not present
or requires updating via "go install".
Any other error will cause this command to exit with a code of 1 (e.g. failing
to parse to go.mod file).
`
// cmd builds the root *cobra.Command hierarchy.
func cmd() *cobra.Command {
c := &cobra.Command{
Use: "gobincache [path to Go binary]",
Short: "Determines whether a Go binary requires updating, relative to it's version in the go.mod.",
Long: long,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
needsInstall, err := requiresInstall(args[0])
if err != nil {
return err
}
if needsInstall {
return &exitCodeError{code: 2, err: fmt.Errorf("binary requires install")}
}
return nil
},
}
return c
}
func requiresInstall(binPath string) (bool, error) {
b, err := os.ReadFile("go.mod")
if err != nil {
return false, fmt.Errorf("reading modfile: %w", err)
}
gomod, err := modfile.Parse("", b, nil)
if err != nil {
return false, fmt.Errorf("parsing modfile: %w", err)
}
info, err := buildinfo.ReadFile(binPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return true, nil
}
return false, fmt.Errorf("reading binary buildinfo (%s): %w", binPath, err)
}
goUpdate, err := needsUpdateForGo(gomod, info)
if err != nil {
return false, err
}
if goUpdate {
return true, nil
}
bin := info.Main
mod := versionFromGoMod(gomod, bin)
// We didn't find a match between the modfile and the binary. Can happen if
// a binary has changed import paths.
if mod == nil {
return true, nil
}
return bin.Version != mod.Version, nil
}
func needsUpdateForGo(gomod *modfile.File, info *debug.BuildInfo) (bool, error) {
modGoVersion := "v" + gomod.Go.Version
binGoVersion := "v" + strings.TrimPrefix(info.GoVersion, "go")
if !semver.IsValid(modGoVersion) {
return false, fmt.Errorf("parsing go.mod go version (%s)", modGoVersion)
}
if !semver.IsValid(binGoVersion) {
return false, fmt.Errorf("parsing binary go version (%s)", binGoVersion)
}
if semver.Compare(binGoVersion, modGoVersion) == -1 {
return true, nil
}
return false, nil
}
func versionFromGoMod(gomod *modfile.File, binaryModule debug.Module) *module.Version {
for _, required := range gomod.Require {
if required.Mod.Path == binaryModule.Path {
return &required.Mod
}
}
return nil
}