From a5943842725a95837b8a92fb96b1b1637beac8cb Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Tue, 25 Jul 2023 11:44:20 +0300 Subject: [PATCH 1/2] Removed ioutil package completely --- build/base.go | 7 +++---- build/file.go | 5 ++--- build/file_test.go | 6 ++---- build/firefox.go | 9 ++++----- selenium/base/fileserver/main.go | 20 +++++++++++++++++--- selenium/base/fileserver/main_test.go | 9 ++++----- static/chrome/devtools/devtools.go | 11 +++++------ static/chrome/devtools/devtools_test.go | 7 +++---- static/safari/cmd/prism/go.mod | 2 +- static/safari/cmd/prism/main.go | 5 ++--- 10 files changed, 43 insertions(+), 38 deletions(-) diff --git a/build/base.go b/build/base.go index dbabe22ea..361d39216 100644 --- a/build/base.go +++ b/build/base.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/markbates/pkger" "io" - "io/ioutil" "log" "net" "net/http" @@ -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) } @@ -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) } diff --git a/build/file.go b/build/file.go index e7a949f38..4c7dcc285 100644 --- a/build/file.go +++ b/build/file.go @@ -11,7 +11,6 @@ import ( "fmt" "gopkg.in/cheggaaa/pb.v1" "io" - "io/ioutil" "log" "net/http" "os" @@ -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) } @@ -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) } diff --git a/build/file_test.go b/build/file_test.go index 0e4a4355c..5f04fdfe0 100644 --- a/build/file_test.go +++ b/build/file_test.go @@ -2,7 +2,6 @@ package build import ( . "github.com/aandryashin/matchers" - "io/ioutil" "os" "testing" ) @@ -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) } @@ -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 } - diff --git a/build/firefox.go b/build/firefox.go index b0b508b07..40cebbeb7 100644 --- a/build/firefox.go +++ b/build/firefox.go @@ -3,7 +3,6 @@ package build import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -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) } @@ -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) } @@ -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) } diff --git a/selenium/base/fileserver/main.go b/selenium/base/fileserver/main.go index 1790ab0ed..735ecdb16 100644 --- a/selenium/base/fileserver/main.go +++ b/selenium/base/fileserver/main.go @@ -3,7 +3,7 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io/fs" "log" "net/http" "os" @@ -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()) }) @@ -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) { diff --git a/selenium/base/fileserver/main_test.go b/selenium/base/fileserver/main_test.go index 465c4acc3..6193d71e6 100644 --- a/selenium/base/fileserver/main_test.go +++ b/selenium/base/fileserver/main_test.go @@ -3,7 +3,6 @@ package main import ( . "github.com/aandryashin/matchers" . "github.com/aandryashin/matchers/httpresp" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -17,7 +16,7 @@ var ( ) func init() { - dir, _ = ioutil.TempDir("", "fileserver") + dir, _ = os.MkdirTemp("", "fileserver") srv = httptest.NewServer(mux(dir)) } @@ -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}) @@ -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}) diff --git a/static/chrome/devtools/devtools.go b/static/chrome/devtools/devtools.go index 438c61f6d..7d3e9aacb 100644 --- a/static/chrome/devtools/devtools.go +++ b/static/chrome/devtools/devtools.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "github.com/mafredri/cdp/devtool" - "io/ioutil" "log" "net/http" "net/http/httputil" @@ -19,7 +18,7 @@ import ( const ( devtoolsBaseDir = "/tmp" - slash = "/" + slash = "/" ) var ( @@ -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{ @@ -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) @@ -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 } diff --git a/static/chrome/devtools/devtools_test.go b/static/chrome/devtools/devtools_test.go index 2938b9531..737c469c7 100644 --- a/static/chrome/devtools/devtools_test.go +++ b/static/chrome/devtools/devtools_test.go @@ -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" @@ -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"}) } diff --git a/static/safari/cmd/prism/go.mod b/static/safari/cmd/prism/go.mod index a8141c280..c257ddadb 100644 --- a/static/safari/cmd/prism/go.mod +++ b/static/safari/cmd/prism/go.mod @@ -1,3 +1,3 @@ module github.com/aerokube/images/prism -go 1.19 +go 1.20 diff --git a/static/safari/cmd/prism/main.go b/static/safari/cmd/prism/main.go index 2bafe4181..fd30d165a 100644 --- a/static/safari/cmd/prism/main.go +++ b/static/safari/cmd/prism/main.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "net/http/httputil" @@ -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{ @@ -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) From 3e626b1479a4e117eea54b841196ae20a4d301f2 Mon Sep 17 00:00:00 2001 From: Ivan Krutov Date: Tue, 25 Jul 2023 11:44:43 +0300 Subject: [PATCH 2/2] Bump Golang to 1.20.6 --- .github/workflows/build.yml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/test.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1526ebd7d..b016e0433 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,7 +14,7 @@ jobs: - name: Setup Golang uses: actions/setup-go@v4 with: - go-version: ~1.20.5 + go-version: ~1.20.6 - uses: actions/cache@v3 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index daa84eef6..3e56ecc16 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,7 +13,7 @@ jobs: - name: Setup Golang uses: actions/setup-go@v4 with: - go-version: ~1.20.5 + go-version: ~1.20.6 - uses: actions/cache@v3 with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3c7e33452..c5246375e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: - name: Setup Golang uses: actions/setup-go@v4 with: - go-version: ~1.20.5 + go-version: ~1.20.6 - uses: actions/cache@v3 with: