refactor: replace ioutil with os and io packages for file operations

This commit is contained in:
Fuad Hasan 2025-02-04 20:24:55 +06:00
parent 73d5942b08
commit 710cfe520b
No known key found for this signature in database
GPG key ID: 3FB5E37EEA838518
9 changed files with 14 additions and 17 deletions

View file

@ -3,7 +3,6 @@ package d2chaos_test
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime/debug" "runtime/debug"
@ -90,14 +89,14 @@ func TestD2Chaos(t *testing.T) {
func test(t *testing.T, textPath, text string) { func test(t *testing.T, textPath, text string) {
t.Logf("writing d2 to %v (%d bytes)", textPath, len(text)) t.Logf("writing d2 to %v (%d bytes)", textPath, len(text))
err := ioutil.WriteFile(textPath, []byte(text), 0644) err := os.WriteFile(textPath, []byte(text), 0644)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
goencText := fmt.Sprintf("%#v", text) goencText := fmt.Sprintf("%#v", text)
t.Logf("writing d2.goenc to %v (%d bytes)", textPath+".goenc", len(goencText)) t.Logf("writing d2.goenc to %v (%d bytes)", textPath+".goenc", len(goencText))
err = ioutil.WriteFile(textPath+".goenc", []byte(goencText), 0644) err = os.WriteFile(textPath+".goenc", []byte(goencText), 0644)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -55,7 +55,7 @@ func layoutCmd(ctx context.Context, ms *xmain.State, ps []d2plugin.Plugin) error
} }
} }
func themesCmd(ctx context.Context, ms *xmain.State) { func themesCmd(_ context.Context, ms *xmain.State) {
fmt.Fprintf(ms.Stdout, "Available themes:\n%s", d2themescatalog.CLIString()) fmt.Fprintf(ms.Stdout, "Available themes:\n%s", d2themescatalog.CLIString())
} }

View file

@ -17,9 +17,9 @@ import (
"sync" "sync"
"time" "time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
"oss.terrastruct.com/util-go/xbrowser" "oss.terrastruct.com/util-go/xbrowser"

View file

@ -3,7 +3,6 @@ package dark_theme_test
import ( import (
"context" "context"
"encoding/xml" "encoding/xml"
"io/ioutil"
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
@ -449,7 +448,7 @@ func run(t *testing.T, tc testCase) {
assert.Success(t, err) assert.Success(t, err)
err = os.MkdirAll(dataPath, 0755) err = os.MkdirAll(dataPath, 0755)
assert.Success(t, err) assert.Success(t, err)
err = ioutil.WriteFile(pathGotSVG, svgBytes, 0600) err = os.WriteFile(pathGotSVG, svgBytes, 0600)
assert.Success(t, err) assert.Success(t, err)
defer os.Remove(pathGotSVG) defer os.Remove(pathGotSVG)

View file

@ -13,7 +13,7 @@ import (
"testing" "testing"
"time" "time"
"nhooyr.io/websocket" "github.com/coder/websocket"
"oss.terrastruct.com/util-go/assert" "oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff" "oss.terrastruct.com/util-go/diff"

View file

@ -5,7 +5,6 @@ import (
_ "embed" _ "embed"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
stdlog "log" stdlog "log"
"os" "os"
"os/exec" "os/exec"
@ -104,7 +103,7 @@ func main() {
return err return err
} }
if info.IsDir() { if info.IsDir() {
files, err := ioutil.ReadDir(path) files, err := os.ReadDir(path)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -112,7 +111,7 @@ func main() {
var testFile os.FileInfo var testFile os.FileInfo
for _, f := range files { for _, f := range files {
if strings.HasSuffix(f.Name(), "exp.svg") { if strings.HasSuffix(f.Name(), "exp.svg") {
testFile = f testFile, _ = f.Info()
break break
} }
} }

2
go.mod generated
View file

@ -7,6 +7,7 @@ toolchain go1.23.0
require ( require (
github.com/PuerkitoBio/goquery v1.10.0 github.com/PuerkitoBio/goquery v1.10.0
github.com/alecthomas/chroma/v2 v2.14.0 github.com/alecthomas/chroma/v2 v2.14.0
github.com/coder/websocket v1.8.12
github.com/dop251/goja v0.0.0-20240927123429-241b342198c2 github.com/dop251/goja v0.0.0-20240927123429-241b342198c2
github.com/dsoprea/go-exif/v3 v3.0.1 github.com/dsoprea/go-exif/v3 v3.0.1
github.com/dsoprea/go-png-image-structure/v2 v2.0.0-20210512210324-29b889a6093d github.com/dsoprea/go-png-image-structure/v2 v2.0.0-20210512210324-29b889a6093d
@ -28,7 +29,6 @@ require (
golang.org/x/tools v0.25.0 golang.org/x/tools v0.25.0
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
gonum.org/v1/plot v0.14.0 gonum.org/v1/plot v0.14.0
nhooyr.io/websocket v1.8.17
oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896 oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896
) )

4
go.sum generated
View file

@ -17,6 +17,8 @@ github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/campoy/embedmd v1.0.0 h1:V4kI2qTJJLf4J29RzI/MAt2c3Bl4dQSYPuflzwFH2hY= github.com/campoy/embedmd v1.0.0 h1:V4kI2qTJJLf4J29RzI/MAt2c3Bl4dQSYPuflzwFH2hY=
github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX3MzVl8= github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX3MzVl8=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
@ -203,8 +205,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y=
nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896 h1:g752s1ECv9FD8GunFOZRGWzjeR0cr/TZdSsjAnFEmL8= oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896 h1:g752s1ECv9FD8GunFOZRGWzjeR0cr/TZdSsjAnFEmL8=
oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4= rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=

View file

@ -6,7 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"html" "html"
"io/ioutil" "io"
"mime" "mime"
"net/http" "net/http"
"net/url" "net/url"
@ -212,7 +212,7 @@ func httpGet(ctx context.Context, href string) ([]byte, string, error) {
return nil, "", fmt.Errorf("expected status 200 but got %d %s", resp.StatusCode, resp.Status) return nil, "", fmt.Errorf("expected status 200 but got %d %s", resp.StatusCode, resp.Status)
} }
r := http.MaxBytesReader(nil, resp.Body, maxImageSize) r := http.MaxBytesReader(nil, resp.Body, maxImageSize)
buf, err := ioutil.ReadAll(r) buf, err := io.ReadAll(r)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }