From 9cffc45f0347e5c8b801c151955fa9326b9b2ba7 Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Wed, 17 Aug 2022 16:19:45 +0200 Subject: [PATCH] go: removed io/ioutil Since the minimum required version of Go has been bumped to 1.16, the deprecation of io/ioutil can now be acted upon. This Commit removes the remaining dependencies on ioutil and replaces them with their io or os counterparts. Signed-off-by: Moritz Poldrack Acked-by: Robin Jarry --- commands/account/recover.go | 4 ++-- commands/msg/forward.go | 3 +-- commands/msgview/open.go | 3 +-- completer/completer.go | 3 +-- config/config.go | 5 ++--- lib/crypto/gpg/gpgbin/decrypt.go | 3 +-- lib/crypto/gpg/gpgbin/verify.go | 5 ++--- lib/crypto/pgp/pgp.go | 3 +-- lib/messageview.go | 3 +-- lib/parse/hyperlinks_test.go | 4 ++-- logging/logger.go | 4 ++-- widgets/compose.go | 5 ++--- worker/lib/parse_test.go | 5 ++--- worker/lib/search.go | 6 +++--- worker/maildir/search.go | 6 +++--- worker/maildir/worker.go | 3 +-- worker/mbox/io.go | 3 +-- worker/mbox/models.go | 5 ++--- worker/mbox/worker.go | 4 ++-- worker/notmuch/worker.go | 4 ++-- 20 files changed, 34 insertions(+), 47 deletions(-) diff --git a/commands/account/recover.go b/commands/account/recover.go index 7a1100e..682a99c 100644 --- a/commands/account/recover.go +++ b/commands/account/recover.go @@ -3,7 +3,7 @@ package account import ( "bytes" "errors" - "io/ioutil" + "io" "os" "path/filepath" @@ -89,7 +89,7 @@ func (Recover) Execute(aerc *widgets.Aerc, args []string) error { return nil, err } defer recoverFile.Close() - data, err := ioutil.ReadAll(recoverFile) + data, err := io.ReadAll(recoverFile) if err != nil { return nil, err } diff --git a/commands/msg/forward.go b/commands/msg/forward.go index d89667a..040360e 100644 --- a/commands/msg/forward.go +++ b/commands/msg/forward.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math/rand" "os" "path" @@ -124,7 +123,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error { } if attachFull { - tmpDir, err := ioutil.TempDir("", "aerc-tmp-attachment") + tmpDir, err := os.MkdirTemp("", "aerc-tmp-attachment") if err != nil { return err } diff --git a/commands/msgview/open.go b/commands/msgview/open.go index 05a3c63..f3723fb 100644 --- a/commands/msgview/open.go +++ b/commands/msgview/open.go @@ -3,7 +3,6 @@ package msgview import ( "fmt" "io" - "io/ioutil" "mime" "os" "time" @@ -60,7 +59,7 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error { } } - tmpFile, err := ioutil.TempFile(os.TempDir(), "aerc-*"+extension) + tmpFile, err := os.CreateTemp(os.TempDir(), "aerc-*"+extension) if err != nil { aerc.PushError(err.Error()) return diff --git a/completer/completer.go b/completer/completer.go index 202607a..bbeb5f1 100644 --- a/completer/completer.go +++ b/completer/completer.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime" "net/mail" "os/exec" @@ -100,7 +99,7 @@ func (c *Completer) completeAddress(s string) ([]string, string, error) { completions, err := readCompletions(stdout) if err != nil { - buf, _ := ioutil.ReadAll(stderr) + buf, _ := io.ReadAll(stderr) msg := strings.TrimSpace(string(buf)) if msg != "" { msg = ": " + msg diff --git a/config/config.go b/config/config.go index 66c6dd1..c6fe70e 100644 --- a/config/config.go +++ b/config/config.go @@ -3,7 +3,6 @@ package config import ( "errors" "fmt" - "io/ioutil" "log" "net/url" "os" @@ -407,7 +406,7 @@ func installTemplate(root, name string) error { } var data []byte for _, dir := range searchDirs { - data, err = ioutil.ReadFile(path.Join(dir, name)) + data, err = os.ReadFile(path.Join(dir, name)) if err == nil { break } @@ -415,7 +414,7 @@ func installTemplate(root, name string) error { if err != nil { return err } - err = ioutil.WriteFile(path.Join(root, name), data, 0o644) + err = os.WriteFile(path.Join(root, name), data, 0o644) if err != nil { return err } diff --git a/lib/crypto/gpg/gpgbin/decrypt.go b/lib/crypto/gpg/gpgbin/decrypt.go index 0962630..86d5575 100644 --- a/lib/crypto/gpg/gpgbin/decrypt.go +++ b/lib/crypto/gpg/gpgbin/decrypt.go @@ -3,7 +3,6 @@ package gpgbin import ( "bytes" "io" - "io/ioutil" "git.sr.ht/~rjarry/aerc/models" ) @@ -12,7 +11,7 @@ import ( // the signature is also verified func Decrypt(r io.Reader) (*models.MessageDetails, error) { md := new(models.MessageDetails) - orig, err := ioutil.ReadAll(r) + orig, err := io.ReadAll(r) if err != nil { return md, err } diff --git a/lib/crypto/gpg/gpgbin/verify.go b/lib/crypto/gpg/gpgbin/verify.go index 5079a0a..8208dc0 100644 --- a/lib/crypto/gpg/gpgbin/verify.go +++ b/lib/crypto/gpg/gpgbin/verify.go @@ -3,7 +3,6 @@ package gpgbin import ( "bytes" "io" - "io/ioutil" "os" "git.sr.ht/~rjarry/aerc/models" @@ -15,7 +14,7 @@ func Verify(m io.Reader, s io.Reader) (*models.MessageDetails, error) { args := []string{"--verify"} if s != nil { // Detached sig, save the sig to a tmp file and send msg over stdin - sig, err := ioutil.TempFile("", "sig") + sig, err := os.CreateTemp("", "sig") if err != nil { return nil, err } @@ -24,7 +23,7 @@ func Verify(m io.Reader, s io.Reader) (*models.MessageDetails, error) { defer os.Remove(sig.Name()) args = append(args, sig.Name(), "-") } - orig, err := ioutil.ReadAll(m) + orig, err := io.ReadAll(m) if err != nil { return nil, err } diff --git a/lib/crypto/pgp/pgp.go b/lib/crypto/pgp/pgp.go index 689bae3..f5612ee 100644 --- a/lib/crypto/pgp/pgp.go +++ b/lib/crypto/pgp/pgp.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path" "strings" @@ -122,7 +121,7 @@ func (m *Mail) Decrypt(r io.Reader, decryptKeys openpgp.PromptFunction) (*models if pgpReader.MessageDetails.IsSigned { // we should consume the UnverifiedBody until EOF in order // to get the correct signature data - data, err := ioutil.ReadAll(pgpReader.MessageDetails.UnverifiedBody) + data, err := io.ReadAll(pgpReader.MessageDetails.UnverifiedBody) if err != nil { return nil, err } diff --git a/lib/messageview.go b/lib/messageview.go index e0e86ea..6ad86c9 100644 --- a/lib/messageview.go +++ b/lib/messageview.go @@ -3,7 +3,6 @@ package lib import ( "bytes" "io" - "io/ioutil" "github.com/ProtonMail/go-crypto/openpgp" "github.com/emersion/go-message" @@ -77,7 +76,7 @@ func NewMessageStoreView(messageInfo *models.MessageInfo, cb(nil, err) return } - msv.message, err = ioutil.ReadAll(md.Body) + msv.message, err = io.ReadAll(md.Body) if err != nil { cb(nil, err) return diff --git a/lib/parse/hyperlinks_test.go b/lib/parse/hyperlinks_test.go index f16392a..ba67664 100644 --- a/lib/parse/hyperlinks_test.go +++ b/lib/parse/hyperlinks_test.go @@ -1,7 +1,7 @@ package parse_test import ( - "io/ioutil" + "io" "strings" "testing" @@ -75,7 +75,7 @@ func TestHyperlinks(t *testing.T) { // make sure reader is exact copy of input reader reader, links := parse.HttpLinks(strings.NewReader(test.text)) - if data, err := ioutil.ReadAll(reader); err != nil { + if data, err := io.ReadAll(reader); err != nil { t.Errorf("could not read text: %v", err) } else if string(data) != test.text { t.Errorf("did not copy input reader correctly") diff --git a/logging/logger.go b/logging/logger.go index cf7192e..0e87fe0 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -2,7 +2,7 @@ package logging import ( "fmt" - "io/ioutil" + "io" "log" "os" ) @@ -24,7 +24,7 @@ func Init() { func ErrorLogger() *log.Logger { if err == nil { - return log.New(ioutil.Discard, "", log.LstdFlags) + return log.New(io.Discard, "", log.LstdFlags) } return err } diff --git a/widgets/compose.go b/widgets/compose.go index 670f4f2..5106e17 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/textproto" "os" "os/exec" @@ -92,7 +91,7 @@ func NewComposer(aerc *Aerc, acct *AccountView, conf *config.AercConfig, logging.Errorf("could not complete header: %v", err) }) - email, err := ioutil.TempFile("", "aerc-compose-*.eml") + email, err := os.CreateTemp("", "aerc-compose-*.eml") if err != nil { // TODO: handle this better return nil, err @@ -454,7 +453,7 @@ func (c *Composer) readSignatureFromFile() []byte { if err != nil { return nil } - signature, err := ioutil.ReadFile(sigFile) + signature, err := os.ReadFile(sigFile) if err != nil { c.aerc.PushError( fmt.Sprintf(" Error loading signature from file: %v", sigFile)) diff --git a/worker/lib/parse_test.go b/worker/lib/parse_test.go index 7e5bb36..227b121 100644 --- a/worker/lib/parse_test.go +++ b/worker/lib/parse_test.go @@ -2,7 +2,6 @@ package lib import ( "io" - "io/ioutil" "os" "path/filepath" "testing" @@ -12,7 +11,7 @@ import ( func TestMessageInfoParser(t *testing.T) { rootDir := "testdata/message/valid" - msgFiles, err := ioutil.ReadDir(rootDir) + msgFiles, err := os.ReadDir(rootDir) die(err) for _, fi := range msgFiles { @@ -37,7 +36,7 @@ func TestMessageInfoParser(t *testing.T) { func TestMessageInfoHandledError(t *testing.T) { rootDir := "testdata/message/invalid" - msgFiles, err := ioutil.ReadDir(rootDir) + msgFiles, err := os.ReadDir(rootDir) die(err) for _, fi := range msgFiles { diff --git a/worker/lib/search.go b/worker/lib/search.go index 551d33c..fe1ec11 100644 --- a/worker/lib/search.go +++ b/worker/lib/search.go @@ -1,7 +1,7 @@ package lib import ( - "io/ioutil" + "io" "net/textproto" "strings" "unicode" @@ -129,7 +129,7 @@ func searchMessage(message RawMessage, criteria *searchCriteria, return false, err } defer reader.Close() - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) if err != nil { return false, err } @@ -141,7 +141,7 @@ func searchMessage(message RawMessage, criteria *searchCriteria, return false, err } defer reader.Close() - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) if err != nil { return false, err } diff --git a/worker/maildir/search.go b/worker/maildir/search.go index 6260deb..2ad1c0e 100644 --- a/worker/maildir/search.go +++ b/worker/maildir/search.go @@ -1,7 +1,7 @@ package maildir import ( - "io/ioutil" + "io" "net/textproto" "strings" "unicode" @@ -151,7 +151,7 @@ func (w *Worker) searchKey(key uint32, criteria *searchCriteria, if err != nil { return false, err } - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) if err != nil { return false, err } @@ -163,7 +163,7 @@ func (w *Worker) searchKey(key uint32, criteria *searchCriteria, return false, err } defer reader.Close() - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) if err != nil { return false, err } diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 9f9fee9..e482f4e 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/url" "os" "os/exec" @@ -543,7 +542,7 @@ func (w *Worker) handleFetchFullMessages(msg *types.FetchFullMessages) error { return err } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return err } diff --git a/worker/mbox/io.go b/worker/mbox/io.go index 6729048..31a906c 100644 --- a/worker/mbox/io.go +++ b/worker/mbox/io.go @@ -3,7 +3,6 @@ package mboxer import ( "errors" "io" - "io/ioutil" "time" "git.sr.ht/~rjarry/aerc/models" @@ -23,7 +22,7 @@ func Read(r io.Reader) ([]lib.RawMessage, error) { return nil, err } - content, err := ioutil.ReadAll(msg) + content, err := io.ReadAll(msg) if err != nil { return nil, err } diff --git a/worker/mbox/models.go b/worker/mbox/models.go index 849133c..eec7337 100644 --- a/worker/mbox/models.go +++ b/worker/mbox/models.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/lib" @@ -150,7 +149,7 @@ func (f *container) newUid() (next uint32) { } func (f *container) Append(r io.Reader, flags []models.Flag) error { - data, err := ioutil.ReadAll(r) + data, err := io.ReadAll(r) if err != nil { return err } @@ -170,7 +169,7 @@ type message struct { } func (m *message) NewReader() (io.ReadCloser, error) { - return ioutil.NopCloser(bytes.NewReader(m.content)), nil + return io.NopCloser(bytes.NewReader(m.content)), nil } func (m *message) ModelFlags() ([]models.Flag, error) { diff --git a/worker/mbox/worker.go b/worker/mbox/worker.go index 1e3ab98..78a9b1b 100644 --- a/worker/mbox/worker.go +++ b/worker/mbox/worker.go @@ -4,7 +4,7 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" + "io" "net/url" "os" "path/filepath" @@ -224,7 +224,7 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { continue } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { logging.Errorf("could not get message reader: %w", err) continue diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index e6a9a49..903460e 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -9,7 +9,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net/url" "os" "os/exec" @@ -396,7 +396,7 @@ func (w *worker) handleFetchFullMessages(msg *types.FetchFullMessages) error { return err } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return err }