Skip to content

Commit 5880634

Browse files
committed
Replace deprecated io/ioutil with modern os/io equivalents
Replaces all ioutil.ReadFile, WriteFile, ReadDir, TempDir, TempFile, ReadAll and Discard calls with their Go 1.16+ replacements in os and io packages. Also removes deprecated rand.Seed(time.Now().UnixNano()) from randomString() (deprecated since Go 1.20) and removes the now-unused time import.
1 parent 7ef75dc commit 5880634

5 files changed

Lines changed: 31 additions & 39 deletions

File tree

src/nginx/supply/supply.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"math/rand"
109
"os"
1110
"os/exec"
1211
"path/filepath"
1312
"regexp"
1413
"sort"
1514
"strings"
16-
"time"
1715

1816
"github.com/cloudfoundry/libbuildpack"
1917
)
@@ -180,7 +178,7 @@ func (s *Supplier) ValidateNginxConf() error {
180178
}
181179

182180
func (s *Supplier) CheckAccessLogging() error {
183-
contents, err := ioutil.ReadFile(filepath.Join(s.Stager.BuildDir(), "nginx.conf"))
181+
contents, err := os.ReadFile(filepath.Join(s.Stager.BuildDir(), "nginx.conf"))
184182
if err != nil {
185183
return err
186184
}
@@ -238,7 +236,7 @@ func (s *Supplier) InstallOpenResty() error {
238236
}
239237

240238
func (s *Supplier) validateNginxConfHasPort() error {
241-
tmpDir, err := ioutil.TempDir("", "")
239+
tmpDir, err := os.MkdirTemp("", "")
242240
if err != nil {
243241
return fmt.Errorf("failed to create temp dir: %w", err)
244242
}
@@ -257,7 +255,7 @@ func (s *Supplier) validateNginxConfHasPort() error {
257255
return fmt.Errorf("varify command failed: %w\noutput: %s", err, string(output))
258256
}
259257

260-
confContents, err := ioutil.ReadFile(nginxConfPath)
258+
confContents, err := os.ReadFile(nginxConfPath)
261259
if err != nil {
262260
return fmt.Errorf("error reading temp config file: %w", err)
263261
}
@@ -270,7 +268,7 @@ func (s *Supplier) validateNginxConfHasPort() error {
270268
if !filepath.IsAbs(confFile) {
271269
confFile = filepath.Join(tmpDir, confFile)
272270
}
273-
contents, err := ioutil.ReadFile(confFile)
271+
contents, err := os.ReadFile(confFile)
274272
if err != nil {
275273
return fmt.Errorf("error reading temp config file %s: %w", confFile, err)
276274
}
@@ -288,8 +286,6 @@ func (s *Supplier) validateNginxConfHasPort() error {
288286
}
289287

290288
func randomString(strLength int) string {
291-
rand.Seed(time.Now().UnixNano())
292-
293289
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
294290
const numCharsPossible = len(letters)
295291
randString := ""
@@ -302,7 +298,7 @@ func randomString(strLength int) string {
302298
}
303299

304300
func (s *Supplier) validateNGINXConfSyntax() error {
305-
tmpConfDir, err := ioutil.TempDir("/tmp", "conf")
301+
tmpConfDir, err := os.MkdirTemp("/tmp", "conf")
306302
if err != nil {
307303
return fmt.Errorf("Error creating temp nginx conf dir: %s", err.Error())
308304
}
@@ -318,8 +314,8 @@ func (s *Supplier) validateNGINXConfSyntax() error {
318314
buildpackYMLPath := filepath.Join(s.Stager.BuildDir(), "buildpack.yml")
319315
cmd := exec.Command(filepath.Join(s.Stager.DepDir(), "bin", "varify"), "-buildpack-yml-path", buildpackYMLPath, nginxConfPath, localModulePath, globalModulePath)
320316
cmd.Dir = tmpConfDir
321-
cmd.Stdout = ioutil.Discard
322-
cmd.Stderr = ioutil.Discard
317+
cmd.Stdout = io.Discard
318+
cmd.Stderr = io.Discard
323319
cmd.Env = append(os.Environ(), "PORT=8080")
324320
if err := s.Command.Run(cmd); err != nil {
325321
return err

src/nginx/supply/supply_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package supply_test
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
exec "os/exec"
98
"path/filepath"
@@ -41,7 +40,7 @@ var _ = Describe("Supply", func() {
4140
mockManifest = NewMockManifest(mockCtrl)
4241
mockInstaller = NewMockInstaller(mockCtrl)
4342
mockCommand = NewMockCommand(mockCtrl)
44-
depDir, err = ioutil.TempDir("", "nginx.depdir")
43+
depDir, err = os.MkdirTemp("", "nginx.depdir")
4544
Expect(err).ToNot(HaveOccurred())
4645
mockStager.EXPECT().DepDir().AnyTimes().Return(depDir)
4746
supplier = supply.New(mockStager, mockManifest, mockInstaller, logger, mockCommand)
@@ -207,7 +206,7 @@ var _ = Describe("Supply", func() {
207206
)
208207

209208
BeforeEach(func() {
210-
buildDir, err = ioutil.TempDir("", "")
209+
buildDir, err = os.MkdirTemp("", "")
211210
Expect(err).NotTo(HaveOccurred())
212211

213212
mockStager.EXPECT().BuildDir().Return(buildDir).AnyTimes()
@@ -221,7 +220,7 @@ var _ = Describe("Supply", func() {
221220

222221
It("calls varify to parse the port", func() {
223222
nginxConfPath := filepath.Join(buildDir, "nginx.conf")
224-
err := ioutil.WriteFile(nginxConfPath, []byte("{{port}}"), 0666)
223+
err := os.WriteFile(nginxConfPath, []byte("{{port}}"), 0666)
225224
Expect(err).NotTo(HaveOccurred())
226225

227226
mockCommand.EXPECT().
@@ -240,7 +239,7 @@ var _ = Describe("Supply", func() {
240239
Context("app's nginx.conf is missing {{port}}", func() {
241240
It("logs error stating {{port}} is missing", func() {
242241
nginxConfPath := filepath.Join(buildDir, "nginx.conf")
243-
err := ioutil.WriteFile(nginxConfPath, []byte("FOOBAR"), 0666)
242+
err := os.WriteFile(nginxConfPath, []byte("FOOBAR"), 0666)
244243
Expect(err).NotTo(HaveOccurred())
245244
mockCommand.EXPECT().RunWithOutput(gomock.Any())
246245
err = supplier.ValidateNginxConf()
@@ -256,31 +255,31 @@ var _ = Describe("Supply", func() {
256255
})
257256

258257
It("logs a warning when access logging is not set", func() {
259-
ioutil.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("some content"), 0666)
258+
os.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("some content"), 0666)
260259
Expect(supplier.CheckAccessLogging()).To(Succeed())
261260
Expect(buffer.String()).To(ContainSubstring("Warning: access logging is turned off in your nginx.conf file, this may make your app difficult to debug."))
262261
})
263262

264263
It("logs a warning when access logging is set to off", func() {
265-
ioutil.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log off"), 0666)
264+
os.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log off"), 0666)
266265
Expect(supplier.CheckAccessLogging()).To(Succeed())
267266
Expect(buffer.String()).To(ContainSubstring("Warning: access logging is turned off in your nginx.conf file, this may make your app difficult to debug."))
268267
})
269268

270269
It("logs a warning when access logging is set to off with extra spaces", func() {
271-
ioutil.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log off"), 0666)
270+
os.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log off"), 0666)
272271
Expect(supplier.CheckAccessLogging()).To(Succeed())
273272
Expect(buffer.String()).To(ContainSubstring("Warning: access logging is turned off in your nginx.conf file, this may make your app difficult to debug."))
274273
})
275274

276275
It("logs a warning when access logging is set to OFF", func() {
277-
ioutil.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log OFF"), 0666)
276+
os.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log OFF"), 0666)
278277
Expect(supplier.CheckAccessLogging()).To(Succeed())
279278
Expect(buffer.String()).To(ContainSubstring("Warning: access logging is turned off in your nginx.conf file, this may make your app difficult to debug."))
280279
})
281280

282281
It("logs a warning when access logging is set to a path", func() {
283-
ioutil.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log /some/path"), 0666)
282+
os.WriteFile(filepath.Join(buildDir, "nginx.conf"), []byte("access_log /some/path"), 0666)
284283
Expect(supplier.CheckAccessLogging()).To(Succeed())
285284
Expect(buffer.String()).ToNot(ContainSubstring("Warning: access logging is turned off in your nginx.conf file, this may make your app difficult to debug."))
286285
})

src/nginx/varify/cli/varify.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
htmlTemplate "html/template"
88
"io"
9-
"io/ioutil"
109
"log"
1110
"os"
1211
"path/filepath"
@@ -48,7 +47,7 @@ func main() {
4847
"The default nameservers %s will be used. Error: %s", resolvConfPath, defaultNameServer, err)
4948
}
5049

51-
body, err := ioutil.ReadFile(filename)
50+
body, err := os.ReadFile(filename)
5251
if err != nil {
5352
log.Fatalf("Could not read config file: %s: %s", filename, err)
5453
}
@@ -108,7 +107,7 @@ func main() {
108107
if !filepath.IsAbs(confFile) {
109108
confFile = filepath.Join(filepath.Dir(filename), confFile)
110109
}
111-
str, err = ioutil.ReadFile(confFile)
110+
str, err = os.ReadFile(confFile)
112111
if err != nil {
113112
log.Fatalf("Could not read config file: %s: %s", filename, err)
114113
}
@@ -169,7 +168,7 @@ func getPlaintextEnvVars(bpYMLPath string) ([]string, error) {
169168
return []string{}, nil
170169
}
171170

172-
bpYMLContents, err := ioutil.ReadFile(bpYMLPath)
171+
bpYMLContents, err := os.ReadFile(bpYMLPath)
173172
if err != nil {
174173
return []string{}, err
175174
}

src/nginx/varify/cli/varify_suite_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main_test
22

33
import (
4-
"io/ioutil"
54
"os/exec"
65
"path/filepath"
76
"testing"
@@ -28,7 +27,7 @@ var _ = AfterSuite(func() {
2827
})
2928

3029
func runCli(tmpDir, body string, env []string, localModulePath, globalModulePath string, resolveConfPath string, defaultNameServer string, bpYMLPath string, expectedExitcode int) (string, *gexec.Session) {
31-
Expect(ioutil.WriteFile(filepath.Join(tmpDir, "nginx.conf"), []byte(body), 0644)).To(Succeed())
30+
Expect(os.WriteFile(filepath.Join(tmpDir, "nginx.conf"), []byte(body), 0644)).To(Succeed())
3231
args := []string{filepath.Join(tmpDir, "nginx.conf"), localModulePath, globalModulePath, resolveConfPath, defaultNameServer}
3332
if bpYMLPath != "" {
3433
args = append([]string{"-buildpack-yml-path", bpYMLPath}, args...)
@@ -40,7 +39,7 @@ func runCli(tmpDir, body string, env []string, localModulePath, globalModulePath
4039
Expect(err).ToNot(HaveOccurred())
4140
Eventually(session).Should(gexec.Exit(expectedExitcode))
4241

43-
output, err := ioutil.ReadFile(filepath.Join(tmpDir, "nginx.conf"))
42+
output, err := os.ReadFile(filepath.Join(tmpDir, "nginx.conf"))
4443
Expect(err).ToNot(HaveOccurred())
4544

4645
return string(output), session

src/nginx/varify/cli/varify_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87

@@ -18,7 +17,7 @@ var _ = Describe("varify", func() {
1817

1918
BeforeEach(func() {
2019
var err error
21-
tmpDir, err = ioutil.TempDir("", "nginx.tmpdir")
20+
tmpDir, err = os.MkdirTemp("", "nginx.tmpdir")
2221
Expect(err).ToNot(HaveOccurred())
2322
})
2423

@@ -47,7 +46,7 @@ nginx:
4746
- "FOO"
4847
`
4948
defer os.RemoveAll(bpYMLPath)
50-
Expect(ioutil.WriteFile(bpYMLPath, []byte(contents), os.ModePerm)).To(Succeed())
49+
Expect(os.WriteFile(bpYMLPath, []byte(contents), os.ModePerm)).To(Succeed())
5150
body, _ := runCli(tmpDir, textBody, []string{`FOO={"abcd":1234}`}, "", "", "", "", bpYMLPath, 0)
5251
Expect(body).To(Equal(`The env var FOO is {"abcd":1234}`))
5352
})
@@ -62,11 +61,11 @@ nginx:
6261
listen {{port}};
6362
}
6463
`
65-
Expect(ioutil.WriteFile(filepath.Join(tmpDir, "custom.conf"), []byte(customConfStr), os.ModePerm)).To(Succeed())
64+
Expect(os.WriteFile(filepath.Join(tmpDir, "custom.conf"), []byte(customConfStr), os.ModePerm)).To(Succeed())
6665
body, _ := runCli(tmpDir, nginxConfStr, []string{"PORT=8080"}, "", "", "", "", "", 0)
6766
Expect(body).To(Equal(nginxConfStr))
6867

69-
contents, err := ioutil.ReadFile(filepath.Join(tmpDir, "custom.conf"))
68+
contents, err := os.ReadFile(filepath.Join(tmpDir, "custom.conf"))
7069
Expect(err).NotTo(HaveOccurred())
7170
Expect(string(contents)).To(Equal(`
7271
server {
@@ -83,8 +82,8 @@ nginx:
8382

8483
Expect(os.Mkdir(localModulePath, 0744)).To(Succeed())
8584
Expect(os.Mkdir(globalModulePath, 0744)).To(Succeed())
86-
Expect(ioutil.WriteFile(filepath.Join(localModulePath, "local.so"), []byte("dummy data"), 0644)).To(Succeed())
87-
Expect(ioutil.WriteFile(filepath.Join(globalModulePath, "global.so"), []byte("dummy data"), 0644)).To(Succeed())
85+
Expect(os.WriteFile(filepath.Join(localModulePath, "local.so"), []byte("dummy data"), 0644)).To(Succeed())
86+
Expect(os.WriteFile(filepath.Join(globalModulePath, "global.so"), []byte("dummy data"), 0644)).To(Succeed())
8887
})
8988

9089
Context("when the module is in local modules directory", func() {
@@ -109,28 +108,28 @@ nginx:
109108

110109
It("reads nameservers from the simple resolv-conf file", func() {
111110
var resolvConfPath = filepath.Join(tmpDir, "resolv-simple.conf")
112-
Expect(ioutil.WriteFile(resolvConfPath, []byte("nameserver "+nameserver1), 0644)).To(Succeed())
111+
Expect(os.WriteFile(resolvConfPath, []byte("nameserver "+nameserver1), 0644)).To(Succeed())
113112
body, _ := runCli(tmpDir, "Hi the nameservers are {{nameservers}}.", nil, "", "", resolvConfPath, defaultNameServer, "", 0)
114113
Expect(body).To(Equal("Hi the nameservers are " + nameserver1 + "."))
115114
})
116115

117116
It("reads nameservers from the unusual resolv-conf file", func() {
118117
var resolvConfPath = filepath.Join(tmpDir, "resolv-unusual.conf")
119-
Expect(ioutil.WriteFile(resolvConfPath, []byte("# comment 1\n \t nameserver "+nameserver1+" \t \n# comment 2"), 0644)).To(Succeed())
118+
Expect(os.WriteFile(resolvConfPath, []byte("# comment 1\n \t nameserver "+nameserver1+" \t \n# comment 2"), 0644)).To(Succeed())
120119
body, _ := runCli(tmpDir, "Hi the nameservers are {{nameservers}}.", nil, "", "", resolvConfPath, defaultNameServer, "", 0)
121120
Expect(body).To(Equal("Hi the nameservers are " + nameserver1 + "."))
122121
})
123122

124123
It("reads nameservers from the resolv-conf file with multiple entries", func() {
125124
var resolvConfPath = filepath.Join(tmpDir, "resolv-multiple.conf")
126-
Expect(ioutil.WriteFile(resolvConfPath, []byte("nameserver "+nameserver1+"\nnameserver "+nameserver2), 0644)).To(Succeed())
125+
Expect(os.WriteFile(resolvConfPath, []byte("nameserver "+nameserver1+"\nnameserver "+nameserver2), 0644)).To(Succeed())
127126
body, _ := runCli(tmpDir, "Hi the nameservers are {{nameservers}}.", nil, "", "", resolvConfPath, defaultNameServer, "", 0)
128127
Expect(body).To(Equal("Hi the nameservers are " + nameserver1 + " " + nameserver2 + "."))
129128
})
130129

131130
It("set the default nameservers if the resolv-conf file is empty", func() {
132131
var resolvConfPath = filepath.Join(tmpDir, "resolv-empty.conf")
133-
Expect(ioutil.WriteFile(resolvConfPath, []byte(""), 666)).To(Succeed())
132+
Expect(os.WriteFile(resolvConfPath, []byte(""), 666)).To(Succeed())
134133
body, _ := runCli(tmpDir, "Hi the nameservers are {{nameservers}}.", nil, "", "", resolvConfPath, defaultNameServer, "", 0)
135134
Expect(body).To(Equal("Hi the nameservers are " + defaultNameServer + "."))
136135
})

0 commit comments

Comments
 (0)