Fix deprecations of ioutil (#3370)

This commit is contained in:
Bart 2023-07-16 10:14:22 +02:00 committed by GitHub
parent 547e101f1d
commit 3c09c77269
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 14 deletions

View File

@ -2,7 +2,6 @@ package fzf
import ( import (
"errors" "errors"
"io/ioutil"
"os" "os"
"strings" "strings"
) )
@ -26,12 +25,12 @@ func NewHistory(path string, maxSize int) (*History, error) {
} }
// Read history file // Read history file
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
// If it doesn't exist, check if we can create a file with the name // If it doesn't exist, check if we can create a file with the name
if os.IsNotExist(err) { if os.IsNotExist(err) {
data = []byte{} data = []byte{}
if err := ioutil.WriteFile(path, data, 0600); err != nil { if err := os.WriteFile(path, data, 0600); err != nil {
return nil, fmtError(err) return nil, fmtError(err)
} }
} else { } else {
@ -62,11 +61,11 @@ func (h *History) append(line string) error {
lines = lines[len(lines)-h.maxSize:] lines = lines[len(lines)-h.maxSize:]
} }
h.lines = append(lines, "") h.lines = append(lines, "")
return ioutil.WriteFile(h.path, []byte(strings.Join(h.lines, "\n")), 0600) return os.WriteFile(h.path, []byte(strings.Join(h.lines, "\n")), 0600)
} }
func (h *History) override(str string) { func (h *History) override(str string) {
// You can update the history but they're not written to the file // You can update the history, but they're not written to the file
if h.cursor == len(h.lines)-1 { if h.cursor == len(h.lines)-1 {
h.lines[h.cursor] = str h.lines[h.cursor] = str
} else if h.cursor < len(h.lines)-1 { } else if h.cursor < len(h.lines)-1 {

View File

@ -1,7 +1,6 @@
package fzf package fzf
import ( import (
"io/ioutil"
"os" "os"
"runtime" "runtime"
"testing" "testing"
@ -25,7 +24,7 @@ func TestHistory(t *testing.T) {
} }
} }
f, _ := ioutil.TempFile("", "fzf-history") f, _ := os.CreateTemp("", "fzf-history")
f.Close() f.Close()
{ // Append lines { // Append lines

View File

@ -2,7 +2,7 @@ package fzf
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"testing" "testing"
"github.com/junegunn/fzf/src/tui" "github.com/junegunn/fzf/src/tui"
@ -357,7 +357,7 @@ func TestDefaultCtrlNP(t *testing.T) {
check([]string{"--bind=ctrl-n:accept"}, tui.CtrlN, actAccept) check([]string{"--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)
check([]string{"--bind=ctrl-p:accept"}, tui.CtrlP, actAccept) check([]string{"--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)
f, _ := ioutil.TempFile("", "fzf-history") f, _ := os.CreateTemp("", "fzf-history")
f.Close() f.Close()
hist := "--history=" + f.Name() hist := "--history=" + f.Name()
check([]string{hist}, tui.CtrlN, actNextHistory) check([]string{hist}, tui.CtrlN, actNextHistory)

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"os" "os"
"os/exec" "os/exec"
@ -2208,7 +2207,7 @@ func hasPreviewFlags(template string) (slot bool, plus bool, query bool) {
} }
func writeTemporaryFile(data []string, printSep string) string { func writeTemporaryFile(data []string, printSep string) string {
f, err := ioutil.TempFile("", "fzf-preview-*") f, err := os.CreateTemp("", "fzf-preview-*")
if err != nil { if err != nil {
errorExit("Unable to create temporary file") errorExit("Unable to create temporary file")
} }

View File

@ -3,7 +3,6 @@
package tui package tui
import ( import (
"io/ioutil"
"os" "os"
"syscall" "syscall"
) )
@ -17,13 +16,17 @@ func ttyname() string {
} }
for _, prefix := range devPrefixes { for _, prefix := range devPrefixes {
files, err := ioutil.ReadDir(prefix) files, err := os.ReadDir(prefix)
if err != nil { if err != nil {
continue continue
} }
for _, file := range files { for _, file := range files {
if stat, ok := file.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev { info, err := file.Info()
if err != nil {
continue
}
if stat, ok := info.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev {
return prefix + file.Name() return prefix + file.Name()
} }
} }