Skip to content

Commit 2498fd5

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. Removes the io/ioutil import from all affected files.
1 parent 2bfe16e commit 2498fd5

3 files changed

Lines changed: 27 additions & 30 deletions

File tree

src/staticfile/finalize/finalize.go

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

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"strings"
@@ -110,18 +109,18 @@ func (sf *Finalizer) WriteStartupFiles() error {
110109
return err
111110
}
112111

113-
err = ioutil.WriteFile(filepath.Join(profiledDir, "staticfile.sh"), []byte(initScript), 0755)
112+
err = os.WriteFile(filepath.Join(profiledDir, "staticfile.sh"), []byte(initScript), 0755)
114113
if err != nil {
115114
return err
116115
}
117116

118-
err = ioutil.WriteFile(filepath.Join(sf.BuildDir, "start_logging.sh"), []byte(startLoggingScript), 0755)
117+
err = os.WriteFile(filepath.Join(sf.BuildDir, "start_logging.sh"), []byte(startLoggingScript), 0755)
119118
if err != nil {
120119
return err
121120
}
122121

123122
bootScript := filepath.Join(sf.BuildDir, "boot.sh")
124-
return ioutil.WriteFile(bootScript, []byte(startCommand), 0755)
123+
return os.WriteFile(bootScript, []byte(startCommand), 0755)
125124
}
126125

127126
func (sf *Finalizer) LoadStaticfile() error {
@@ -258,12 +257,12 @@ func (sf *Finalizer) CopyFilesToPublic(appRootDir string) error {
258257
return nil
259258
}
260259

261-
tmpDir, err := ioutil.TempDir("", "staticfile-buildpack.approot.")
260+
tmpDir, err := os.MkdirTemp("", "staticfile-buildpack.approot.")
262261
if err != nil {
263262
return err
264263
}
265264

266-
files, err := ioutil.ReadDir(appRootDir)
265+
files, err := os.ReadDir(appRootDir)
267266
if err != nil {
268267
return err
269268
}
@@ -341,7 +340,7 @@ func (sf *Finalizer) ConfigureNginx() error {
341340
sf.Log.Warning("overriding nginx.conf is deprecated and highly discouraged, as it breaks the functionality of the Staticfile and Staticfile.auth configuration directives. Please use the NGINX buildpack available at: https://github.com/cloudfoundry/nginx-buildpack")
342341
}
343342
} else {
344-
err = ioutil.WriteFile(confDest, []byte(contents), 0644)
343+
err = os.WriteFile(confDest, []byte(contents), 0644)
345344
}
346345

