Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint: ioutil is deprecated, I have the power of search and replace #4440

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ issues:
- ineffective break statement. Did you mean to break out of the outer loop
# revive: irrelevant error about naming
- "var-naming: don't use leading k in Go names"
# staticcheck: we'll keep using ioutil for now
- 'SA1019: "io/ioutil" has been deprecated since Go 1.16'

exclude-rules:
# Add all linters here -- Comment this block out for testing linters
Expand Down
3 changes: 1 addition & 2 deletions agreement/fuzzer/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
Expand Down Expand Up @@ -440,7 +439,7 @@ func TestFuzzer(t *testing.T) {
t.Run(testName, func(t *testing.T) {
partitiontest.PartitionTest(t) // Check if this expect test should by run, may SKIP
jsonFilename := jsonFiles[testName]
jsonBytes, err := ioutil.ReadFile(jsonFilename)
jsonBytes, err := os.ReadFile(jsonFilename)
require.NoError(t, err)
var fuzzerTest FuzzerTestFile
err = json.Unmarshal(jsonBytes, &fuzzerTest)
Expand Down
3 changes: 1 addition & 2 deletions cmd/algod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -119,7 +118,7 @@ func run() int {
}

// Load genesis
genesisText, err := ioutil.ReadFile(genesisPath)
genesisText, err := os.ReadFile(genesisPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read genesis file %s: %v\n", genesisPath, err)
return 1
Expand Down
5 changes: 2 additions & 3 deletions cmd/algod/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -30,15 +29,15 @@ import (

func BenchmarkAlgodStartup(b *testing.B) {
tmpDir := b.TempDir()
genesisFile, err := ioutil.ReadFile("../../installer/genesis/devnet/genesis.json")
genesisFile, err := os.ReadFile("../../installer/genesis/devnet/genesis.json")
require.NoError(b, err)

dataDirectory = &tmpDir
bInitAndExit := true
initAndExit = &bInitAndExit
b.StartTimer()
for n := 0; n < b.N; n++ {
err := ioutil.WriteFile(filepath.Join(tmpDir, config.GenesisJSONFile), genesisFile, 0766)
err := os.WriteFile(filepath.Join(tmpDir, config.GenesisJSONFile), genesisFile, 0766)
require.NoError(b, err)
fmt.Printf("file %s was written\n", filepath.Join(tmpDir, config.GenesisJSONFile))
run()
Expand Down
8 changes: 4 additions & 4 deletions cmd/algofix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"go/parser"
"go/scanner"
"go/token"
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -135,7 +135,7 @@ func processFile(filename string, useStdin bool) error {
defer f.Close()
}

src, err := ioutil.ReadAll(f)
src, err := io.ReadAll(f)
if err != nil {
return err
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func processFile(filename string, useStdin bool) error {
}

fixedSome = true
return ioutil.WriteFile(f.Name(), newSrc, 0)
return os.WriteFile(f.Name(), newSrc, 0)
}

var gofmtBuf bytes.Buffer
Expand Down Expand Up @@ -248,7 +248,7 @@ func isGoFile(f os.FileInfo) bool {
}

func writeTempFile(dir, prefix string, data []byte) (string, error) {
file, err := ioutil.TempFile(dir, prefix)
file, err := os.CreateTemp(dir, prefix)
if err != nil {
return "", err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/algofix/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -161,12 +160,12 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass
if err != nil {
return err
}
dir, err := ioutil.TempDir(os.TempDir(), "fix_cgo_typecheck")
dir, err := os.MkdirTemp(os.TempDir(), "fix_cgo_typecheck")
if err != nil {
return err
}
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "in.go"), txt, 0600)
err = os.WriteFile(filepath.Join(dir, "in.go"), txt, 0600)
if err != nil {
return err
}
Expand All @@ -175,7 +174,7 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass
if err != nil {
return err
}
out, err := ioutil.ReadFile(filepath.Join(dir, "_cgo_gotypes.go"))
out, err := os.ReadFile(filepath.Join(dir, "_cgo_gotypes.go"))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/algoh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -351,8 +350,8 @@ func captureErrorLogs(algohConfig algoh.HostConfig, errorOutput stdCollector, ou
log.EventWithDetails(telemetryspec.HostApplicationState, telemetryspec.ErrorOutputEvent, details)

// Write stdout & stderr streams to disk
_ = ioutil.WriteFile(filepath.Join(absolutePath, nodecontrol.StdOutFilename), []byte(output.output), os.ModePerm)
_ = ioutil.WriteFile(filepath.Join(absolutePath, nodecontrol.StdErrFilename), []byte(errorOutput.output), os.ModePerm)
_ = os.WriteFile(filepath.Join(absolutePath, nodecontrol.StdOutFilename), []byte(output.output), os.ModePerm)
_ = os.WriteFile(filepath.Join(absolutePath, nodecontrol.StdErrFilename), []byte(errorOutput.output), os.ModePerm)
}
if errorCondition && algohConfig.UploadOnError {
fmt.Fprintf(os.Stdout, "Uploading logs...\n")
Expand Down
18 changes: 9 additions & 9 deletions cmd/algokey/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

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

"github.com/algorand/go-algorand/crypto"
Expand Down Expand Up @@ -63,7 +63,7 @@ func loadMnemonic(mnemonic string) crypto.Seed {
}

func loadKeyfile(keyfile string) crypto.Seed {
seedbytes, err := ioutil.ReadFile(keyfile)
seedbytes, err := os.ReadFile(keyfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read key seed from %s: %v\n", keyfile, err)
os.Exit(1)
Expand All @@ -75,7 +75,7 @@ func loadKeyfile(keyfile string) crypto.Seed {
}

func writePrivateKey(keyfile string, seed crypto.Seed) {
err := ioutil.WriteFile(keyfile, seed[:], 0600)
err := os.WriteFile(keyfile, seed[:], 0600)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot write key to %s: %v\n", keyfile, err)
os.Exit(1)
Expand All @@ -84,7 +84,7 @@ func writePrivateKey(keyfile string, seed crypto.Seed) {

func writePublicKey(pubkeyfile string, checksummed string) {
data := fmt.Sprintf("%s\n", checksummed)
err := ioutil.WriteFile(pubkeyfile, []byte(data), 0666)
err := os.WriteFile(pubkeyfile, []byte(data), 0666)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot write public key to %s: %v\n", pubkeyfile, err)
os.Exit(1)
Expand All @@ -100,7 +100,7 @@ func computeMnemonic(seed crypto.Seed) string {
return mnemonic
}

// writeFile is a wrapper of ioutil.WriteFile which considers the special
// writeFile is a wrapper of os.WriteFile which considers the special
// case of stdout filename
func writeFile(filename string, data []byte, perm os.FileMode) error {
var err error
Expand All @@ -111,14 +111,14 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
}
return nil
}
return ioutil.WriteFile(filename, data, perm)
return os.WriteFile(filename, data, perm)
}

// readFile is a wrapper of ioutil.ReadFile which considers the
// readFile is a wrapper of os.ReadFile which considers the
// special case of stdin filename
func readFile(filename string) ([]byte, error) {
if filename == stdinFileNameValue {
return ioutil.ReadAll(os.Stdin)
return io.ReadAll(os.Stdin)
}
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}
3 changes: 1 addition & 2 deletions cmd/algokey/keyreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -248,7 +247,7 @@ func run(params keyregCmdParams) error {
return fmt.Errorf("failed to write transaction to stdout: %w", err)
}
} else {
if err = ioutil.WriteFile(params.txFile, data, 0600); err != nil {
if err = os.WriteFile(params.txFile, data, 0600); err != nil {
return fmt.Errorf("failed to write transaction to '%s': %w", params.txFile, err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/algokey/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -66,7 +65,7 @@ var multisigCmd = &cobra.Command{
seed := loadKeyfileOrMnemonic(multisigKeyfile, multisigMnemonic)
key := crypto.GenerateSignatureSecrets(seed)

txdata, err := ioutil.ReadFile(multisigTxfile)
txdata, err := os.ReadFile(multisigTxfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read transactions from %s: %v\n", multisigTxfile, err)
os.Exit(1)
Expand Down Expand Up @@ -101,7 +100,7 @@ var multisigCmd = &cobra.Command{
outBytes = append(outBytes, protocol.Encode(&stxn)...)
}

err = ioutil.WriteFile(multisigOutfile, outBytes, 0600)
err = os.WriteFile(multisigOutfile, outBytes, 0600)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot write signed transactions to %s: %v\n", multisigOutfile, err)
os.Exit(1)
Expand Down
5 changes: 2 additions & 3 deletions cmd/algokey/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,7 +51,7 @@ var signCmd = &cobra.Command{
seed := loadKeyfileOrMnemonic(signKeyfile, signMnemonic)
key := crypto.GenerateSignatureSecrets(seed)

txdata, err := ioutil.ReadFile(signTxfile)
txdata, err := os.ReadFile(signTxfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read transactions from %s: %v\n", signTxfile, err)
os.Exit(1)
Expand All @@ -78,7 +77,7 @@ var signCmd = &cobra.Command{
outBytes = append(outBytes, protocol.Encode(&stxn)...)
}

err = ioutil.WriteFile(signOutfile, outBytes, 0600)
err = os.WriteFile(signOutfile, outBytes, 0600)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot write signed transactions to %s: %v\n", signOutfile, err)
os.Exit(1)
Expand Down
3 changes: 1 addition & 2 deletions cmd/algons/dnsCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"net"
"os"
"regexp"
Expand Down Expand Up @@ -477,7 +476,7 @@ func doExportZone(network string, outputFilename string) bool {
return false
}
if outputFilename != "" {
err = ioutil.WriteFile(outputFilename, exportedZone, 0666)
err = os.WriteFile(outputFilename, exportedZone, 0666)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to write exported zone file : %v\n", err)
return false
Expand Down
15 changes: 7 additions & 8 deletions cmd/buildtools/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -102,7 +101,7 @@ var timestampCmd = &cobra.Command{
// Write out the genesis file in the same way we do to generate originally
// (see gen/generate.go)
jsonData := protocol.EncodeJSON(genesis)
err = ioutil.WriteFile(timestampFile, append(jsonData, '\n'), 0666)
err = os.WriteFile(timestampFile, append(jsonData, '\n'), 0666)
if err != nil {
reportErrorf("Error saving genesis file '%s': %v\n", timestampFile, err)
}
Expand All @@ -117,7 +116,7 @@ var dumpGenesisIDCmd = &cobra.Command{
Short: "Dump the genesis ID for the specified genesis file",
Run: func(cmd *cobra.Command, args []string) {
// Load genesis
genesisText, err := ioutil.ReadFile(genesisFile)
genesisText, err := os.ReadFile(genesisFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read genesis file %s: %v\n", genesisFile, err)
os.Exit(1)
Expand All @@ -139,7 +138,7 @@ var dumpGenesisHashCmd = &cobra.Command{
Short: "Dump the genesis Hash for the specified genesis file",
Run: func(cmd *cobra.Command, args []string) {
// Load genesis
genesisText, err := ioutil.ReadFile(genesisFile)
genesisText, err := os.ReadFile(genesisFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read genesis file %s: %v\n", genesisFile, err)
os.Exit(1)
Expand Down Expand Up @@ -206,7 +205,7 @@ var ensureCmd = &cobra.Command{
} else {
// Write source genesis (now updated with release timestamp, if applicable)
jsonData := protocol.EncodeJSON(sourceGenesis)
err = ioutil.WriteFile(targetFile, jsonData, 0666)
err = os.WriteFile(targetFile, jsonData, 0666)
if err != nil {
reportErrorf("Error writing target genesis file '%s': %v\n", targetFile, err)
}
Expand All @@ -231,13 +230,13 @@ func ensureReleaseGenesis(src bookkeeping.Genesis, releaseFile string) (err erro

releaseGenesis = src
jsonData := protocol.EncodeJSON(releaseGenesis)
err = ioutil.WriteFile(releaseFile, jsonData, 0666)
err = os.WriteFile(releaseFile, jsonData, 0666)
if err != nil {
return fmt.Errorf("error saving file: %v", err)
}

hash := releaseGenesis.Hash()
err = ioutil.WriteFile(releaseFileHash, []byte(hash.String()), 0666)
err = os.WriteFile(releaseFileHash, []byte(hash.String()), 0666)
if err != nil {
return fmt.Errorf("error saving hash file '%s': %v", releaseFileHash, err)
}
Expand Down Expand Up @@ -278,7 +277,7 @@ func verifyGenesisHashes(src, release bookkeeping.Genesis, hashFile string) (err
return fmt.Errorf("source and release hashes differ - genesis.json may have diverge from released version")
}

relHashBytes, err := ioutil.ReadFile(hashFile)
relHashBytes, err := os.ReadFile(hashFile)
if err != nil {
return fmt.Errorf("error loading release hash file '%s'", hashFile)
}
Expand Down
Loading