Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Move from io/ioutil to io and os packages
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Juneezee committed Nov 20, 2021
1 parent ef07133 commit 417f0d1
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewConfig() *Config {
}

func loadJSON(filename string, v interface{}) error {
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("read error: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
Expand All @@ -15,7 +14,7 @@ import (
const testLogConf = "config/container-logs.json"

func configfile(s string) string {
tmp, err := ioutil.TempFile("", "config")
tmp, err := os.CreateTemp("", "config")
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package main

import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -39,7 +39,7 @@ func (mp *MetadataProcessor) OnSessionStopped(stoppedSession event.StoppedSessio
return
}
filename := filepath.Join(logOutputDir, stoppedSession.SessionId+metadataFileExtension)
err = ioutil.WriteFile(filename, data, 0644)
err = os.WriteFile(filename, data, 0644)
if err != nil {
log.Printf("[%d] [METADATA] [%s] [Failed to save to %s: %v]", stoppedSession.RequestId, stoppedSession.SessionId, filename, err)
return
Expand Down
4 changes: 2 additions & 2 deletions s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package main

import (
"context"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestS3Uploader(t *testing.T) {
ReducedRedundancy: true,
}
uploader.Init()
f, _ := ioutil.TempFile("", "some-file")
f, _ := os.CreateTemp("", "some-file")
input := event.CreatedFile{
Event: event.Event{
RequestId: 4342,
Expand Down
5 changes: 2 additions & 3 deletions selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -112,7 +111,7 @@ func create(w http.ResponseWriter, r *http.Request) {
sessionStartTime := time.Now()
requestId := serial()
user, remote := util.RequestInfo(r)
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
log.Printf("[%d] [ERROR_READING_REQUEST] [%v]", requestId, err)
Expand Down Expand Up @@ -680,7 +679,7 @@ func logs(w http.ResponseWriter, r *http.Request) {
}

func listFilesAsJson(requestId uint64, w http.ResponseWriter, dir string, errStatus string) {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
log.Printf("[%d] [%s] [%s]", requestId, errStatus, fmt.Sprintf("Failed to list directory %s: %v", logOutputDir, err))
w.WriteHeader(http.StatusInternalServerError)
Expand Down
20 changes: 10 additions & 10 deletions selenoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -34,8 +34,8 @@ var (

func init() {
enableFileUpload = true
videoOutputDir, _ = ioutil.TempDir("", "selenoid-test")
logOutputDir, _ = ioutil.TempDir("", "selenoid-test")
videoOutputDir, _ = os.MkdirTemp("", "selenoid-test")
logOutputDir, _ = os.MkdirTemp("", "selenoid-test")
saveAllLogs = true
gitRevision = "test-revision"
ggrHost = &ggr.Host{
Expand Down Expand Up @@ -652,7 +652,7 @@ func TestFileUpload(t *testing.T) {
f, err := os.Open(jsonResponse["value"])
AssertThat(t, err, Is{nil})

content, err := ioutil.ReadAll(f)
content, err := io.ReadAll(f)
AssertThat(t, err, Is{nil})

AssertThat(t, string(content), EqualTo{"Hello World!"})
Expand Down Expand Up @@ -720,7 +720,7 @@ func TestPing(t *testing.T) {
AssertThat(t, rsp.Body, Is{Not{nil}})

var data map[string]interface{}
bt, readErr := ioutil.ReadAll(rsp.Body)
bt, readErr := io.ReadAll(rsp.Body)
AssertThat(t, readErr, Is{nil})
jsonErr := json.Unmarshal(bt, &data)
AssertThat(t, jsonErr, Is{nil})
Expand All @@ -743,7 +743,7 @@ func TestStatus(t *testing.T) {
AssertThat(t, rsp.Body, Is{Not{nil}})

var data map[string]interface{}
bt, readErr := ioutil.ReadAll(rsp.Body)
bt, readErr := io.ReadAll(rsp.Body)
AssertThat(t, readErr, Is{nil})
jsonErr := json.Unmarshal(bt, &data)
AssertThat(t, jsonErr, Is{nil})
Expand All @@ -760,7 +760,7 @@ func TestStatus(t *testing.T) {
func TestServeAndDeleteVideoFile(t *testing.T) {
fileName := "testfile"
filePath := filepath.Join(videoOutputDir, fileName)
ioutil.WriteFile(filePath, []byte("test-data"), 0644)
os.WriteFile(filePath, []byte("test-data"), 0644)

rsp, err := http.Get(With(srv.URL).Path("/video/testfile"))
AssertThat(t, err, Is{nil})
Expand All @@ -787,7 +787,7 @@ func TestServeAndDeleteVideoFile(t *testing.T) {
func TestServeAndDeleteLogFile(t *testing.T) {
fileName := "logfile.log"
filePath := filepath.Join(logOutputDir, fileName)
ioutil.WriteFile(filePath, []byte("test-data"), 0644)
os.WriteFile(filePath, []byte("test-data"), 0644)

rsp, err := http.Get(With(srv.URL).Path("/logs/logfile.log"))
AssertThat(t, err, Is{nil})
Expand Down Expand Up @@ -822,7 +822,7 @@ func TestFileDownload(t *testing.T) {
rsp, err := http.Get(With(srv.URL).Path(fmt.Sprintf("/download/%s/testfile", sess["sessionId"])))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
data, err := ioutil.ReadAll(rsp.Body)
data, err := io.ReadAll(rsp.Body)
AssertThat(t, err, Is{nil})
AssertThat(t, string(data), EqualTo{"test-data"})

Expand All @@ -848,7 +848,7 @@ func TestClipboard(t *testing.T) {
rsp, err := http.Get(With(srv.URL).Path(fmt.Sprintf("/clipboard/%s", sess["sessionId"])))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
data, err := ioutil.ReadAll(rsp.Body)
data, err := io.ReadAll(rsp.Body)
AssertThat(t, err, Is{nil})
AssertThat(t, string(data), EqualTo{"test-clipboard-value"})

Expand Down
3 changes: 1 addition & 2 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -223,7 +222,7 @@ func testConfig(env *service.Environment) *config.Config {
}

func testEnvironment() *service.Environment {
logOutputDir, _ = ioutil.TempDir("", "selenoid-test")
logOutputDir, _ = os.MkdirTemp("", "selenoid-test")
return &service.Environment{
CPU: int64(0),
Memory: int64(0),
Expand Down

0 comments on commit 417f0d1

Please sign in to comment.