347346
if err != nil {

src/staticfile/finalize/finalize_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package finalize_test
22

33
import (
44
"errors"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"regexp"
@@ -37,10 +36,10 @@ var _ = Describe("Compile", func() {
3736
)
3837

3938
BeforeEach(func() {
40-
buildDir, err = ioutil.TempDir("", "staticfile-buildpack.build.")
39+
buildDir, err = os.MkdirTemp("", "staticfile-buildpack.build.")
4140
Expect(err).To(BeNil())
4241

43-
depDir, err = ioutil.TempDir("", "staticfile-buildpack.depDir.")
42+
depDir, err = os.MkdirTemp("", "staticfile-buildpack.depDir.")
4443
Expect(err).To(BeNil())
4544

4645
buffer = new(bytes.Buffer)
@@ -75,7 +74,7 @@ var _ = Describe("Compile", func() {
7574
err = finalizer.WriteStartupFiles()
7675
Expect(err).To(BeNil())
7776

78-
contents, err := ioutil.ReadFile(filepath.Join(depDir, "profile.d", "staticfile.sh"))
77+
contents, err := os.ReadFile(filepath.Join(depDir, "profile.d", "staticfile.sh"))
7978
Expect(err).To(BeNil())
8079
Expect(string(contents)).To(ContainSubstring("export LD_LIBRARY_PATH=$APP_ROOT/nginx/lib:$LD_LIBRARY_PATH"))
8180
})
@@ -84,7 +83,7 @@ var _ = Describe("Compile", func() {
8483
err = finalizer.WriteStartupFiles()
8584
Expect(err).To(BeNil())
8685

87-
contents, err := ioutil.ReadFile(filepath.Join(buildDir, "start_logging.sh"))
86+
contents, err := os.ReadFile(filepath.Join(buildDir, "start_logging.sh"))
8887
Expect(err).To(BeNil())
8988
Expect(string(contents)).To(Equal("\ncat < $APP_ROOT/nginx/logs/access.log &\n(>&2 cat) < $APP_ROOT/nginx/logs/error.log &\n"))
9089
})
@@ -102,7 +101,7 @@ var _ = Describe("Compile", func() {
102101
err = finalizer.WriteStartupFiles()
103102
Expect(err).To(BeNil())
104103

105-
contents, err := ioutil.ReadFile(filepath.Join(buildDir, "boot.sh"))
104+
contents, err := os.ReadFile(filepath.Join(buildDir, "boot.sh"))
106105
Expect(err).To(BeNil())
107106
Expect(string(contents)).To(Equal("#!/bin/sh\nset -ex\n$APP_ROOT/start_logging.sh\nnginx -p $APP_ROOT/nginx -c $APP_ROOT/nginx/conf/nginx.conf\n"))
108107
})
@@ -350,7 +349,7 @@ var _ = Describe("Compile", func() {
350349

351350
Context("Staticfile.auth is present", func() {
352351
BeforeEach(func() {
353-
err = ioutil.WriteFile(filepath.Join(buildDir, "Staticfile.auth"), []byte("some credentials"), 0644)
352+
err = os.WriteFile(filepath.Join(buildDir, "Staticfile.auth"), []byte("some credentials"), 0644)
354353
Expect(err).To(BeNil())
355354
})
356355
JustBeforeEach(func() {
@@ -428,7 +427,7 @@ var _ = Describe("Compile", func() {
428427

429428
Context("the directory exists but is actually a file", func() {
430429
BeforeEach(func() {
431-
ioutil.WriteFile(filepath.Join(buildDir, "actually_a_file"), []byte("xxx"), 0644)
430+
os.WriteFile(filepath.Join(buildDir, "actually_a_file"), []byte("xxx"), 0644)
432431
staticfile.RootDir = "actually_a_file"
433432
})
434433

@@ -544,13 +543,13 @@ var _ = Describe("Compile", func() {
544543
err = os.MkdirAll(filepath.Join(buildDir, "public"), 0755)
545544
Expect(err).To(BeNil())
546545

547-
err = ioutil.WriteFile(filepath.Join(buildDir, "public", "nginx.conf"), []byte("nginx configuration"), 0644)
546+
err = os.WriteFile(filepath.Join(buildDir, "public", "nginx.conf"), []byte("nginx configuration"), 0644)
548547
Expect(err).To(BeNil())
549548
})
550549

551550
It("uses the custom configuration", func() {
552551
Expect(filepath.Join(buildDir, "nginx", "conf", "nginx.conf")).To(BeARegularFile())
553-
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
552+
data, err = os.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
554553
Expect(err).To(BeNil())
555554
Expect(data).To(Equal([]byte("nginx configuration")))
556555
})
@@ -568,7 +567,7 @@ var _ = Describe("Compile", func() {
568567
leadCloseWsp := regexp.MustCompile(`(?m)^\s+`)
569568
stripStartWsp := func(inp string) string { return leadCloseWsp.ReplaceAllString(inp, "") }
570569
readNginxConfAndStrip := func() string {
571-
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
570+
data, err = os.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
572571
Expect(err).To(BeNil())
573572
return stripStartWsp(string(data))
574573
}
@@ -820,7 +819,7 @@ var _ = Describe("Compile", func() {
820819
Context("there is a Staticfile.auth", func() {
821820
BeforeEach(func() {
822821
staticfile.BasicAuth = true
823-
err = ioutil.WriteFile(filepath.Join(buildDir, "Staticfile.auth"), []byte("authentication info"), 0644)
822+
err = os.WriteFile(filepath.Join(buildDir, "Staticfile.auth"), []byte("authentication info"), 0644)
824823
Expect(err).To(BeNil())
825824
})
826825

@@ -830,7 +829,7 @@ var _ = Describe("Compile", func() {
830829
})
831830

832831
It("copies the Staticfile.auth to .htpasswd", func() {
833-
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", ".htpasswd"))
832+
data, err = os.ReadFile(filepath.Join(buildDir, "nginx", "conf", ".htpasswd"))
834833
Expect(err).To(BeNil())
835834
Expect(string(data)).To(Equal("authentication info"))
836835
})
@@ -856,20 +855,20 @@ var _ = Describe("Compile", func() {
856855
err = os.MkdirAll(filepath.Join(buildDir, "public"), 0755)
857856
Expect(err).To(BeNil())
858857

859-
err = ioutil.WriteFile(filepath.Join(buildDir, "public", "mime.types"), []byte("mime types info"), 0644)
858+
err = os.WriteFile(filepath.Join(buildDir, "public", "mime.types"), []byte("mime types info"), 0644)
860859
Expect(err).To(BeNil())
861860
})
862861

863862
It("uses the custom configuration", func() {
864-
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "mime.types"))
863+
data, err = os.ReadFile(filepath.Join(buildDir, "nginx", "conf", "mime.types"))
865864
Expect(err).To(BeNil())
866865
Expect(data).To(Equal([]byte("mime types info")))
867866
})
868867
})
869868

870869
Context("custom mime.types does NOT exist", func() {
871870
It("uses the provided mime.types", func() {
872-
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "mime.types"))
871+
data, err = os.ReadFile(filepath.Join(buildDir, "nginx", "conf", "mime.types"))
873872
Expect(err).To(BeNil())
874873
Expect(string(data)).To(Equal(finalize.MimeTypes))
875874
})
@@ -888,14 +887,14 @@ var _ = Describe("Compile", func() {
888887
buildDirFiles = []string{"Staticfile", "Staticfile.auth", "manifest.yml", ".profile", "stackato.yml"}
889888

890889
for _, file := range buildDirFiles {
891-
err = ioutil.WriteFile(filepath.Join(buildDir, file), []byte(file+"contents"), 0644)
890+
err = os.WriteFile(filepath.Join(buildDir, file), []byte(file+"contents"), 0644)
892891
Expect(err).To(BeNil())
893892
}
894893

895894
appRootFiles = []string{".hidden.html", "index.html"}
896895

897896
for _, file := range appRootFiles {
898-
err = ioutil.WriteFile(filepath.Join(appRootDir, file), []byte(file+"contents"), 0644)
897+
err = os.WriteFile(filepath.Join(appRootDir, file), []byte(file+"contents"), 0644)
899898
Expect(err).To(BeNil())
900899
}
901900

@@ -920,7 +919,7 @@ var _ = Describe("Compile", func() {
920919
err = os.MkdirAll(appRootDir, 0755)
921920
Expect(err).To(BeNil())
922921

923-
err = ioutil.WriteFile(filepath.Join(appRootDir, "index2.html"), []byte("html contents"), 0644)
922+
err = os.WriteFile(filepath.Join(appRootDir, "index2.html"), []byte("html contents"), 0644)
924923
})
925924

926925
It("doesn't copy any files", func() {
@@ -944,7 +943,7 @@ var _ = Describe("Compile", func() {
944943
Context("host dotfiles is set", func() {
945944
BeforeEach(func() {
946945
staticfile.HostDotFiles = true
947-
appRootDir, err = ioutil.TempDir("", "staticfile-buildpack.app_root.")
946+
appRootDir, err = os.MkdirTemp("", "staticfile-buildpack.app_root.")
948947
Expect(err).To(BeNil())
949948
})
950949

@@ -979,7 +978,7 @@ var _ = Describe("Compile", func() {
979978
Context("and <buildDir>/public exists", func() {
980979
BeforeEach(func() {
981980
Expect(os.Mkdir(filepath.Join(buildDir, "public"), 0755)).To(Succeed())
982-
Expect(ioutil.WriteFile(filepath.Join(buildDir, "public", "orig.html"), []byte("html contents"), 0644)).To(Succeed())
981+
Expect(os.WriteFile(filepath.Join(buildDir, "public", "orig.html"), []byte("html contents"), 0644)).To(Succeed())
983982
})
984983
It("overrides <buildDir>/public", func() {
985984
Expect(filepath.Join(buildDir, "public", "orig.html")).ToNot(BeAnExistingFile())

src/staticfile/supply/supply_test.go

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

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76

@@ -33,7 +32,7 @@ var _ = Describe("Supply", func() {
3332
)
3433

3534
BeforeEach(func() {
36-
depsDir, err = ioutil.TempDir("", "staticfile-buildpack.deps.")
35+
depsDir, err = os.MkdirTemp("", "staticfile-buildpack.deps.")
3736
Expect(err).To(BeNil())
3837

3938
depsIdx = "32"

0 commit comments

Comments
 (0)