-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdep.go
More file actions
173 lines (153 loc) · 4.77 KB
/
dep.go
File metadata and controls
173 lines (153 loc) · 4.77 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package golinker
import (
"bytes"
"encoding/hex"
"fmt"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/olekukonko/tablewriter"
)
var onceDeps sync.Once
var deps map[string]string
// extractVersion returns the version tag or commit hash
func extractVersion(version string) string {
// Check version type
// 1. v1.2 (tag)
// 2. v6.0.0-20250303095825-24047e466509 (psuedo)
splits := strings.Split(version, "-")
if len(splits) == 3 && semver.IsValid(splits[0]) {
if _, err := time.Parse("20060102150405", splits[1]); err == nil {
// valid timestamp
if _, err := hex.DecodeString(splits[2]); err == nil {
// valid sha
return splits[2]
}
}
}
return version
}
// CheckDeps must only be called from within an init().
func CheckDeps(moduleName string, imports ...string) {
onceDeps.Do(func() {
_deps, ok := debug.ReadBuildInfo()
if !ok {
panic("couldn't get fetch build info")
}
// Dependencies baked into executable
deps = map[string]string{}
for _, mod := range _deps.Deps {
if mod.Replace != nil {
if _, exists := deps[mod.Replace.Path]; exists {
panic(mod.Replace.Path + " already exists")
}
deps[mod.Replace.Path] = mod.Replace.Version
} else {
if _, exists := deps[mod.Path]; exists {
panic(mod.Path + " already exists")
}
deps[mod.Path] = mod.Version
}
}
})
defer func() {
if r := recover(); r != nil {
table := tablewriter.NewWriter(os.Stderr)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Dep (" + moduleName + ")", "Required", "Current", "*"})
for _, i := range imports {
splits := strings.SplitN(i, "=>", 2)
splits = strings.SplitN(splits[len(splits)-1], "::", 2)
ip := splits[0]
_v := splits[1]
_current := deps[ip]
final := ""
v, current := extractVersion(_v), extractVersion(_current)
if v != current {
final = "<=="
}
table.Append([]string{ip, _v, _current, final})
}
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Dependency version mismatch: Exact Versions are required.")
fmt.Fprintln(os.Stderr, "The replace directive can be used to pin dependencies to a single commit (rather than a minimum version).")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "(This is a limitation also inherent in the plugin pkg: https://pkg.go.dev/plugin#hdr-Warnings)")
fmt.Fprintln(os.Stderr, "⮑ “Similar crashing problems are likely to arise unless all common dependencies of the application and its plugins are built from exactly the same source code.”")
fmt.Fprintln(os.Stderr, "")
table.Render()
// Generate go mod lines
f := &modfile.File{}
f.AddModuleStmt(moduleName)
replaceStmts := []string{}
for _, i := range imports {
splits := strings.SplitN(i, "=>", 2)
require := splits[0]
requireSplits := strings.SplitN(require, "::", 2)
ip := requireSplits[0]
ver := requireSplits[1]
f.AddRequire(ip, ver)
if len(splits) > 1 {
replace := splits[1]
replaceSplits := strings.SplitN(replace, "::", 2)
replaceStmts = append(replaceStmts, fmt.Sprintf(`%s => %s %s`, ip, replaceSplits[0], replaceSplits[1]))
} else {
replaceStmts = append(replaceStmts, fmt.Sprintf(`%s => %s %s`, ip, ip, ver))
}
}
modText, _ := f.Format()
if pos := bytes.Index(modText, []byte("require")); pos != -1 {
modText = modText[pos:]
}
if len(replaceStmts) > 0 {
buf := bytes.NewBuffer(modText)
buf.WriteString("\n// Pinned dependencies for " + moduleName + ":\nreplace (\n")
for _, r := range replaceStmts {
buf.WriteString(" " + r + "\n")
}
buf.WriteString(")")
modText = buf.Bytes()
}
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "sample go.mod:")
fmt.Fprintln(os.Stderr, string(modText))
os.Exit(1)
}
}()
for _, i := range imports {
var ip string // import path
var _v string // version (could be tag (ie. v1.0.0) or (psuedo) a1030444159b))
splits := strings.SplitN(i, "=>", 2)
if len(splits) > 1 {
// Replace found
splits := strings.SplitN(splits[1], "::", 2)
ip = splits[0]
_v = splits[1]
} else {
// No replacement OR artificial replacement we added in of itself
splits := strings.SplitN(splits[0], "::", 2)
ip = splits[0]
_v = splits[1]
}
_val, exists := deps[ip]
if !exists {
// huh? The required dependency is not in the build.
panic(ip)
}
if extractVersion(_v) != extractVersion(_val) {
panic(ip)
}
}
return
}
func GoVersionCheck(moduleName string, goBuildVersion string) {
if rv := runtime.Version(); rv != goBuildVersion {
fmt.Fprintf(os.Stderr, `module %s: was built using %s but application was built using: %s. Consider changing build tag.`, moduleName, goBuildVersion, rv)
os.Exit(1)
}
}