From 1645759742879a9c43030ff9ceb8b70656ad5921 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 17 Nov 2021 11:25:52 +0800 Subject: [PATCH] refactor: move from io/ioutil to io and os packages The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun --- src/cli/cli.go | 15 +++++++-------- src/croc/croc.go | 3 +-- src/croc/croc_test.go | 3 +-- src/install/updateversion.go | 5 ++--- src/utils/utils.go | 5 ++--- src/utils/utils_test.go | 9 ++++----- 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index 1a47870..1630dea 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -177,7 +176,7 @@ func getConfigFile() string { func determinePass(c *cli.Context) (pass string) { pass = c.String("pass") - b, err := ioutil.ReadFile(pass) + b, err := os.ReadFile(pass) if err == nil { pass = strings.TrimSpace(string(b)) } @@ -213,7 +212,7 @@ func send(c *cli.Context) (err error) { } else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 { crocOptions.RelayAddress = "" } - b, errOpen := ioutil.ReadFile(getConfigFile()) + b, errOpen := os.ReadFile(getConfigFile()) if errOpen == nil && !c.Bool("remember") { var rememberedOptions croc.Options err = json.Unmarshal(b, &rememberedOptions) @@ -310,7 +309,7 @@ func send(c *cli.Context) (err error) { } func getStdin() (fnames []string, err error) { - f, err := ioutil.TempFile(".", "croc-stdin-") + f, err := os.CreateTemp(".", "croc-stdin-") if err != nil { return } @@ -327,7 +326,7 @@ func getStdin() (fnames []string, err error) { } func makeTempFileWithString(s string) (fnames []string, err error) { - f, err := ioutil.TempFile(".", "croc-stdin-") + f, err := os.CreateTemp(".", "croc-stdin-") if err != nil { return } @@ -401,7 +400,7 @@ func saveConfig(c *cli.Context, crocOptions croc.Options) { log.Error(err) return } - err = ioutil.WriteFile(configFile, bConfig, 0644) + err = os.WriteFile(configFile, bConfig, 0644) if err != nil { log.Error(err) return @@ -451,7 +450,7 @@ func receive(c *cli.Context) (err error) { return } configFile = path.Join(configFile, "receive.json") - b, errOpen := ioutil.ReadFile(configFile) + b, errOpen := os.ReadFile(configFile) if errOpen == nil && !c.Bool("remember") { var rememberedOptions croc.Options err = json.Unmarshal(b, &rememberedOptions) @@ -509,7 +508,7 @@ func receive(c *cli.Context) (err error) { log.Error(err) return } - err = ioutil.WriteFile(configFile, bConfig, 0644) + err = os.WriteFile(configFile, bConfig, 0644) if err != nil { log.Error(err) return diff --git a/src/croc/croc.go b/src/croc/croc.go index 215230d..b9e63e1 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math" "net" "os" @@ -1482,7 +1481,7 @@ func (c *Client) receiveData(i int) { c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name, ) - b, _ := ioutil.ReadFile(pathToFile) + b, _ := os.ReadFile(pathToFile) fmt.Print(string(b)) } log.Debug("sending close-sender") diff --git a/src/croc/croc_test.go b/src/croc/croc_test.go index 877ba8e..140ab63 100644 --- a/src/croc/croc_test.go +++ b/src/croc/croc_test.go @@ -1,7 +1,6 @@ package croc import ( - "io/ioutil" "os" "sync" "testing" @@ -153,7 +152,7 @@ func TestCrocLocal(t *testing.T) { func TestCrocError(t *testing.T) { content := []byte("temporary file's content") - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { panic(err) } diff --git a/src/install/updateversion.go b/src/install/updateversion.go index 6da0201..1fea3af 100644 --- a/src/install/updateversion.go +++ b/src/install/updateversion.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "os/exec" "strings" @@ -43,7 +42,7 @@ func run() (err error) { } func replaceInFile(fname, start, end, replacement string) (err error) { - b, err := ioutil.ReadFile(fname) + b, err := os.ReadFile(fname) if err != nil { return } @@ -58,7 +57,7 @@ func replaceInFile(fname, start, end, replacement string) (err error) { fmt.Sprintf("%s%s%s", start, replacement, end), 1, ) - err = ioutil.WriteFile(fname, []byte(newF), 0644) + err = os.WriteFile(fname, []byte(newF), 0644) return } diff --git a/src/utils/utils.go b/src/utils/utils.go index 19c0e6e..3e68330 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -9,7 +9,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "log" "math" "math/big" @@ -136,7 +135,7 @@ func PublicIP() (ip string, err error) { defer resp.Body.Close() if resp.StatusCode == http.StatusOK { - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -283,7 +282,7 @@ func GetLocalIPs() (ips []string, err error) { } func RandomFileName() (fname string, err error) { - f, err := ioutil.TempFile(".", "croc-stdin-") + f, err := os.CreateTemp(".", "croc-stdin-") if err != nil { return } diff --git a/src/utils/utils_test.go b/src/utils/utils_test.go index 93ac39c..50e025e 100644 --- a/src/utils/utils_test.go +++ b/src/utils/utils_test.go @@ -3,7 +3,6 @@ package utils import ( "bytes" "fmt" - "io/ioutil" "log" "math/rand" "os" @@ -17,7 +16,7 @@ import ( var bigFileSize = 75000000 func bigFile() { - ioutil.WriteFile("bigfile.test", bytes.Repeat([]byte("z"), bigFileSize), 0666) + os.WriteFile("bigfile.test", bytes.Repeat([]byte("z"), bigFileSize), 0666) } func BenchmarkMD5(b *testing.B) { @@ -119,7 +118,7 @@ func TestMissingChunks(t *testing.T) { rand.Seed(1) bigBuff := make([]byte, fileSize) rand.Read(bigBuff) - ioutil.WriteFile("missing.test", bigBuff, 0644) + os.WriteFile("missing.test", bigBuff, 0644) empty := make([]byte, chunkSize) f, err := os.OpenFile("missing.test", os.O_RDWR, 0644) assert.Nil(t, err) @@ -139,7 +138,7 @@ func TestMissingChunks(t *testing.T) { os.Remove("missing.test") content := []byte("temporary file's content") - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { log.Fatal(err) } @@ -171,7 +170,7 @@ func TestMissingChunks(t *testing.T) { func TestHashFile(t *testing.T) { content := []byte("temporary file's content") - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { log.Fatal(err) }