1
0
mirror of https://github.com/google/gops.git synced 2024-11-24 08:22:25 +02:00

replace ioutil usages with io and os (#202)

ioutil was deprecated in go1.16 and its functions now directly call io
and os functions. It is recommended to use these implementations in new
code.
This commit is contained in:
segevda 2023-02-10 15:03:23 +02:00 committed by GitHub
parent dbeb29cd3e
commit a241038823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 16 deletions

View File

@ -12,7 +12,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
gosignal "os/signal" gosignal "os/signal"
@ -115,7 +114,7 @@ func Listen(opts Options) error {
} }
port := listener.Addr().(*net.TCPAddr).Port port := listener.Addr().(*net.TCPAddr).Port
portfile = filepath.Join(gopsdir, strconv.Itoa(os.Getpid())) portfile = filepath.Join(gopsdir, strconv.Itoa(os.Getpid()))
err = ioutil.WriteFile(portfile, []byte(strconv.Itoa(port)), os.ModePerm) err = os.WriteFile(portfile, []byte(strconv.Itoa(port)), os.ModePerm)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,7 +1,6 @@
package goprocess package goprocess
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -72,17 +71,17 @@ func TestFindAll(t *testing.T) {
if runtime.GOOS != "linux" { if runtime.GOOS != "linux" {
t.Skip() t.Skip()
} }
tempDir, err := ioutil.TempDir("", "") tempDir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Errorf("failed to create temp dir: %v", err) t.Errorf("failed to create temp dir: %v", err)
} }
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
for _, p := range tc.input { for _, p := range tc.input {
os.Mkdir(filepath.Join(tempDir, strconv.Itoa(int(p.Pid))), 0o755) os.Mkdir(filepath.Join(tempDir, strconv.Itoa(int(p.Pid))), 0o755)
ioutil.WriteFile(filepath.Join(tempDir, strconv.Itoa(int(p.Pid)), "stat"), []byte( os.WriteFile(filepath.Join(tempDir, strconv.Itoa(int(p.Pid)), "stat"), []byte(
`1440024 () R 0 1440024 0 34821 1440024 4194304 134 0 0 0 0 0 0 0 20 0 1 0 95120609 6746112 274 18446744073709551615 94467689938944 94467690036601 140724224197808 0 0 0 0 0 0 0 0 0 17 11 0 0 0 0 0 94467690068048 94467690071296 94467715629056 140724224199226 140724224199259 140724224199259 140724224204780 0`, `1440024 () R 0 1440024 0 34821 1440024 4194304 134 0 0 0 0 0 0 0 20 0 1 0 95120609 6746112 274 18446744073709551615 94467689938944 94467690036601 140724224197808 0 0 0 0 0 0 0 0 0 17 11 0 0 0 0 0 94467690068048 94467690071296 94467715629056 140724224199226 140724224199259 140724224199259 140724224204780 0`,
), 0o644) ), 0o644)
ioutil.WriteFile(filepath.Join(tempDir, strconv.Itoa(int(p.Pid)), "status"), []byte( os.WriteFile(filepath.Join(tempDir, strconv.Itoa(int(p.Pid)), "status"), []byte(
`Name: `Name:
Umask: 0022 Umask: 0022
State: R (running) State: R (running)

View File

@ -9,7 +9,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@ -179,11 +178,11 @@ func trace(addr net.TCPAddr, _ []string) error {
if len(out) == 0 { if len(out) == 0 {
return errors.New("nothing has traced") return errors.New("nothing has traced")
} }
tmpfile, err := ioutil.TempFile("", "trace") tmpfile, err := os.CreateTemp("", "trace")
if err != nil { if err != nil {
return err return err
} }
if err := ioutil.WriteFile(tmpfile.Name(), out, 0); err != nil { if err := os.WriteFile(tmpfile.Name(), out, 0); err != nil {
return err return err
} }
fmt.Printf("Trace dump saved to: %s\n", tmpfile.Name()) fmt.Printf("Trace dump saved to: %s\n", tmpfile.Name())
@ -201,7 +200,7 @@ func trace(addr net.TCPAddr, _ []string) error {
} }
func pprof(addr net.TCPAddr, p byte, prefix string) error { func pprof(addr net.TCPAddr, p byte, prefix string) error {
tmpDumpFile, err := ioutil.TempFile("", prefix+"_profile") tmpDumpFile, err := os.CreateTemp("", prefix+"_profile")
if err != nil { if err != nil {
return err return err
} }
@ -213,7 +212,7 @@ func pprof(addr net.TCPAddr, p byte, prefix string) error {
if len(out) == 0 { if len(out) == 0 {
return errors.New("failed to read the profile") return errors.New("failed to read the profile")
} }
if err := ioutil.WriteFile(tmpDumpFile.Name(), out, 0); err != nil { if err := os.WriteFile(tmpDumpFile.Name(), out, 0); err != nil {
return err return err
} }
fmt.Printf("Profile dump saved to: %s\n", tmpDumpFile.Name()) fmt.Printf("Profile dump saved to: %s\n", tmpDumpFile.Name())
@ -224,7 +223,7 @@ func pprof(addr net.TCPAddr, p byte, prefix string) error {
defer os.Remove(tmpDumpFile.Name()) defer os.Remove(tmpDumpFile.Name())
} }
// Download running binary // Download running binary
tmpBinFile, err := ioutil.TempFile("", "binary") tmpBinFile, err := os.CreateTemp("", "binary")
if err != nil { if err != nil {
return err return err
} }
@ -237,7 +236,7 @@ func pprof(addr net.TCPAddr, p byte, prefix string) error {
return errors.New("failed to read the binary") return errors.New("failed to read the binary")
} }
defer os.Remove(tmpBinFile.Name()) defer os.Remove(tmpBinFile.Name())
if err := ioutil.WriteFile(tmpBinFile.Name(), out, 0); err != nil { if err := os.WriteFile(tmpBinFile.Name(), out, 0); err != nil {
return err return err
} }
} }
@ -294,7 +293,7 @@ func cmd(addr net.TCPAddr, c byte, params ...byte) ([]byte, error) {
return nil, fmt.Errorf("couldn't get port by PID: %v", err) return nil, fmt.Errorf("couldn't get port by PID: %v", err)
} }
all, err := ioutil.ReadAll(conn) all, err := io.ReadAll(conn)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -6,7 +6,6 @@ package internal
import ( import (
"errors" "errors"
"io/ioutil"
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -53,7 +52,7 @@ func GetPort(pid int) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
b, err := ioutil.ReadFile(portfile) b, err := os.ReadFile(portfile)
if err != nil { if err != nil {
return "", err return "", err
} }