Skip to content

Commit

Permalink
fix: replace deprecated package io/ioutil with os (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulbqb authored Dec 6, 2022
1 parent 280514c commit a990fab
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 46 deletions.
4 changes: 2 additions & 2 deletions abci/example/kvstore/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kvstore

import (
"fmt"
"io/ioutil"
"os"

"github.com/line/ostracon/abci/types"
"github.com/line/ostracon/crypto"
Expand All @@ -18,7 +18,7 @@ func LoadPrivValidatorKeyFile(keyFilePath string) (*privval.FilePVKey, error) {
if !tmos.FileExists(keyFilePath) {
return nil, fmt.Errorf("private validator file %s does not exist", keyFilePath)
}
keyJSONBytes, _ := ioutil.ReadFile(keyFilePath)
keyJSONBytes, _ := os.ReadFile(keyFilePath)
pvKey := privval.FilePVKey{}
err := tmjson.Unmarshal(keyJSONBytes, &pvKey)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/ostracon/commands/debug/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package debug
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -82,7 +81,7 @@ func dumpCmdHandler(_ *cobra.Command, args []string) error {
func dumpDebugData(outDir string, conf *cfg.Config, rpc *rpchttp.HTTP) {
start := time.Now().UTC()

tmpDir, err := ioutil.TempDir(outDir, "ostracon_debug_tmp")
tmpDir, err := os.MkdirTemp(outDir, "ostracon_debug_tmp")
if err != nil {
logger.Error("failed to create temporary directory", "dir", tmpDir, "error", err)
return
Expand Down
3 changes: 1 addition & 2 deletions cmd/ostracon/commands/debug/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -111,5 +110,5 @@ func writeStateJSONToFile(state interface{}, dir, filename string) error {
return fmt.Errorf("failed to encode state dump: %w", err)
}

return ioutil.WriteFile(path.Join(dir, filename), stateJSON, os.ModePerm)
return os.WriteFile(path.Join(dir, filename), stateJSON, os.ModePerm)
}
3 changes: 1 addition & 2 deletions cmd/ostracon/commands/debug/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package debug
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -56,7 +55,7 @@ func killCmdHandler(cmd *cobra.Command, args []string) error {

// Create a temporary directory which will contain all the state dumps and
// relevant files and directories that will be compressed into a file.
tmpDir, err := ioutil.TempDir(os.TempDir(), "ostracon_debug_tmp")
tmpDir, err := os.MkdirTemp(os.TempDir(), "ostracon_debug_tmp")
if err != nil {
return fmt.Errorf("failed to create temporary directory: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/ostracon/commands/debug/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package debug
import (
"context"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -74,10 +74,10 @@ func dumpProfile(dir, addr, profile string, debug int) error {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read %s profile response body: %w", profile, err)
}

return ioutil.WriteFile(path.Join(dir, fmt.Sprintf("%s.out", profile)), body, os.ModePerm)
return os.WriteFile(path.Join(dir, fmt.Sprintf("%s.out", profile)), body, os.ModePerm)
}
4 changes: 2 additions & 2 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -543,7 +543,7 @@ func ResetTestRoot(testName string) *Config {

func ResetTestRootWithChainID(testName string, chainID string) *Config {
// create a unique, concurrency-safe test directory under os.TempDir()
rootDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s_", chainID, testName))
rootDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s_", chainID, testName))
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package consensus
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"runtime/debug"
"sort"
Expand Down Expand Up @@ -1997,7 +1997,7 @@ func (cs *State) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (add
)
}
if added && cs.ProposalBlockParts.IsComplete() {
bz, err := ioutil.ReadAll(cs.ProposalBlockParts.GetReader())
bz, err := io.ReadAll(cs.ProposalBlockParts.GetReader())
if err != nil {
return added, err
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/armor/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package armor
import (
"bytes"
"fmt"
"io/ioutil"
"io"

"golang.org/x/crypto/openpgp/armor" //nolint: staticcheck // nobody uses this package but leave for a moment
)
Expand Down Expand Up @@ -31,7 +31,7 @@ func DecodeArmor(armorStr string) (blockType string, headers map[string]string,
if err != nil {
return "", nil, nil, err
}
data, err = ioutil.ReadAll(block.Body)
data, err = io.ReadAll(block.Body)
if err != nil {
return "", nil, nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions libs/cli/helper.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 All @@ -19,7 +18,7 @@ func WriteConfigVals(dir string, vals map[string]string) error {
data += fmt.Sprintf("%s = \"%s\"\n", k, v)
}
cfile := filepath.Join(dir, "config.toml")
return ioutil.WriteFile(cfile, []byte(data), 0600)
return os.WriteFile(cfile, []byte(data), 0600)
}

// RunWithArgs executes the given command with the specified command line args
Expand Down
7 changes: 3 additions & 4 deletions libs/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -60,11 +59,11 @@ func FileExists(filePath string) bool {
}

func ReadFile(filePath string) ([]byte, error) {
return ioutil.ReadFile(filePath)
return os.ReadFile(filePath)
}

func MustReadFile(filePath string) []byte {
fileBytes, err := ioutil.ReadFile(filePath)
fileBytes, err := os.ReadFile(filePath)
if err != nil {
Exit(fmt.Sprintf("MustReadFile failed: %v", err))
return nil
Expand All @@ -73,7 +72,7 @@ func MustReadFile(filePath string) []byte {
}

func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
return ioutil.WriteFile(filePath, contents, mode)
return os.WriteFile(filePath, contents, mode)
}

func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
Expand Down
6 changes: 3 additions & 3 deletions p2p/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"os"

"github.com/line/ostracon/crypto"
"github.com/line/ostracon/crypto/ed25519"
Expand Down Expand Up @@ -70,7 +70,7 @@ func LoadOrGenNodeKey(filePath string) (*NodeKey, error) {

// LoadNodeKey loads NodeKey located in filePath.
func LoadNodeKey(filePath string) (*NodeKey, error) {
jsonBytes, err := ioutil.ReadFile(filePath)
jsonBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand All @@ -88,7 +88,7 @@ func (nodeKey *NodeKey) SaveAs(filePath string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, jsonBytes, 0600)
err = os.WriteFile(filePath, jsonBytes, 0600)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/upnp/upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -316,7 +316,7 @@ func (n *upnpNAT) getExternalIPAddress() (info statusInfo, err error) {
return
}
var envelope Envelope
data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions privval/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"time"

Expand Down Expand Up @@ -206,7 +206,7 @@ func LoadFilePVEmptyState(keyFilePath, stateFilePath string) *FilePV {

// If loadState is true, we load from the stateFilePath. Otherwise, we use an empty LastSignState.
func loadFilePV(keyFilePath, stateFilePath string, loadState bool) *FilePV {
keyJSONBytes, err := ioutil.ReadFile(keyFilePath)
keyJSONBytes, err := os.ReadFile(keyFilePath)
if err != nil {
tmos.Exit(err.Error())
}
Expand All @@ -224,7 +224,7 @@ func loadFilePV(keyFilePath, stateFilePath string, loadState bool) *FilePV {
pvState := FilePVLastSignState{}

if loadState {
stateJSONBytes, err := ioutil.ReadFile(stateFilePath)
stateJSONBytes, err := os.ReadFile(stateFilePath)
if err != nil {
tmos.Exit(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions rpc/jsonrpc/client/http_json_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -222,7 +222,7 @@ func (c *Client) Call(

defer httpResponse.Body.Close()

responseBytes, err := ioutil.ReadAll(httpResponse.Body)
responseBytes, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (c *Client) sendBatch(ctx context.Context, requests []*jsonRPCBufferedReque

defer httpResponse.Body.Close()

responseBytes, err := ioutil.ReadAll(httpResponse.Body)
responseBytes, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, fmt.Errorf("read response body: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/jsonrpc/client/http_uri_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -76,7 +76,7 @@ func (c *URIClient) Call(ctx context.Context, method string,
}
defer resp.Body.Close()

responseBytes, err := ioutil.ReadAll(resp.Body)
responseBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response body: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/jsonrpc/server/http_json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"reflect"
"sort"
Expand All @@ -19,7 +19,7 @@ import (
// jsonrpc calls grab the given method's function info and runs reflect.Call
func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
res := types.RPCInvalidRequestError(nil,
fmt.Errorf("error reading request body: %w", err),
Expand Down
4 changes: 2 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/line/ostracon/crypto"
Expand Down Expand Up @@ -340,7 +340,7 @@ func MakeGenesisStateFromFile(genDocFile string) (State, error) {

// MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
genDocJSON, err := ioutil.ReadFile(genDocFile)
genDocJSON, err := os.ReadFile(genDocFile)
if err != nil {
return nil, fmt.Errorf("couldn't read GenesisDoc file: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions statesync/chunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package statesync
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -42,7 +41,7 @@ type chunkQueue struct {
// newChunkQueue creates a new chunk queue for a snapshot, using a temp dir for storage.
// Callers must call Close() when done.
func newChunkQueue(snapshot *snapshot, tempDir string) (*chunkQueue, error) {
dir, err := ioutil.TempDir(tempDir, "oc-statesync")
dir, err := os.MkdirTemp(tempDir, "oc-statesync")
if err != nil {
return nil, fmt.Errorf("unable to create temp dir for state sync chunks: %w", err)
}
Expand Down Expand Up @@ -84,7 +83,7 @@ func (q *chunkQueue) Add(chunk *chunk) (bool, error) {
}

path := filepath.Join(q.dir, strconv.FormatUint(uint64(chunk.Index), 10))
err := ioutil.WriteFile(path, chunk.Chunk, 0600)
err := os.WriteFile(path, chunk.Chunk, 0600)
if err != nil {
return false, fmt.Errorf("failed to save chunk %v to file %v: %w", chunk.Index, path, err)
}
Expand Down Expand Up @@ -209,7 +208,7 @@ func (q *chunkQueue) load(index uint32) (*chunk, error) {
if !ok {
return nil, nil
}
body, err := ioutil.ReadFile(path)
body, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to load chunk %v: %w", index, err)
}
Expand Down
4 changes: 2 additions & 2 deletions types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/line/ostracon/crypto"
Expand Down Expand Up @@ -154,7 +154,7 @@ func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {

// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
jsonBlob, err := ioutil.ReadFile(genDocFile)
jsonBlob, err := os.ReadFile(genDocFile)
if err != nil {
return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
}
Expand Down

0 comments on commit a990fab

Please sign in to comment.