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

Commit

Permalink
Removed ioutil package completely
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Jul 25, 2023
1 parent 5d4bb07 commit a594384
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 38 deletions.
7 changes: 3 additions & 4 deletions build/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/markbates/pkger"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -54,12 +53,12 @@ func (bs *BrowserSource) Prepare() (string, string, error) {
if err != nil {
return "", "", fmt.Errorf("download file: %v", err)
}
f, err := ioutil.TempFile("", "images")
f, err := os.CreateTemp("", "images")
if err != nil {
return "", "", fmt.Errorf("temporary file: %v", err)
}
outputFileName := f.Name()
err = ioutil.WriteFile(outputFileName, data, 0644)
err = os.WriteFile(outputFileName, data, 0644)
if err != nil {
return "", "", fmt.Errorf("save downloaded file: %v", err)
}
Expand Down Expand Up @@ -131,7 +130,7 @@ func requireCommand(cmd string) bool {
}

func tmpDir() (string, error) {
dir, err := ioutil.TempDir("", "images")
dir, err := os.MkdirTemp("", "images")
if err != nil {
return "", fmt.Errorf("create temporary dir: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions build/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -95,7 +94,7 @@ func extractFile(data []byte, filename string, outputDir string) (string, error)
return untar(data, filename, outputDir)
} else {
outputPath := filepath.Join(outputDir, filename)
err := ioutil.WriteFile(outputPath, data, os.ModePerm)
err := os.WriteFile(outputPath, data, os.ModePerm)
if err != nil {
return "", fmt.Errorf("failed to save file %s: %v", outputPath, err)
}
Expand Down Expand Up @@ -214,7 +213,7 @@ func doSendGet(url string, token string) ([]byte, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unsuccessful response: %d %s", resp.StatusCode, resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response body: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions build/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package build

import (
. "github.com/aandryashin/matchers"
"io/ioutil"
"os"
"testing"
)
Expand All @@ -26,7 +25,7 @@ func TestUntar(t *testing.T) {
}

func withTmpDir(t *testing.T, prefix string, fn func(*testing.T, string)) {
dir, err := ioutil.TempDir("", prefix)
dir, err := os.MkdirTemp("", prefix)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -56,10 +55,9 @@ func testUnpack(t *testing.T, data []byte, fileName string, fn func([]byte, stri
}

func readFile(t *testing.T, fileName string) []byte {
data, err := ioutil.ReadFile(fileName)
data, err := os.ReadFile(fileName)
if err != nil {
t.Fatal(err)
}
return data
}

9 changes: 4 additions & 5 deletions build/firefox.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package build
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -106,12 +105,12 @@ func (c *Firefox) Build() error {
image.Labels = labels

browsersJsonFile := filepath.Join(image.Dir, "browsers.json")
data, err := ioutil.ReadFile(browsersJsonFile)
data, err := os.ReadFile(browsersJsonFile)
if err != nil {
return fmt.Errorf("failed to read browsers.json: %v", err)
}
newContents := strings.Replace(string(data), "@@VERSION@@", firefoxMajorMinorVersion, -1)
err = ioutil.WriteFile(browsersJsonFile, []byte(newContents), 0)
err = os.WriteFile(browsersJsonFile, []byte(newContents), 0)
if err != nil {
return fmt.Errorf("failed to update browsers.json: %v", err)
}
Expand Down Expand Up @@ -188,7 +187,7 @@ func (c *Firefox) downloadSelenoid(dir string) (string, error) {
return "", fmt.Errorf("download Selenoid: %v", err)
}
outputPath := filepath.Join(dir, "selenoid")
err = ioutil.WriteFile(outputPath, data, 0755)
err = os.WriteFile(outputPath, data, 0755)
if err != nil {
return "", fmt.Errorf("save Selenoid: %v", err)
}
Expand Down Expand Up @@ -219,7 +218,7 @@ func (c *Firefox) downloadSeleniumJAR(dir string) (string, error) {
return "", fmt.Errorf("download Selenium JAR: %v", err)
}
outputPath := filepath.Join(dir, "selenium-server-standalone.jar")
err = ioutil.WriteFile(outputPath, data, 0644)
err = os.WriteFile(outputPath, data, 0644)
if err != nil {
return "", fmt.Errorf("save Selenium JAR: %v", err)
}
Expand Down
20 changes: 17 additions & 3 deletions selenium/base/fileserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io/fs"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -52,7 +52,21 @@ func mux(dir string) http.Handler {
}

func listFilesAsJson(w http.ResponseWriter, dir string) {
files, err := ioutil.ReadDir(dir)

entries, err := os.ReadDir(dir)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
files := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
files = append(files, info)
}
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().After(files[j].ModTime())
})
Expand All @@ -65,7 +79,7 @@ func listFilesAsJson(w http.ResponseWriter, dir string) {
ret = append(ret, f.Name())
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(ret)
_ = json.NewEncoder(w).Encode(ret)
}

func deleteFileIfExists(w http.ResponseWriter, r *http.Request, dir string) {
Expand Down
9 changes: 4 additions & 5 deletions selenium/base/fileserver/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
. "github.com/aandryashin/matchers"
. "github.com/aandryashin/matchers/httpresp"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -17,7 +16,7 @@ var (
)

func init() {
dir, _ = ioutil.TempDir("", "fileserver")
dir, _ = os.MkdirTemp("", "fileserver")
srv = httptest.NewServer(mux(dir))
}

Expand All @@ -26,8 +25,8 @@ func withUrl(path string) string {
}

func TestDownloadAndRemoveFile(t *testing.T) {
tempFile, _ := ioutil.TempFile(dir, "fileserver")
ioutil.WriteFile(tempFile.Name(), []byte("test-data"), 0644)
tempFile, _ := os.CreateTemp(dir, "fileserver")
_ = os.WriteFile(tempFile.Name(), []byte("test-data"), 0644)
tempFileName := filepath.Base(tempFile.Name())
resp, err := http.Get(withUrl("/" + tempFileName))
AssertThat(t, err, Is{nil})
Expand All @@ -42,7 +41,7 @@ func TestDownloadAndRemoveFile(t *testing.T) {
AssertThat(t, rsp, IsJson{&files})
AssertThat(t, files, EqualTo{[]string{tempFileName}})

req, _ := http.NewRequest(http.MethodDelete, withUrl("/" + tempFileName), nil)
req, _ := http.NewRequest(http.MethodDelete, withUrl("/"+tempFileName), nil)
resp, err = http.DefaultClient.Do(req)
AssertThat(t, err, Is{nil})
AssertThat(t, resp, Code{200})
Expand Down
11 changes: 5 additions & 6 deletions static/chrome/devtools/devtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"github.com/mafredri/cdp/devtool"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
Expand All @@ -19,7 +18,7 @@ import (

const (
devtoolsBaseDir = "/tmp"
slash = "/"
slash = "/"
)

var (
Expand Down Expand Up @@ -79,9 +78,9 @@ func protocol(w http.ResponseWriter, r *http.Request) {
return
}
u := &url.URL{
Host: h,
Host: h,
Scheme: "http",
Path: "/json/protocol",
Path: "/json/protocol",
}
log.Printf("[PROTOCOL] [%s]", u.String())
(&httputil.ReverseProxy{
Expand Down Expand Up @@ -143,7 +142,7 @@ func devtoolsHost() (string, error) {

func androidDevtoolsHost() (string, error) {
const androidDevtoolsPort = 9333
cmd := exec.Command("adb", "forward", fmt.Sprintf("tcp:%d", androidDevtoolsPort), "localabstract:chrome_devtools_remote")
cmd := exec.Command("adb", "forward", fmt.Sprintf("tcp:%d", androidDevtoolsPort), "localabstract:chrome_devtools_remote")
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("failed to forward devtools port: %v", err)
Expand Down Expand Up @@ -175,7 +174,7 @@ func detectDevtoolsHost(baseDir string) string {
continue
}
portFile := filepath.Join(c, "DevToolsActivePort")
data, err := ioutil.ReadFile(portFile)
data, err := os.ReadFile(portFile)
if err != nil {
continue
}
Expand Down
7 changes: 3 additions & 4 deletions static/chrome/devtools/devtools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/target"
"github.com/mafredri/cdp/rpcc"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -165,12 +164,12 @@ func TestDevtools(t *testing.T) {
}

func TestDetectDevtoolsHost(t *testing.T) {
name, _ := ioutil.TempDir("", "devtools")
name, _ := os.MkdirTemp("", "devtools")
defer os.RemoveAll(name)
profilePath := filepath.Join(name, ".org.chromium.Chromium.deadbee")
os.MkdirAll(profilePath, os.ModePerm)
_ = os.MkdirAll(profilePath, os.ModePerm)
portFile := filepath.Join(profilePath, "DevToolsActivePort")
ioutil.WriteFile(portFile, []byte("12345\n/devtools/browser/6f37c7fe-a0a6-4346-a6e2-80c5da0687f0"), 0644)
_ = os.WriteFile(portFile, []byte("12345\n/devtools/browser/6f37c7fe-a0a6-4346-a6e2-80c5da0687f0"), 0644)

AssertThat(t, detectDevtoolsHost(name), EqualTo{"127.0.0.1:12345"})
}
Expand Down
2 changes: 1 addition & 1 deletion static/safari/cmd/prism/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/aerokube/images/prism

go 1.19
go 1.20
5 changes: 2 additions & 3 deletions static/safari/cmd/prism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -97,7 +96,7 @@ func main() {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
r.Body = ioutil.NopCloser(bytes.NewReader(body))
r.Body = io.NopCloser(bytes.NewReader(body))
r.ContentLength = int64(len(body))
}
(&httputil.ReverseProxy{
Expand Down Expand Up @@ -132,7 +131,7 @@ func main() {
resp.Header.Del("Server")
resp.Header.Del("Content-Length")
resp.ContentLength = int64(len(buf))
resp.Body = ioutil.NopCloser(bytes.NewReader(buf))
resp.Body = io.NopCloser(bytes.NewReader(buf))
return nil
},
}).ServeHTTP(w, r)
Expand Down

0 comments on commit a594384

Please sign in to comment.