Skip to content

Commit b589941

Browse files
authored
Merge pull request #2931 from manu12miskin/fix-io/ioutil-deprecated
🌱 : (fix golint) - Remove deprecated io/ioutil and use os, io instead
2 parents a6251f0 + 8e0fe5c commit b589941

File tree

10 files changed

+25
-31
lines changed

10 files changed

+25
-31
lines changed

docs/book/utils/litgo/literate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"go/scanner"
2222
"go/token"
23-
"io/ioutil"
2423
"log"
2524
"net/url"
2625
"os"
@@ -61,7 +60,7 @@ func (l Literate) Process(input *plugin.Input) error {
6160
path := pathInfo.FullPath()
6261

6362
// TODO(directxman12): don't escape root?
64-
contents, err := ioutil.ReadFile(path)
63+
contents, err := os.ReadFile(path)
6564
if err != nil {
6665
return "", fmt.Errorf("unable to import %q: %v", path, err)
6766
}

docs/book/utils/plugin/plugin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"os"
2524
)
2625

@@ -65,7 +64,7 @@ func Run(plug Plugin, inputRaw io.Reader, outputRaw io.Writer, args ...string) e
6564
return fmt.Errorf("unable to write output book object: %v", err)
6665
}
6766

68-
ioutil.WriteFile("/tmp/litout.json", out, os.ModePerm)
67+
os.WriteFile("/tmp/litout.json", out, os.ModePerm)
6968

7069
return nil
7170
}

pkg/cli/alpha/config-gen/cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package configgen
1919
import (
2020
"embed"
2121
"fmt"
22-
"io/ioutil"
2322
"log"
2423
"os"
2524
"path/filepath"
@@ -316,7 +315,7 @@ kubebuilder alpha config-gen install-as-plugin
316315
}
317316

318317
// r-x perms to prevent overwrite vulnerability since the script will be executed out-of-tree.
319-
return ioutil.WriteFile(fullScriptPath, []byte(pluginScript), 0o500)
318+
return os.WriteFile(fullScriptPath, []byte(pluginScript), 0o500)
320319
},
321320
}
322321
c.AddCommand(install)

pkg/cli/alpha/config-gen/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package configgen
1818

1919
import (
20-
"io/ioutil"
20+
"os"
2121
"time"
2222

2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -210,7 +210,7 @@ func (kp *KubebuilderConfigGen) Default() error {
210210
}
211211

212212
if kp.Spec.ControllerManager.ComponentConfig.ConfigFilepath != "" {
213-
b, err := ioutil.ReadFile(kp.Spec.ControllerManager.ComponentConfig.ConfigFilepath)
213+
b, err := os.ReadFile(kp.Spec.ControllerManager.ComponentConfig.ConfigFilepath)
214214
if err != nil {
215215
return err
216216
}

pkg/cli/cli_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package cli
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
21+
"io"
2222
"os"
2323
"strings"
2424

@@ -590,7 +590,7 @@ plugins:
590590
_ = w.Close()
591591

592592
Expect(err).NotTo(HaveOccurred())
593-
printed, _ := ioutil.ReadAll(r)
593+
printed, _ := io.ReadAll(r)
594594
Expect(string(printed)).To(Equal(
595595
fmt.Sprintf(noticeColor, fmt.Sprintf(deprecationFmt, deprecationWarning))))
596596
})

