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

Commit

Permalink
Merge pull request #1156 from Juneezee/deprecate-ioutil
Browse files Browse the repository at this point in the history
Move from io/ioutil to io and os packages
  • Loading branch information
vania-pooh authored Nov 20, 2021
2 parents a20a4a8 + 417f0d1 commit 57c9044
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 97 deletions.
9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package config

import (
"log"

"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"time"

"github.com/aerokube/selenoid/session"
"github.com/docker/docker/api/types/container"
"time"
)

// Session - session id and vnc flag
Expand Down Expand Up @@ -86,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
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ module github.com/aerokube/selenoid
go 1.17

require (
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e
github.com/aerokube/ggr v0.0.0-20181215175518-4a2e23fa1769
github.com/aerokube/util v0.0.0-20190701120823-161c21b50f69
github.com/aws/aws-sdk-go v1.20.12
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v0.7.3-0.20190629173937-e105a74c5419
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/imdario/mergo v0.3.6
github.com/mafredri/cdp v0.21.0
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
github.com/pkg/errors v0.8.1
golang.org/x/net v0.0.0-20190311183353-d8887717615a
)

require (
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/mafredri/cdp v0.21.0
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.2 // indirect
golang.org/x/net v0.0.0-20190311183353-d8887717615a
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 // indirect
google.golang.org/grpc v1.21.1 // indirect
Expand Down
13 changes: 5 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,31 @@ import (
"context"
"encoding/json"
"flag"
"github.com/aerokube/selenoid/jsonerror"
"github.com/pkg/errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"golang.org/x/net/websocket"

"fmt"

"path/filepath"

ggr "github.com/aerokube/ggr/config"
"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/jsonerror"
"github.com/aerokube/selenoid/protect"
"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/selenoid/upload"
"github.com/aerokube/util"
"github.com/aerokube/util/docker"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"golang.org/x/net/websocket"
)

var (
Expand Down
10 changes: 6 additions & 4 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//go:build metadata
// +build metadata

package main

import (
"encoding/json"
"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/session"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"

"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/session"
)

const metadataFileExtension = ".json"
Expand Down Expand Up @@ -37,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
2 changes: 1 addition & 1 deletion protect/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package protect

import (
"errors"
"github.com/aerokube/selenoid/jsonerror"
"log"
"math"
"net/http"
"time"

"github.com/aerokube/selenoid/jsonerror"
"github.com/aerokube/util"
)

Expand Down
14 changes: 8 additions & 6 deletions s3_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
//go:build s3
// +build s3

package main

import (
"context"
. "github.com/aandryashin/matchers"
"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/selenoid/upload"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

. "github.com/aandryashin/matchers"
"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/selenoid/upload"
)

var (
Expand Down Expand Up @@ -62,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
13 changes: 6 additions & 7 deletions selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/jsonerror"
"github.com/aerokube/selenoid/service"
"github.com/imdario/mergo"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand All @@ -28,10 +23,14 @@ import (
"sync"
"time"

"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/jsonerror"
"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/util"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stdcopy"
"github.com/imdario/mergo"
"golang.org/x/net/websocket"
)

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
32 changes: 15 additions & 17 deletions selenoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/rpcc"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/aerokube/selenoid/config"

"encoding/json"
"path/filepath"

. "github.com/aandryashin/matchers"
. "github.com/aandryashin/matchers/httpresp"
ggr "github.com/aerokube/ggr/config"
"github.com/aerokube/selenoid/config"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/rpcc"
)

var _ = func() bool {
Expand All @@ -36,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 @@ -654,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 @@ -722,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 @@ -745,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 @@ -762,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 @@ -789,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 @@ -824,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 @@ -850,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
8 changes: 4 additions & 4 deletions service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package service
import (
"context"
"fmt"
"github.com/docker/go-units"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/aerokube/selenoid/config"
Expand All @@ -20,9 +22,7 @@ import (
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
"os"
"path/filepath"
"strings"
"github.com/docker/go-units"
)

const (
Expand Down
Loading

0 comments on commit 57c9044

Please sign in to comment.