@@ -5,14 +5,13 @@ import (
5
5
"encoding/json"
6
6
"flag"
7
7
"fmt"
8
- "golang.org/x/mod/module"
9
- "io/ioutil"
10
8
"os"
11
9
"os/exec"
12
10
"sort"
13
11
"strings"
14
12
15
13
"golang.org/x/mod/modfile"
14
+ "golang.org/x/mod/module"
16
15
)
17
16
18
17
// debug controls whether we print extra statements.
@@ -110,7 +109,7 @@ func getGoVersion(goModPath string) (string, error) {
110
109
111
110
// parseMod reads go.mod into memory as a modfile.File
112
111
func parseMod (path string ) (* modfile.File , error ) {
113
- data , err := ioutil .ReadFile (path )
112
+ data , err := os .ReadFile (path )
114
113
if err != nil {
115
114
return nil , fmt .Errorf ("reading %s: %w" , path , err )
116
115
}
@@ -127,7 +126,7 @@ func writeModFile(mf *modfile.File, path string) error {
127
126
if err != nil {
128
127
return fmt .Errorf ("formatting modfile: %w" , err )
129
128
}
130
- if err := ioutil .WriteFile (path , formatted , 0644 ); err != nil {
129
+ if err := os .WriteFile (path , formatted , 0600 ); err != nil {
131
130
return fmt .Errorf ("writing %s: %w" , path , err )
132
131
}
133
132
if debug {
@@ -231,7 +230,7 @@ func applyReplacements(mf *modfile.File, pins map[string]string) {
231
230
if len (pins ) == 0 {
232
231
return
233
232
}
234
- var sorted []string
233
+ sorted := make ( []string , 0 , len ( pins ))
235
234
for p := range pins {
236
235
sorted = append (sorted , p )
237
236
}
@@ -250,7 +249,7 @@ func applyReplacements(mf *modfile.File, pins map[string]string) {
250
249
// ensureKubernetesReplace ensures there's a "k8s.io/kubernetes => k8s.io/kubernetes vX.Y.Z" line
251
250
// matching the require(...) version in case something references it directly.
252
251
func ensureKubernetesReplace (mf * modfile.File , k8sVer string ) {
253
- var newReplaces []* modfile.Replace
252
+ newReplaces := make ( []* modfile.Replace , 0 , len ( mf . Replace ) + 1 )
254
253
255
254
for _ , rep := range mf .Replace {
256
255
if rep .Old .Path == "k8s.io/kubernetes" && rep .New .Version != k8sVer {
@@ -313,7 +312,8 @@ func runCmd(name string, args ...string) error {
313
312
314
313
// versionExists quietly tries `go mod download modPath@ver`. If 0 exit code => true.
315
314
func 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 )
317
317
cmd .Stdout = nil
318
318
cmd .Stderr = nil
319
319
return cmd .Run () == nil
0 commit comments