ioutil: Remove deprecated functions where possible

This commit is contained in:
Jöran Karl 2024-05-30 21:34:11 +02:00
parent 18a81f043c
commit 6e8daa117a
12 changed files with 29 additions and 39 deletions

View file

@ -4,7 +4,6 @@ import (
"bufio"
"encoding/gob"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -91,7 +90,7 @@ func CleanConfig() {
// detect incorrectly formatted buffer/ files
buffersPath := filepath.Join(config.ConfigDir, "buffers")
files, err := ioutil.ReadDir(buffersPath)
files, err := os.ReadDir(buffersPath)
if err == nil {
var badFiles []string
var buffer buffer.SerializedBuffer

View file

@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
@ -209,7 +208,7 @@ func LoadInput(args []string) []*buffer.Buffer {
// Option 2
// The input is not a terminal, so something is being piped in
// and we should read from stdin
input, err = ioutil.ReadAll(os.Stdin)
input, err = io.ReadAll(os.Stdin)
if err != nil {
screen.TermMessage("Error reading from stdin: ", err)
input = []byte{}

View file

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -27,7 +26,7 @@ var Binder = map[string]func(e Event, action string){
func createBindingsIfNotExist(fname string) {
if _, e := os.Stat(fname); errors.Is(e, fs.ErrNotExist) {
ioutil.WriteFile(fname, []byte("{}"), util.FileMode)
os.WriteFile(fname, []byte("{}"), util.FileMode)
}
}
@ -39,7 +38,7 @@ func InitBindings() {
createBindingsIfNotExist(filename)
if _, e := os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
input, err := os.ReadFile(filename)
if err != nil {
screen.TermMessage("Error reading bindings.json file: " + err.Error())
return
@ -267,7 +266,7 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
filename := filepath.Join(config.ConfigDir, "bindings.json")
createBindingsIfNotExist(filename)
if _, e = os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
input, err := os.ReadFile(filename)
if err != nil {
return false, errors.New("Error reading bindings.json file: " + err.Error())
}
@ -306,7 +305,7 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
BindKey(k, v, Binder["buffer"])
txt, _ := json.MarshalIndent(parsed, "", " ")
return true, ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode)
return true, os.WriteFile(filename, append(txt, '\n'), util.FileMode)
}
return false, e
}
@ -319,7 +318,7 @@ func UnbindKey(k string) error {
filename := filepath.Join(config.ConfigDir, "bindings.json")
createBindingsIfNotExist(filename)
if _, e = os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
input, err := os.ReadFile(filename)
if err != nil {
return errors.New("Error reading bindings.json file: " + err.Error())
}
@ -356,7 +355,7 @@ func UnbindKey(k string) error {
}
txt, _ := json.MarshalIndent(parsed, "", " ")
return ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode)
return os.WriteFile(filename, append(txt, '\n'), util.FileMode)
}
return e
}

View file

@ -2,7 +2,7 @@ package buffer
import (
"bytes"
"io/ioutil"
"io/fs"
"os"
"sort"
"strings"
@ -109,15 +109,15 @@ func FileComplete(b *Buffer) ([]string, []string) {
sep := string(os.PathSeparator)
dirs := strings.Split(input, sep)
var files []os.FileInfo
var files []fs.DirEntry
var err error
if len(dirs) > 1 {
directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
directories, _ = util.ReplaceHome(directories)
files, err = ioutil.ReadDir(directories)
files, err = os.ReadDir(directories)
} else {
files, err = ioutil.ReadDir(".")
files, err = os.ReadDir(".")
}
if err != nil {

View file

@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -544,7 +543,7 @@ func (b *Buffer) ReOpen() error {
}
reader := bufio.NewReader(transform.NewReader(file, enc.NewDecoder()))
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
txt := string(data)
if err != nil {

View file

@ -5,7 +5,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -396,7 +395,7 @@ func (pv *PluginVersion) DownloadAndInstall(out io.Writer) error {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

View file

@ -2,7 +2,6 @@ package config
import (
"errors"
"io/ioutil"
"log"
"os"
"path"
@ -81,7 +80,7 @@ func (rf realFile) Name() string {
}
func (rf realFile) Data() ([]byte, error) {
return ioutil.ReadFile(string(rf))
return os.ReadFile(string(rf))
}
func (af assetFile) Name() string {
@ -107,7 +106,7 @@ func AddRealRuntimeFile(fileType RTFiletype, file RuntimeFile) {
// AddRuntimeFilesFromDirectory registers each file from the given directory for
// the filetype which matches the file-pattern
func AddRuntimeFilesFromDirectory(fileType RTFiletype, directory, pattern string) {
files, _ := ioutil.ReadDir(directory)
files, _ := os.ReadDir(directory)
for _, f := range files {
if ok, _ := filepath.Match(pattern, f.Name()); !f.IsDir() && ok {
fullPath := filepath.Join(directory, f.Name())
@ -194,14 +193,14 @@ func InitPlugins() {
// Search ConfigDir for plugin-scripts
plugdir := filepath.Join(ConfigDir, "plug")
files, _ := ioutil.ReadDir(plugdir)
files, _ := os.ReadDir(plugdir)
isID := regexp.MustCompile(`^[_A-Za-z0-9]+$`).MatchString
for _, d := range files {
plugpath := filepath.Join(plugdir, d.Name())
if stat, err := os.Stat(plugpath); err == nil && stat.IsDir() {
srcs, _ := ioutil.ReadDir(plugpath)
srcs, _ := os.ReadDir(plugpath)
p := new(Plugin)
p.Name = d.Name()
p.DirName = d.Name()
@ -209,7 +208,7 @@ func InitPlugins() {
if strings.HasSuffix(f.Name(), ".lua") {
p.Srcs = append(p.Srcs, realFile(filepath.Join(plugdir, d.Name(), f.Name())))
} else if strings.HasSuffix(f.Name(), ".json") {
data, err := ioutil.ReadFile(filepath.Join(plugdir, d.Name(), f.Name()))
data, err := os.ReadFile(filepath.Join(plugdir, d.Name(), f.Name()))
if err != nil {
continue
}

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -222,7 +221,7 @@ func ReadSettings() error {
parsedSettings = make(map[string]interface{})
filename := filepath.Join(ConfigDir, "settings.json")
if _, e := os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
input, err := os.ReadFile(filename)
if err != nil {
settingsParseError = true
return errors.New("Error reading settings.json file: " + err.Error())
@ -356,7 +355,7 @@ func WriteSettings(filename string) error {
}
txt, _ := json.MarshalIndent(parsedSettings, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode)
err = os.WriteFile(filename, append(txt, '\n'), util.FileMode)
}
return err
}
@ -378,7 +377,7 @@ func OverwriteSettings(filename string) error {
}
txt, _ := json.MarshalIndent(settings, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode)
err = os.WriteFile(filename, append(txt, '\n'), util.FileMode)
}
return err
}

View file

@ -6,7 +6,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
@ -34,7 +33,7 @@ func main() {
if len(os.Args) > 1 {
os.Chdir(os.Args[1])
}
files, _ := ioutil.ReadDir(".")
files, _ := os.ReadDir(".")
for _, f := range files {
fname := f.Name()
if strings.HasSuffix(fname, ".yaml") {
@ -46,7 +45,7 @@ func main() {
func convert(name string) {
filename := name + ".yaml"
var hdr HeaderYaml
source, err := ioutil.ReadFile(filename)
source, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
@ -68,7 +67,7 @@ func encode(name string, c HeaderYaml) {
func decode(name string) Header {
start := time.Now()
data, _ := ioutil.ReadFile(name + ".hdr")
data, _ := os.ReadFile(name + ".hdr")
strs := bytes.Split(data, []byte{'\n'})
var hdr Header
hdr.FileType = string(strs[0])

View file

@ -4,7 +4,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
@ -161,6 +160,6 @@ func main() {
return
}
data, _ := ioutil.ReadFile(os.Args[1])
data, _ := os.ReadFile(os.Args[1])
fmt.Print(generateFile(parseFile(string(data), os.Args[1])))
}

View file

@ -4,7 +4,7 @@ package main
import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os/exec"
"strings"
@ -19,7 +19,7 @@ func main() {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
var data interface{}

View file

@ -4,7 +4,6 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
@ -210,7 +209,7 @@ func main() {
var tests []test
for _, filename := range os.Args[1:] {
source, err := ioutil.ReadFile(filename)
source, err := os.ReadFile(filename)
if err != nil {
log.Fatalln(err)
}