pkg/plugin/util/util.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/rand"
2323
"errors"
2424
"fmt"
25-
"io/ioutil"
2625
"math/big"
2726
"os"
2827
"regexp"
@@ -67,7 +66,7 @@ func GetNonEmptyLines(output string) []string {
6766
func InsertCode(filename, target, code string) error {
6867
// false positive
6968
// nolint:gosec
70-
contents, err := ioutil.ReadFile(filename)
69+
contents, err := os.ReadFile(filename)
7170
if err != nil {
7271
return err
7372
}
@@ -78,15 +77,15 @@ func InsertCode(filename, target, code string) error {
7877
out := string(contents[:idx+len(target)]) + code + string(contents[idx+len(target):])
7978
// false positive
8079
// nolint:gosec
81-
return ioutil.WriteFile(filename, []byte(out), 0644)
80+
return os.WriteFile(filename, []byte(out), 0644)
8281
}
8382

8483
// UncommentCode searches for target in the file and remove the comment prefix
8584
// of the target content. The target content may span multiple lines.
8685
func UncommentCode(filename, target, prefix string) error {
8786
// false positive
8887
// nolint:gosec
89-
content, err := ioutil.ReadFile(filename)
88+
content, err := os.ReadFile(filename)
9089
if err != nil {
9190
return err
9291
}
@@ -127,14 +126,14 @@ func UncommentCode(filename, target, prefix string) error {
127126
}
128127
// false positive
129128
// nolint:gosec
130-
return ioutil.WriteFile(filename, out.Bytes(), 0644)
129+
return os.WriteFile(filename, out.Bytes(), 0644)
131130
}
132131

133132
// ImplementWebhooks will mock an webhook data
134133
func ImplementWebhooks(filename string) error {
135134
// false positive
136135
// nolint:gosec
137-
bs, err := ioutil.ReadFile(filename)
136+
bs, err := os.ReadFile(filename)
138137
if err != nil {
139138
return err
140139
}
@@ -181,7 +180,7 @@ func ImplementWebhooks(filename string) error {
181180
}
182181
// false positive
183182
// nolint:gosec
184-
return ioutil.WriteFile(filename, []byte(str), 0644)
183+
return os.WriteFile(filename, []byte(str), 0644)
185184
}
186185

187186
// EnsureExistAndReplace check if the content exists and then do the replace
@@ -200,15 +199,15 @@ func ReplaceInFile(path, old, new string) error {
200199
}
201200
// false positive
202201
// nolint:gosec
203-
b, err := ioutil.ReadFile(path)
202+
b, err := os.ReadFile(path)
204203
if err != nil {
205204
return err
206205
}
207206
if !strings.Contains(string(b), old) {
208207
return errors.New("unable to find the content to be replaced")
209208
}
210209
s := strings.Replace(string(b), old, new, -1)
211-
err = ioutil.WriteFile(path, []byte(s), info.Mode())
210+
err = os.WriteFile(path, []byte(s), info.Mode())
212211
if err != nil {
213212
return err
214213
}
@@ -228,15 +227,15 @@ func ReplaceRegexInFile(path, match, replace string) error {
228227
}
229228
// false positive
230229
// nolint:gosec
231-
b, err := ioutil.ReadFile(path)
230+
b, err := os.ReadFile(path)
232231
if err != nil {
233232
return err
234233
}
235234
s := matcher.ReplaceAllString(string(b), replace)
236235
if s == string(b) {
237236
return errors.New("unable to find the content to be replaced")
238237
}
239-
err = ioutil.WriteFile(path, []byte(s), info.Mode())
238+
err = os.WriteFile(path, []byte(s), info.Mode())
240239
if err != nil {
241240
return err
242241
}
@@ -246,7 +245,7 @@ func ReplaceRegexInFile(path, match, replace string) error {
246245
// HasFileContentWith check if given `text` can be found in file
247246
func HasFileContentWith(path, text string) (bool, error) {
248247
// nolint:gosec
249-
contents, err := ioutil.ReadFile(path)
248+
contents, err := os.ReadFile(path)
250249
if err != nil {
251250
return false, err
252251
}

pkg/plugins/golang/declarative/v1/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package v1
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"path/filepath"
2323
"strings"
2424

@@ -74,7 +74,7 @@ func updateDockerfile() error {
7474
func insertCodeIfDoesNotExist(filename, target, code string) error {
7575
// false positive
7676
// nolint:gosec
77-
contents, err := ioutil.ReadFile(filename)
77+
contents, err := os.ReadFile(filename)
7878
if err != nil {
7979
return err
8080
}

pkg/plugins/golang/v3/commons.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package v3
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"path/filepath"
2323
"strings"
2424

@@ -37,7 +37,7 @@ const deprecateMsg = "The v1beta1 API version for CRDs and Webhooks are deprecat
3737
// nolint:lll,gosec
3838
func applyScaffoldCustomizationsForVbeta1() error {
3939
makefilePath := filepath.Join("Makefile")
40-
bs, err := ioutil.ReadFile(makefilePath)
40+
bs, err := os.ReadFile(makefilePath)
4141
if err != nil {
4242
return err
4343
}

pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package scaffolds
1919
import (
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322
"os"
2423
"strings"
2524

@@ -77,7 +76,7 @@ func loadConfig(configPath string) ([]templates.CustomMetricItem, error) {
7776
}
7877

7978
func configReader(reader io.Reader) ([]templates.CustomMetricItem, error) {
80-
yamlFile, err := ioutil.ReadAll(reader)
79+
yamlFile, err := io.ReadAll(reader)
8180
if err != nil {
8281
return nil, err
8382
}

test/e2e/utils/test_context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package utils
1919
import (
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322
"os"
2423
"os/exec"
2524
"path/filepath"
@@ -340,13 +339,13 @@ func (cc *CmdContext) Run(cmd *exec.Cmd) ([]byte, error) {
340339
func (t *TestContext) AllowProjectBeMultiGroup() error {
341340
const multiGroup = `multigroup: true
342341
`
343-
projectBytes, err := ioutil.ReadFile(filepath.Join(t.Dir, "PROJECT"))
342+
projectBytes, err := os.ReadFile(filepath.Join(t.Dir, "PROJECT"))
344343
if err != nil {
345344
return err
346345
}
347346

348347
projectBytes = append([]byte(multiGroup), projectBytes...)
349-
err = ioutil.WriteFile(filepath.Join(t.Dir, "PROJECT"), projectBytes, 0o644)
348+
err = os.WriteFile(filepath.Join(t.Dir, "PROJECT"), projectBytes, 0o644)
350349
if err != nil {
351350
return err
352351
}

0 commit comments

Comments
 (0)