Skip to content

Commit cc9a238

Browse files
authored
remove io/ioutil since is deprecated (#818)
1 parent 6c5dfae commit cc9a238

File tree

10 files changed

+25
-34
lines changed

10 files changed

+25
-34
lines changed

conn.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"encoding/pem"
2929
"errors"
3030
"fmt"
31-
"io/ioutil"
3231
"net/http"
3332
"os"
3433
"path/filepath"
@@ -134,7 +133,7 @@ func uploadHandler(c *gin.Context) {
134133
var filePaths []string
135134
filePaths = append(filePaths, filePath)
136135

137-
tmpdir, err := ioutil.TempDir("", "extrafiles")
136+
tmpdir, err := os.MkdirTemp("", "extrafiles")
138137
if err != nil {
139138
c.String(http.StatusBadRequest, err.Error())
140139
return
@@ -151,7 +150,7 @@ func uploadHandler(c *gin.Context) {
151150
return
152151
}
153152

154-
err := ioutil.WriteFile(path, extraFile.Hex, 0644)
153+
err := os.WriteFile(path, extraFile.Hex, 0644)
155154
if err != nil {
156155
c.String(http.StatusBadRequest, err.Error())
157156
return

tools/download.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"errors"
2828
"fmt"
2929
"io"
30-
"io/ioutil"
3130
"net/http"
3231
"os"
3332
"os/exec"
@@ -125,7 +124,7 @@ func (t *Tools) DownloadPackageIndex(indexFile, signatureFile string) error {
125124
defer resp.Body.Close()
126125

127126
// Read the body
128-
body, err := ioutil.ReadAll(resp.Body)
127+
body, err := io.ReadAll(resp.Body)
129128
if err != nil {
130129
return err
131130
}
@@ -138,12 +137,12 @@ func (t *Tools) DownloadPackageIndex(indexFile, signatureFile string) error {
138137
defer signature.Body.Close()
139138

140139
// Read the body
141-
signatureBody, err := ioutil.ReadAll(signature.Body)
140+
signatureBody, err := io.ReadAll(signature.Body)
142141
if err != nil {
143142
return err
144143
}
145-
ioutil.WriteFile(indexFile, body, 0644)
146-
ioutil.WriteFile(signatureFile, signatureBody, 0644)
144+
os.WriteFile(indexFile, body, 0644)
145+
os.WriteFile(signatureFile, signatureBody, 0644)
147146

148147
t.LastRefresh = time.Now()
149148

@@ -194,7 +193,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
194193
return err
195194
}
196195

197-
body, err := ioutil.ReadFile(indexFile)
196+
body, err := os.ReadFile(indexFile)
198197
if err != nil {
199198
return err
200199
}
@@ -236,7 +235,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
236235
defer resp.Body.Close()
237236

238237
// Read the body
239-
body, err = ioutil.ReadAll(resp.Body)
238+
body, err = io.ReadAll(resp.Body)
240239
if err != nil {
241240
return err
242241
}

tools/download_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package tools
1818
import (
1919
"encoding/json"
2020
"fmt"
21-
"io/ioutil"
21+
"io"
2222
"net/http"
2323
"os"
2424
"path"
@@ -104,7 +104,7 @@ func TestTools_DownloadAndUnpackBehaviour(t *testing.T) {
104104
}
105105
expectedDirList := []string{"bin", "etc"}
106106

107-
tmpDir, err := ioutil.TempDir("", "download_test")
107+
tmpDir, err := os.MkdirTemp("", "download_test")
108108
if err != nil {
109109
t.Fatal(err)
110110
}
@@ -119,7 +119,7 @@ func TestTools_DownloadAndUnpackBehaviour(t *testing.T) {
119119
defer resp.Body.Close()
120120

121121
// Read the body
122-
body, err := ioutil.ReadAll(resp.Body)
122+
body, err := io.ReadAll(resp.Body)
123123
if err != nil {
124124
t.Errorf("%v", err)
125125
}
@@ -148,7 +148,7 @@ func TestTools_DownloadAndUnpackBehaviour(t *testing.T) {
148148
default:
149149
t.Errorf("no suitable type found")
150150
}
151-
files, err := ioutil.ReadDir(location)
151+
files, err := os.ReadDir(location)
152152
if err != nil {
153153
t.Errorf("%v", err)
154154
}

tools/tools.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package tools
1818
import (
1919
"encoding/json"
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"os/user"
2423
"path"
@@ -125,15 +124,15 @@ func (t *Tools) writeMap() error {
125124
return err
126125
}
127126
filePath := path.Join(dir(), "installed.json")
128-
return ioutil.WriteFile(filePath, b, 0644)
127+
return os.WriteFile(filePath, b, 0644)
129128
}
130129

131130
// readMap() reads the installed map from json file "installed.json"
132131
func (t *Tools) readMap() error {
133132
t.mutex.Lock()
134133
defer t.mutex.Unlock()
135134
filePath := path.Join(dir(), "installed.json")
136-
b, err := ioutil.ReadFile(filePath)
135+
b, err := os.ReadFile(filePath)
137136
if err != nil {
138137
return err
139138
}

upload/upload.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"log"
2524
"mime/multipart"
2625
"net/http"
@@ -301,7 +300,7 @@ func form(port, board, file string, auth Auth, l Logger) error {
301300

302301
// Check the response
303302
if res.StatusCode != http.StatusOK {
304-
body, _ := ioutil.ReadAll(res.Body)
303+
body, _ := io.ReadAll(res.Body)
305304
return errors.New("Request error:" + string(body))
306305
}
307306
return nil

utilities/utilities.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"errors"
2222
"io"
23-
"io/ioutil"
2423
"os"
2524
"os/exec"
2625
"path"
@@ -35,7 +34,7 @@ import (
3534
// Note that path could be defined and still there could be an error.
3635
func SaveFileonTempDir(filename string, data io.Reader) (path string, err error) {
3736
// Create Temp Directory
38-
tmpdir, err := ioutil.TempDir("", "arduino-create-agent")
37+
tmpdir, err := os.MkdirTemp("", "arduino-create-agent")
3938
if err != nil {
4039
return "", errors.New("Could not create temp directory to store downloaded file. Do you have permissions?")
4140
}

v2/pkgs/indexes.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
b64 "encoding/base64"
2121
"encoding/json"
22-
"io/ioutil"
2322
"net/url"
2423
"os"
2524
"path/filepath"
@@ -71,7 +70,7 @@ func (c *Indexes) Add(ctx context.Context, payload *indexes.IndexPayload) (*inde
7170
func (c *Indexes) Get(ctx context.Context, uri string) (index Index, err error) {
7271
filename := b64.StdEncoding.EncodeToString([]byte(url.PathEscape(uri)))
7372
path := filepath.Join(c.Folder, filename)
74-
data, err := ioutil.ReadFile(path)
73+
data, err := os.ReadFile(path)
7574
if err != nil {
7675
return index, err
7776
}
@@ -89,7 +88,7 @@ func (c *Indexes) List(context.Context) ([]string, error) {
8988
// Create folder if it doesn't exist
9089
_ = os.MkdirAll(c.Folder, 0755)
9190
// Read files
92-
files, err := ioutil.ReadDir(c.Folder)
91+
files, err := os.ReadDir(c.Folder)
9392

9493
if err != nil {
9594
return nil, err

v2/pkgs/indexes_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package pkgs_test
1717

1818
import (
1919
"context"
20-
"io/ioutil"
2120
"net/http"
2221
"net/http/httptest"
2322
"os"
@@ -38,7 +37,7 @@ func TestIndexes(t *testing.T) {
3837
defer ts.Close()
3938

4039
// Initialize indexes with a temp folder
41-
tmp, err := ioutil.TempDir("", "")
40+
tmp, err := os.MkdirTemp("", "")
4241
if err != nil {
4342
t.Fatal(err)
4443
}

v2/pkgs/tools.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"errors"
2525
"fmt"
2626
"io"
27-
"io/ioutil"
2827
"net/http"
2928
"os"
3029
"path/filepath"
@@ -89,7 +88,7 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
8988
res := tools.ToolCollection{}
9089

9190
// Find packagers
92-
packagers, err := ioutil.ReadDir(c.Folder)
91+
packagers, err := os.ReadDir(c.Folder)
9392
if err != nil {
9493
if !strings.Contains(err.Error(), "no such file") {
9594
return nil, err
@@ -106,15 +105,15 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
106105
}
107106

108107
// Find tools
109-
toolss, err := ioutil.ReadDir(filepath.Join(c.Folder, packager.Name()))
108+
toolss, err := os.ReadDir(filepath.Join(c.Folder, packager.Name()))
110109
if err != nil {
111110
return nil, err
112111
}
113112

114113
for _, tool := range toolss {
115114
// Find versions
116115
path := filepath.Join(c.Folder, packager.Name(), tool.Name())
117-
versions, err := ioutil.ReadDir(path)
116+
versions, err := os.ReadDir(path)
118117
if err != nil {
119118
continue // we ignore errors because the folders could be dirty
120119
}
@@ -264,7 +263,7 @@ func writeInstalled(folder, path string) error {
264263
// read installed.json
265264
installed := map[string]string{}
266265

267-
data, err := ioutil.ReadFile(filepath.Join(folder, "installed.json"))
266+
data, err := os.ReadFile(filepath.Join(folder, "installed.json"))
268267
if err == nil {
269268
err = json.Unmarshal(data, &installed)
270269
if err != nil {
@@ -283,5 +282,5 @@ func writeInstalled(folder, path string) error {
283282
return err
284283
}
285284

286-
return ioutil.WriteFile(filepath.Join(folder, "installed.json"), data, 0644)
285+
return os.WriteFile(filepath.Join(folder, "installed.json"), data, 0644)
287286
}

v2/pkgs/tools_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package pkgs_test
1717

1818
import (
1919
"context"
20-
"io/ioutil"
2120
"net/http"
2221
"net/http/httptest"
2322
"os"
@@ -40,7 +39,7 @@ func TestTools(t *testing.T) {
4039
defer ts.Close()
4140

4241
// Initialize indexes with a temp folder
43-
tmp, err := ioutil.TempDir("", "")
42+
tmp, err := os.MkdirTemp("", "")
4443
if err != nil {
4544
t.Fatal(err)
4645
}

0 commit comments

Comments
 (0)