Skip to content

Commit

Permalink
refactor: 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 [1]. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

[1]: https://golang.org/doc/go1.16#ioutil
Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored and eandre committed Dec 19, 2022
1 parent 522092a commit 6660fff
Show file tree
Hide file tree
Showing 34 changed files with 61 additions and 87 deletions.
13 changes: 6 additions & 7 deletions cli/cmd/encore/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -201,11 +200,11 @@ func createApp(ctx context.Context, name, template string) (err error) {
gray.Printf("Downloaded template %s.\n", ex.Name)
} else {
// Set up files that we need when we don't have an example
if err := ioutil.WriteFile(filepath.Join(name, ".gitignore"), []byte("/.encore\n"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(name, ".gitignore"), []byte("/.encore\n"), 0644); err != nil {
fatal(err)
}
encoreModData := []byte("module encore.app\n")
if err := ioutil.WriteFile(filepath.Join(name, "go.mod"), encoreModData, 0644); err != nil {
if err := os.WriteFile(filepath.Join(name, "go.mod"), encoreModData, 0644); err != nil {
fatal(err)
}
}
Expand Down Expand Up @@ -241,7 +240,7 @@ func createApp(ctx context.Context, name, template string) (err error) {
}
`)
}
if err := ioutil.WriteFile(filepath.Join(name, "encore.app"), encoreAppData, 0644); err != nil {
if err := os.WriteFile(filepath.Join(name, "encore.app"), encoreAppData, 0644); err != nil {
return err
}

Expand Down Expand Up @@ -505,7 +504,7 @@ func slurpJSON(req *http.Request, respData interface{}) error {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("got non-200 response: %s: %s", resp.Status, body)
}
if err := json.NewDecoder(resp.Body).Decode(respData); err != nil {
Expand Down Expand Up @@ -589,7 +588,7 @@ func addEncoreRemote(root, appID string) {
func linkApp(appID string, force bool) {
root, _ := determineAppRoot()
filePath := filepath.Join(root, "encore.app")
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
fatal(err)
os.Exit(1)
Expand Down Expand Up @@ -652,7 +651,7 @@ func linkApp(appID string, force bool) {
}

val.Format()
if err := ioutil.WriteFile(filePath, val.Pack(), 0644); err != nil {
if err := os.WriteFile(filePath, val.Pack(), 0644); err != nil {
fatal(err)
os.Exit(1)
}
Expand Down
3 changes: 1 addition & 2 deletions cli/cmd/encore/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"

Expand Down Expand Up @@ -78,7 +77,7 @@ Supported language codes are:
if output == "" {
os.Stdout.Write(resp.Code)
} else {
if err := ioutil.WriteFile(output, resp.Code, 0755); err != nil {
if err := os.WriteFile(output, resp.Code, 0755); err != nil {
fatal(err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/encore/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"os"
"syscall"
"time"
Expand Down Expand Up @@ -64,7 +64,7 @@ Note that this strips trailing newlines from the secret value.`,
value = string(data)
fmt.Fprintln(os.Stderr)
} else {
data, err := ioutil.ReadAll(os.Stdin)
data, err := io.ReadAll(os.Stdin)
if err != nil {
fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cli/daemon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package daemon

import (
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (s *Server) OnError(r *run.Run, err *errlist.List) {
// parseApp parses the app.
func (s *Server) parseApp(appRoot, workingDir string, parseTests bool) (*parser.Result, error) {
modPath := filepath.Join(appRoot, "go.mod")
modData, err := ioutil.ReadFile(modPath)
modData, err := os.ReadFile(modPath)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cli/daemon/dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -179,7 +178,7 @@ func (h *handler) apiCall(ctx context.Context, reply jsonrpc2.Replier, p *apiCal
log.Error().Err(err).Msg("dash: api call failed")
return reply(ctx, nil, err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()

// Encode the body back into a Go style struct
Expand Down
4 changes: 2 additions & 2 deletions cli/daemon/engine/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package runtime
import (
"encoding/base64"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -52,7 +52,7 @@ func (s *server) RecordTrace(w http.ResponseWriter, req *http.Request) {
return
}

data, err := ioutil.ReadAll(req.Body)
data, err := io.ReadAll(req.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
3 changes: 1 addition & 2 deletions cli/daemon/internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -68,7 +67,7 @@ func ReadOrCreate(appRoot string) (mf *Manifest, err error) {
out, _ := json.Marshal(&man)
if err := os.MkdirAll(filepath.Dir(cfgPath), 0755); err != nil {
return nil, err
} else if err := ioutil.WriteFile(cfgPath, out, 0644); err != nil {
} else if err := os.WriteFile(cfgPath, out, 0644); err != nil {
return nil, err
}
return &man, nil
Expand Down
4 changes: 2 additions & 2 deletions cli/daemon/run/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package run

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
Expand Down Expand Up @@ -137,7 +137,7 @@ type parseAppParams struct {
// parseApp parses the app and returns the parse result.
func (mgr *Manager) parseApp(p parseAppParams) (*parser.Result, error) {
modPath := filepath.Join(p.App.Root(), "go.mod")
modData, err := ioutil.ReadFile(modPath)
modData, err := os.ReadFile(modPath)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cli/daemon/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
mathrand "math/rand"
"net"
"net/http"
Expand Down Expand Up @@ -483,7 +482,7 @@ func (r *Run) StartProc(params *StartProcParams) (p *Proc, err error) {
io.ReadCloser
io.Writer
}{
ReadCloser: ioutil.NopCloser(respRd),
ReadCloser: io.NopCloser(respRd),
Writer: reqWr,
}
p.Client, err = yamux.Client(rwc, yamux.DefaultConfig())
Expand Down
3 changes: 1 addition & 2 deletions cli/daemon/sqldb/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -61,7 +60,7 @@ func (src *src) ReadUp(version uint) (r io.ReadCloser, identifier string, err er
if err != nil {
return nil, "", err
}
return ioutil.NopCloser(bytes.NewReader(data)), m.Description, nil
return io.NopCloser(bytes.NewReader(data)), m.Description, nil
}

func (src *src) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
Expand Down
3 changes: 1 addition & 2 deletions cli/internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -70,7 +69,7 @@ func Check(ctx context.Context) (latestVersion *LatestVersion, err error) {
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("GET %s: responded with %s: %s", releaseAPI, resp.Status, body)
}

Expand Down
15 changes: 7 additions & 8 deletions compiler/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -186,7 +185,7 @@ func (b *builder) Build() (res *Result, err error) {
}
}()

b.workdir, err = ioutil.TempDir("", "encore-build")
b.workdir, err = os.MkdirTemp("", "encore-build")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -255,7 +254,7 @@ func (b *builder) endCodeGenTracker() error {
func (b *builder) parseApp() error {
defer b.trace("parse app")()
modPath := filepath.Join(b.appRoot, "go.mod")
modData, err := ioutil.ReadFile(modPath)
modData, err := os.ReadFile(modPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -365,16 +364,16 @@ func (b *builder) writeModFile() error {

modBytes := modfile.Format(b.modfile.Syntax)
dstGomod := filepath.Join(b.workdir, "go.mod")
return ioutil.WriteFile(dstGomod, modBytes, 0644)
return os.WriteFile(dstGomod, modBytes, 0644)
}

func (b *builder) writeSumFile() error {
defer b.trace("write sum file")()
appSum, err := ioutil.ReadFile(filepath.Join(b.appRoot, "go.sum"))
appSum, err := os.ReadFile(filepath.Join(b.appRoot, "go.sum"))
if err != nil && !os.IsNotExist(err) {
return err
}
runtimeSum, err := ioutil.ReadFile(filepath.Join(b.cfg.EncoreRuntimePath, "go.sum"))
runtimeSum, err := os.ReadFile(filepath.Join(b.cfg.EncoreRuntimePath, "go.sum"))
if err != nil {
return err
}
Expand All @@ -383,7 +382,7 @@ func (b *builder) writeSumFile() error {
}
data := append(appSum, runtimeSum...)
dstGosum := filepath.Join(b.workdir, "go.sum")
return ioutil.WriteFile(dstGosum, data, 0644)
return os.WriteFile(dstGosum, data, 0644)
}

func (b *builder) writePackages() error {
Expand Down Expand Up @@ -418,7 +417,7 @@ func (b *builder) buildMain() error {

overlayData, _ := json.Marshal(map[string]interface{}{"Replace": b.overlay})
overlayPath := filepath.Join(b.workdir, "overlay.json")
if err := ioutil.WriteFile(overlayPath, overlayData, 0644); err != nil {
if err := os.WriteFile(overlayPath, overlayData, 0644); err != nil {
return err
}

Expand Down
3 changes: 1 addition & 2 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package compiler_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -30,7 +29,7 @@ func TestCompile(t *testing.T) {
e.Setenv("HOME", home)
e.Setenv("GOFLAGS", "-modcacherw")
gomod := []byte("module test\n\nrequire encore.dev v0.17.0")
return ioutil.WriteFile(filepath.Join(e.WorkDir, "go.mod"), gomod, 0755)
return os.WriteFile(filepath.Join(e.WorkDir, "go.mod"), gomod, 0755)
},
})
}
Expand Down
7 changes: 3 additions & 4 deletions compiler/exec_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"go/ast"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -61,7 +60,7 @@ func (b *builder) ExecScript() (res *Result, err error) {
}
}()

b.workdir, err = ioutil.TempDir("", "encore-exec")
b.workdir, err = os.MkdirTemp("", "encore-exec")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -169,7 +168,7 @@ FileLoop:

name := filepath.Base(mainFile.Path)
dst := filepath.Join(dir, name)
if err := ioutil.WriteFile(dst, mainFile.Contents, 0644); err != nil {
if err := os.WriteFile(dst, mainFile.Contents, 0644); err != nil {
return err
}
b.addOverlay(mainFile.Path, dst)
Expand All @@ -183,7 +182,7 @@ func (b *builder) buildExecScript() error {

overlayData, _ := json.Marshal(map[string]interface{}{"Replace": b.overlay})
overlayPath := filepath.Join(b.workdir, "overlay.json")
if err := ioutil.WriteFile(overlayPath, overlayData, 0644); err != nil {
if err := os.WriteFile(overlayPath, overlayData, 0644); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ import (
"fmt"
jsoniter "github.com/json-iterator/go"
"io"
"io/ioutil"
"strconv"
)

Expand Down Expand Up @@ -283,7 +282,7 @@ func (e *Marshaller) setErr(msg, field string, err error) {
}

func (d *Marshaller) Body(body io.Reader) (payload []byte) {
payload, err := ioutil.ReadAll(body)
payload, err := io.ReadAll(body)
if err == nil && len(payload) == 0 {
d.setErr("missing request body", "request_body", fmt.Errorf("missing request body"))
} else if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ import (
"fmt"
jsoniter "github.com/json-iterator/go"
"io"
"io/ioutil"
"strconv"
)

Expand Down Expand Up @@ -283,7 +282,7 @@ func (e *Marshaller) setErr(msg, field string, err error) {
}

func (d *Marshaller) Body(body io.Reader) (payload []byte) {
payload, err := ioutil.ReadAll(body)
payload, err := io.ReadAll(body)
if err == nil && len(payload) == 0 {
d.setErr("missing request body", "request_body", fmt.Errorf("missing request body"))
} else if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ import (
"fmt"
jsoniter "github.com/json-iterator/go"
"io"
"io/ioutil"
)

// Marshaller is used to serialize request data into strings and deserialize response data from strings
Expand Down Expand Up @@ -268,7 +267,7 @@ func (e *Marshaller) setErr(msg, field string, err error) {
}

func (d *Marshaller) Body(body io.Reader) (payload []byte) {
payload, err := ioutil.ReadAll(body)
payload, err := io.ReadAll(body)
if err == nil && len(payload) == 0 {
d.setErr("missing request body", "request_body", fmt.Errorf("missing request body"))
} else if err != nil {
Expand Down
Loading

0 comments on commit 6660fff

Please sign in to comment.