@@ -5,14 +5,13 @@ import (
55 "encoding/json"
66 "flag"
77 "fmt"
8- "golang.org/x/mod/module"
9- "io/ioutil"
108 "os"
119 "os/exec"
1210 "sort"
1311 "strings"
1412
1513 "golang.org/x/mod/modfile"
14+ "golang.org/x/mod/module"
1615)
1716
1817// debug controls whether we print extra statements.
@@ -110,7 +109,7 @@ func getGoVersion(goModPath string) (string, error) {
110109
111110// parseMod reads go.mod into memory as a modfile.File
112111func parseMod (path string ) (* modfile.File , error ) {
113- data , err := ioutil .ReadFile (path )
112+ data , err := os .ReadFile (path )
114113 if err != nil {
115114 return nil , fmt .Errorf ("reading %s: %w" , path , err )
116115 }
@@ -127,7 +126,7 @@ func writeModFile(mf *modfile.File, path string) error {
127126 if err != nil {
128127 return fmt .Errorf ("formatting modfile: %w" , err )
129128 }
130- if err := ioutil .WriteFile (path , formatted , 0644 ); err != nil {
129+ if err := os .WriteFile (path , formatted , 0600 ); err != nil {
131130 return fmt .Errorf ("writing %s: %w" , path , err )
132131 }
133132 if debug {
@@ -231,7 +230,7 @@ func applyReplacements(mf *modfile.File, pins map[string]string) {
231230 if len (pins ) == 0 {
232231 return
233232 }
234- var sorted []string
233+ sorted := make ( []string , 0 , len ( pins ))
235234 for p := range pins {
236235 sorted = append (sorted , p )
237236 }
@@ -250,7 +249,7 @@ func applyReplacements(mf *modfile.File, pins map[string]string) {
250249// ensureKubernetesReplace ensures there's a "k8s.io/kubernetes => k8s.io/kubernetes vX.Y.Z" line
251250// matching the require(...) version in case something references it directly.
252251func ensureKubernetesReplace (mf * modfile.File , k8sVer string ) {
253- var newReplaces []* modfile.Replace
252+ newReplaces := make ( []* modfile.Replace , 0 , len ( mf . Replace ) + 1 )
254253
255254 for _ , rep := range mf .Replace {
256255 if rep .Old .Path == "k8s.io/kubernetes" && rep .New .Version != k8sVer {
@@ -313,7 +312,8 @@ func runCmd(name string, args ...string) error {
313312
314313// versionExists quietly tries `go mod download modPath@ver`. If 0 exit code => true.
315314func versionExists (modPath , ver string ) bool {
316- cmd := exec .Command ("go" , "mod" , "download" , fmt .Sprintf ("%s@%s" , modPath , ver ))
315+ safeArg := fmt .Sprintf ("%s@%s" , modPath , ver )
316+ cmd := exec .Command ("go" , "mod" , "download" , safeArg )
317317 cmd .Stdout = nil
318318 cmd .Stderr = nil
319319 return cmd .Run () == nil
0 commit comments