Skip to content

Commit

Permalink
Merge pull request #7765 from ErikEk/retire-io-ioutil-dep
Browse files Browse the repository at this point in the history
Remove io/ioutil package dependency
  • Loading branch information
guggero committed Apr 25, 2024
2 parents dcd8e16 + 0d94131 commit 2089a88
Show file tree
Hide file tree
Showing 46 changed files with 101 additions and 128 deletions.
5 changes: 2 additions & 3 deletions cert/selfsigned.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"os"
Expand Down Expand Up @@ -295,14 +294,14 @@ func GenCertPair(org string, tlsExtraIPs, tlsExtraDomains []string,
func WriteCertPair(certFile, keyFile string, certBytes, keyBytes []byte) error {
// Write cert and key files.
if certFile != "" {
err := ioutil.WriteFile(certFile, certBytes, 0644)
err := os.WriteFile(certFile, certBytes, 0644)
if err != nil {
return err
}
}

if keyFile != "" {
err := ioutil.WriteFile(keyFile, keyBytes, 0600)
err := os.WriteFile(keyFile, keyBytes, 0600)
if err != nil {
os.Remove(certFile)
return err
Expand Down
18 changes: 9 additions & 9 deletions cert/selfsigned_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cert_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -42,10 +42,10 @@ func TestIsOutdatedCert(t *testing.T) {
// number of IPs and domains.
for numIPs := 1; numIPs <= len(extraIPs); numIPs++ {
for numDomains := 1; numDomains <= len(extraDomains); numDomains++ {
certBytes, err := ioutil.ReadFile(certPath)
certBytes, err := os.ReadFile(certPath)
require.NoError(t, err)

keyBytes, err := ioutil.ReadFile(keyPath)
keyBytes, err := os.ReadFile(keyPath)
require.NoError(t, err)

_, parsedCert, err := cert.LoadCertFromBytes(
Expand Down Expand Up @@ -98,10 +98,10 @@ func TestIsOutdatedPermutation(t *testing.T) {
err = cert.WriteCertPair(certPath, keyPath, certBytes, keyBytes)
require.NoError(t, err)

certBytes, err = ioutil.ReadFile(certPath)
certBytes, err = os.ReadFile(certPath)
require.NoError(t, err)

keyBytes, err = ioutil.ReadFile(keyPath)
keyBytes, err = os.ReadFile(keyPath)
require.NoError(t, err)

_, parsedCert, err := cert.LoadCertFromBytes(certBytes, keyBytes)
Expand Down Expand Up @@ -171,10 +171,10 @@ func TestTLSDisableAutofill(t *testing.T) {
require.NoError(t, err)

// Read certs from disk.
certBytes, err = ioutil.ReadFile(certPath)
certBytes, err = os.ReadFile(certPath)
require.NoError(t, err)

keyBytes, err = ioutil.ReadFile(keyPath)
keyBytes, err = os.ReadFile(keyPath)
require.NoError(t, err)

// Load the certificate.
Expand Down Expand Up @@ -230,10 +230,10 @@ func TestTLSConfig(t *testing.T) {
err = cert.WriteCertPair(certPath, keyPath, certBytes, keyBytes)
require.NoError(t, err)

certBytes, err = ioutil.ReadFile(certPath)
certBytes, err = os.ReadFile(certPath)
require.NoError(t, err)

keyBytes, err = ioutil.ReadFile(keyPath)
keyBytes, err = os.ReadFile(keyPath)
require.NoError(t, err)

// Load the certificate.
Expand Down
6 changes: 3 additions & 3 deletions cert/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cert
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"os"
"sync"
)

Expand Down Expand Up @@ -31,11 +31,11 @@ var (
func GetCertBytesFromPath(certPath, keyPath string) (certBytes,
keyBytes []byte, err error) {

certBytes, err = ioutil.ReadFile(certPath)
certBytes, err = os.ReadFile(certPath)
if err != nil {
return nil, nil, err
}
keyBytes, err = ioutil.ReadFile(keyPath)
keyBytes, err = os.ReadFile(keyPath)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions chainreg/chainregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -547,7 +547,7 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) {
if err != nil {
return nil, nil, err
}
rpcCert, err = ioutil.ReadAll(certFile)
rpcCert, err = io.ReadAll(certFile)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions chanbackup/backupfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chanbackup

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

Expand Down Expand Up @@ -138,7 +137,7 @@ func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) {
// Now that we've confirmed the target file is populated, we'll read
// all the contents of the file. This function ensures that file is
// always closed, even if we can't read the contents.
multiBytes, err := ioutil.ReadFile(b.fileName)
multiBytes, err := os.ReadFile(b.fileName)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions chanbackup/backupfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package chanbackup
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand All @@ -27,7 +26,7 @@ func assertBackupMatches(t *testing.T, filePath string,

t.Helper()

packedBackup, err := ioutil.ReadFile(filePath)
packedBackup, err := os.ReadFile(filePath)
require.NoError(t, err, "unable to test file")

if !bytes.Equal(packedBackup, currentBackup) {
Expand Down
3 changes: 1 addition & 2 deletions channeldb/migration/lnwire21/announcement_signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lnwire

import (
"io"
"io/ioutil"
)

// AnnounceSignatures is a direct message between two endpoints of a
Expand Down Expand Up @@ -66,7 +65,7 @@ func (a *AnnounceSignatures) Decode(r io.Reader, pver uint32) error {
// we'll collect the remainder into the ExtraOpaqueData field. If there
// aren't any bytes, then we'll snip off the slice to avoid carrying
// around excess capacity.
a.ExtraOpaqueData, err = ioutil.ReadAll(r)
a.ExtraOpaqueData, err = io.ReadAll(r)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions channeldb/migration/lnwire21/channel_announcement.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lnwire
import (
"bytes"
"io"
"io/ioutil"

"github.com/btcsuite/btcd/chaincfg/chainhash"
)
Expand Down Expand Up @@ -89,7 +88,7 @@ func (a *ChannelAnnouncement) Decode(r io.Reader, pver uint32) error {
// we'll collect the remainder into the ExtraOpaqueData field. If there
// aren't any bytes, then we'll snip off the slice to avoid carrying
// around excess capacity.
a.ExtraOpaqueData, err = ioutil.ReadAll(r)
a.ExtraOpaqueData, err = io.ReadAll(r)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions channeldb/migration/lnwire21/channel_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"

"github.com/btcsuite/btcd/chaincfg/chainhash"
)
Expand Down Expand Up @@ -160,7 +159,7 @@ func (a *ChannelUpdate) Decode(r io.Reader, pver uint32) error {
// we'll collect the remainder into the ExtraOpaqueData field. If there
// aren't any bytes, then we'll snip off the slice to avoid carrying
// around excess capacity.
a.ExtraOpaqueData, err = ioutil.ReadAll(r)
a.ExtraOpaqueData, err = io.ReadAll(r)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions channeldb/migration/lnwire21/node_announcement.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"image/color"
"io"
"io/ioutil"
"net"
"unicode/utf8"
)
Expand Down Expand Up @@ -127,7 +126,7 @@ func (a *NodeAnnouncement) Decode(r io.Reader, pver uint32) error {
// we'll collect the remainder into the ExtraOpaqueData field. If there
// aren't any bytes, then we'll snip off the slice to avoid carrying
// around excess capacity.
a.ExtraOpaqueData, err = ioutil.ReadAll(r)
a.ExtraOpaqueData, err = io.ReadAll(r)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/lncli/cmd_macaroon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -192,7 +192,7 @@ func bakeMacaroon(ctx *cli.Context) error {
// a file or write to the standard output using hex encoding.
switch {
case savePath != "":
err = ioutil.WriteFile(savePath, macBytes, 0644)
err = os.WriteFile(savePath, macBytes, 0644)
if err != nil {
return err
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func printMacaroon(ctx *cli.Context) error {
macPath := lncfg.CleanAndExpandPath(ctx.String("macaroon_file"))

// Load the specified macaroon file.
macBytes, err = ioutil.ReadFile(macPath)
macBytes, err = os.ReadFile(macPath)
if err != nil {
return fmt.Errorf("unable to read macaroon path %v: %v",
macPath, err)
Expand Down Expand Up @@ -441,7 +441,7 @@ func constrainMacaroon(ctx *cli.Context) error {
sourceMacFile := lncfg.CleanAndExpandPath(args.First())
args = args.Tail()

sourceMacBytes, err := ioutil.ReadFile(sourceMacFile)
sourceMacBytes, err := os.ReadFile(sourceMacFile)
if err != nil {
return fmt.Errorf("error trying to read source macaroon file "+
"%s: %v", sourceMacFile, err)
Expand Down Expand Up @@ -471,7 +471,7 @@ func constrainMacaroon(ctx *cli.Context) error {
}

// Now we can output the result.
err = ioutil.WriteFile(destMacFile, destMacBytes, 0644)
err = os.WriteFile(destMacFile, destMacBytes, 0644)
if err != nil {
return fmt.Errorf("error writing destination macaroon file "+
"%s: %v", destMacFile, err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/lncli/cmd_open_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -1016,7 +1015,7 @@ func readTerminalOrFile(quit chan struct{}) (string, error) {

// If it's a path to an existing file and it's small enough, let's try
// to read its content now.
content, err := ioutil.ReadFile(maybeFile)
content, err := os.ReadFile(maybeFile)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/lncli/cmd_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -987,7 +987,7 @@ func sendToRoute(ctx *cli.Context) error {
// The user is signalling that we should read stdin in order to parse
// the set of target routes.
case args.Present() && args.First() == "-":
b, err := ioutil.ReadAll(os.Stdin)
b, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/lncli/cmd_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -423,7 +422,7 @@ func profileAddMacaroon(ctx *cli.Context) error {

// Now load and possibly encrypt the macaroon file.
macPath := lncfg.CleanAndExpandPath(ctx.GlobalString("macaroonpath"))
macBytes, err := ioutil.ReadFile(macPath)
macBytes, err := os.ReadFile(macPath)
if err != nil {
return fmt.Errorf("unable to read macaroon path: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/lncli/cmd_walletunlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -712,7 +711,7 @@ func createWatchOnly(ctx *cli.Context) error {
}

jsonFile := lncfg.CleanAndExpandPath(ctx.Args().First())
jsonBytes, err := ioutil.ReadFile(jsonFile)
jsonBytes, err := os.ReadFile(jsonFile)
if err != nil {
return fmt.Errorf("error reading JSON from file %v: %v",
jsonFile, err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/lncli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"strconv"
Expand Down Expand Up @@ -2833,7 +2832,7 @@ func parseChanBackups(ctx *cli.Context) (*lnrpc.RestoreChanBackupRequest, error)
}, nil

case ctx.IsSet("multi_file"):
packedMulti, err := ioutil.ReadFile(ctx.String("multi_file"))
packedMulti, err := os.ReadFile(ctx.String("multi_file"))
if err != nil {
return nil, fmt.Errorf("unable to decode multi packed "+
"backup: %v", err)
Expand Down

0 comments on commit 2089a88

Please sign in to comment.