@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "flag"
6
7
"fmt"
7
8
"io/ioutil"
8
9
"os"
@@ -14,7 +15,7 @@ import (
14
15
)
15
16
16
17
// debug controls whether we print extra statements.
17
- var debug = true
18
+ var debug bool
18
19
19
20
// moduleInfo is the partial output of `go list -m -json all`.
20
21
type moduleInfo struct {
@@ -23,6 +24,10 @@ type moduleInfo struct {
23
24
}
24
25
25
26
func main () {
27
+ // Define the command-line flag
28
+ flag .BoolVar (& debug , "debug" , false , "Enable debug output" )
29
+ flag .Parse ()
30
+
26
31
if err := fixGoMod ("go.mod" ); err != nil {
27
32
fmt .Fprintf (os .Stderr , "fixGoMod failed: %v\n " , err )
28
33
os .Exit (1 )
@@ -59,13 +64,17 @@ func fixGoMod(goModPath string) error {
59
64
if k8sVer == "" {
60
65
return fmt .Errorf ("did not find k8s.io/kubernetes in require block" )
61
66
}
62
- fmt .Printf ("Found k8s.io/kubernetes version: %s\n " , k8sVer )
67
+ if debug {
68
+ fmt .Printf ("Found k8s.io/kubernetes version: %s\n " , k8sVer )
69
+ }
63
70
64
71
published := toPublishedVersion (k8sVer )
65
72
if published == "" {
66
73
return fmt .Errorf ("cannot derive staging version from %s" , k8sVer )
67
74
}
68
- fmt .Printf ("Unifying staging modules to: %s (from %s)\n " , published , k8sVer )
75
+ if debug {
76
+ fmt .Printf ("Unifying staging modules to: %s (from %s)\n " , published , k8sVer )
77
+ }
69
78
70
79
// forcibly unify the REQUIRE items for all staging modules
71
80
forceRequireStaging (mf2 , published )
@@ -100,7 +109,7 @@ func fixGoMod(goModPath string) error {
100
109
return fmt .Errorf ("running final go list: %w" , err )
101
110
}
102
111
if bytes .Contains (finalOut , []byte ("v0.0.0" )) {
103
- fmt .Println ("Warning : Some modules remain at v0.0.0, possibly no valid tags." )
112
+ fmt .Println ("WARNING : Some modules remain at v0.0.0, possibly no valid tags." )
104
113
} else {
105
114
fmt .Println ("Success: staging modules pinned to" , published )
106
115
}
@@ -140,8 +149,10 @@ func pruneK8sReplaces(mf *modfile.File) {
140
149
var keep []* modfile.Replace
141
150
for _ , rep := range mf .Replace {
142
151
if strings .HasPrefix (rep .Old .Path , "k8s.io/" ) {
143
- fmt .Printf ("Dropping old replace for %s => %s %s\n " ,
144
- rep .Old .Path , rep .New .Path , rep .New .Version )
152
+ if debug {
153
+ fmt .Printf ("Dropping old replace for %s => %s %s\n " ,
154
+ rep .Old .Path , rep .New .Path , rep .New .Version )
155
+ }
145
156
} else {
146
157
keep = append (keep , rep )
147
158
}
@@ -166,13 +177,17 @@ func forceRequireStaging(mf *modfile.File, newVersion string) {
166
177
}
167
178
// remove them
168
179
for _ , p := range stagingPaths {
169
- fmt .Printf ("Removing require line for %s\n " , p )
180
+ if debug {
181
+ fmt .Printf ("Removing require line for %s\n " , p )
182
+ }
170
183
_ = mf .DropRequire (p ) // returns an error if not found, ignore
171
184
}
172
185
// re-add them at the new version if we can download that version
173
186
for _ , p := range stagingPaths {
174
187
if versionExists (p , newVersion ) {
175
- fmt .Printf ("Adding require line for %s at %s\n " , p , newVersion )
188
+ if debug {
189
+ fmt .Printf ("Adding require line for %s at %s\n " , p , newVersion )
190
+ }
176
191
_ = mf .AddRequire (p , newVersion )
177
192
} else {
178
193
fmt .Printf ("WARNING: no valid tag for %s at %s, skipping\n " , p , newVersion )
@@ -198,13 +213,17 @@ func discoverPinsAlways(listOut, published string) map[string]string {
198
213
continue
199
214
}
200
215
if hasMajorVersionSuffix (mi .Path ) {
201
- fmt .Printf ("Skipping major-version module %s\n " , mi .Path )
216
+ if debug {
217
+ fmt .Printf ("Skipping major-version module %s\n " , mi .Path )
218
+ }
202
219
continue
203
220
}
204
221
// unify everything if a valid tag exists
205
222
if mi .Version != published {
206
223
if versionExists (mi .Path , published ) {
207
- fmt .Printf ("Pinning %s from %s to %s\n " , mi .Path , mi .Version , published )
224
+ if debug {
225
+ fmt .Printf ("Pinning %s from %s to %s\n " , mi .Path , mi .Version , published )
226
+ }
208
227
pins [mi .Path ] = published
209
228
} else {
210
229
fmt .Printf ("WARNING: no valid tag for %s at %s, leaving as %s\n " ,
@@ -227,7 +246,9 @@ func applyReplacements(mf *modfile.File, pins map[string]string) {
227
246
sort .Strings (sorted )
228
247
for _ , path := range sorted {
229
248
ver := pins [path ]
230
- fmt .Printf ("Applying new replace: %s => %s\n " , path , ver )
249
+ if debug {
250
+ fmt .Printf ("Applying new replace: %s => %s\n " , path , ver )
251
+ }
231
252
if err := mf .AddReplace (path , "" , path , ver ); err != nil {
232
253
die ("Error adding replace for %s: %v" , path , err )
233
254
}
@@ -242,15 +263,19 @@ func ensureKubernetesReplace(mf *modfile.File, k8sVer string) {
242
263
if rep .Old .Path == "k8s.io/kubernetes" {
243
264
found = true
244
265
if rep .New .Version != k8sVer {
245
- fmt .Printf ("Updating k8s.io/kubernetes replace from %s to %s\n " ,
246
- rep .New .Version , k8sVer )
266
+ if debug {
267
+ fmt .Printf ("Updating k8s.io/kubernetes replace from %s to %s\n " ,
268
+ rep .New .Version , k8sVer )
269
+ }
247
270
rep .New .Version = k8sVer
248
271
}
249
272
break
250
273
}
251
274
}
252
275
if ! found {
253
- fmt .Printf ("Inserting k8s.io/kubernetes => %s\n " , k8sVer )
276
+ if debug {
277
+ fmt .Printf ("Inserting k8s.io/kubernetes => %s\n " , k8sVer )
278
+ }
254
279
if err := mf .AddReplace ("k8s.io/kubernetes" , "" , "k8s.io/kubernetes" , k8sVer ); err != nil {
255
280
die ("Error adding replace for k8s.io/kubernetes: %v" , err )
256
281
}
0 commit comments