@@ -3,6 +3,7 @@ package main
33import (
44 "bytes"
55 "encoding/json"
6+ "flag"
67 "fmt"
78 "io/ioutil"
89 "os"
@@ -14,7 +15,7 @@ import (
1415)
1516
1617// debug controls whether we print extra statements.
17- var debug = true
18+ var debug bool
1819
1920// moduleInfo is the partial output of `go list -m -json all`.
2021type moduleInfo struct {
@@ -23,6 +24,10 @@ type moduleInfo struct {
2324}
2425
2526func main () {
27+ // Define the command-line flag
28+ flag .BoolVar (& debug , "debug" , false , "Enable debug output" )
29+ flag .Parse ()
30+
2631 if err := fixGoMod ("go.mod" ); err != nil {
2732 fmt .Fprintf (os .Stderr , "fixGoMod failed: %v\n " , err )
2833 os .Exit (1 )
@@ -59,13 +64,17 @@ func fixGoMod(goModPath string) error {
5964 if k8sVer == "" {
6065 return fmt .Errorf ("did not find k8s.io/kubernetes in require block" )
6166 }
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+ }
6370
6471 published := toPublishedVersion (k8sVer )
6572 if published == "" {
6673 return fmt .Errorf ("cannot derive staging version from %s" , k8sVer )
6774 }
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+ }
6978
7079 // forcibly unify the REQUIRE items for all staging modules
7180 forceRequireStaging (mf2 , published )
@@ -100,7 +109,7 @@ func fixGoMod(goModPath string) error {
100109 return fmt .Errorf ("running final go list: %w" , err )
101110 }
102111 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." )
104113 } else {
105114 fmt .Println ("Success: staging modules pinned to" , published )
106115 }
@@ -140,8 +149,10 @@ func pruneK8sReplaces(mf *modfile.File) {
140149 var keep []* modfile.Replace
141150 for _ , rep := range mf .Replace {
142151 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+ }
145156 } else {
146157 keep = append (keep , rep )
147158 }
@@ -166,13 +177,17 @@ func forceRequireStaging(mf *modfile.File, newVersion string) {
166177 }
167178 // remove them
168179 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+ }
170183 _ = mf .DropRequire (p ) // returns an error if not found, ignore
171184 }
172185 // re-add them at the new version if we can download that version
173186 for _ , p := range stagingPaths {
174187 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+ }
176191 _ = mf .AddRequire (p , newVersion )
177192 } else {
178193 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 {
198213 continue
199214 }
200215 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+ }
202219 continue
203220 }
204221 // unify everything if a valid tag exists
205222 if mi .Version != published {
206223 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+ }
208227 pins [mi .Path ] = published
209228 } else {
210229 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) {
227246 sort .Strings (sorted )
228247 for _ , path := range sorted {
229248 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+ }
231252 if err := mf .AddReplace (path , "" , path , ver ); err != nil {
232253 die ("Error adding replace for %s: %v" , path , err )
233254 }
@@ -242,15 +263,19 @@ func ensureKubernetesReplace(mf *modfile.File, k8sVer string) {
242263 if rep .Old .Path == "k8s.io/kubernetes" {
243264 found = true
244265 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+ }
247270 rep .New .Version = k8sVer
248271 }
249272 break
250273 }
251274 }
252275 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+ }
254279 if err := mf .AddReplace ("k8s.io/kubernetes" , "" , "k8s.io/kubernetes" , k8sVer ); err != nil {
255280 die ("Error adding replace for k8s.io/kubernetes: %v" , err )
256281 }
0 commit comments