Skip to content

Commit

Permalink
Merge pull request #351 from ffromani/fix-lint-prep-v1
Browse files Browse the repository at this point in the history
go: chore: remove references to io/ioutil
  • Loading branch information
jaypipes authored Jul 21, 2023
2 parents 7a46924 + 1ddd983 commit 02bdf58
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 105 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1333,15 +1333,14 @@ package.

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

"github.com/jaypipes/ghw/pkg/snapshot"
)

// ...

scratchDir, err := ioutil.TempDir("", "ghw-snapshot-*")
scratchDir, err := os.MkdirTemp("", "ghw-snapshot-*")
if err != nil {
fmt.Printf("Error creating clone directory: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/ghw-snapshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"

Expand Down Expand Up @@ -65,7 +64,7 @@ func defaultOutPath() (string, error) {
}

func execute(cmd *cobra.Command, args []string) error {
scratchDir, err := ioutil.TempDir("", "ghw-snapshot")
scratchDir, err := os.MkdirTemp("", "ghw-snapshot")
if err != nil {
return err
}
Expand Down
23 changes: 11 additions & 12 deletions pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package block
import (
"bufio"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -38,7 +37,7 @@ func diskPhysicalBlockSizeBytes(paths *linuxpath.Paths, disk string) uint64 {
// We can find the sector size in Linux by looking at the
// /sys/block/$DEVICE/queue/physical_block_size file in sysfs
path := filepath.Join(paths.SysBlock, disk, "queue", "physical_block_size")
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return 0
}
Expand All @@ -53,7 +52,7 @@ func diskSizeBytes(paths *linuxpath.Paths, disk string) uint64 {
// We can find the number of 512-byte sectors by examining the contents of
// /sys/block/$DEVICE/size and calculate the physical bytes accordingly.
path := filepath.Join(paths.SysBlock, disk, "size")
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return 0
}
Expand All @@ -70,7 +69,7 @@ func diskNUMANodeID(paths *linuxpath.Paths, disk string) int {
return -1
}
for partial := link; strings.HasPrefix(partial, "../devices/"); partial = filepath.Base(partial) {
if nodeContents, err := ioutil.ReadFile(filepath.Join(paths.SysBlock, partial, "numa_node")); err != nil {
if nodeContents, err := os.ReadFile(filepath.Join(paths.SysBlock, partial, "numa_node")); err != nil {
if nodeInt, err := strconv.Atoi(string(nodeContents)); err != nil {
return nodeInt
}
Expand All @@ -83,7 +82,7 @@ func diskVendor(paths *linuxpath.Paths, disk string) string {
// In Linux, the vendor for a disk device is found in the
// /sys/block/$DEVICE/device/vendor file in sysfs
path := filepath.Join(paths.SysBlock, disk, "device", "vendor")
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return util.UNKNOWN
}
Expand All @@ -93,7 +92,7 @@ func diskVendor(paths *linuxpath.Paths, disk string) string {
// udevInfoDisk gets the udev info for a disk
func udevInfoDisk(paths *linuxpath.Paths, disk string) (map[string]string, error) {
// Get device major:minor numbers
devNo, err := ioutil.ReadFile(filepath.Join(paths.SysBlock, disk, "dev"))
devNo, err := os.ReadFile(filepath.Join(paths.SysBlock, disk, "dev"))
if err != nil {
return nil, err
}
Expand All @@ -103,7 +102,7 @@ func udevInfoDisk(paths *linuxpath.Paths, disk string) (map[string]string, error
// udevInfoPartition gets the udev info for a partition
func udevInfoPartition(paths *linuxpath.Paths, disk string, partition string) (map[string]string, error) {
// Get device major:minor numbers
devNo, err := ioutil.ReadFile(filepath.Join(paths.SysBlock, disk, partition, "dev"))
devNo, err := os.ReadFile(filepath.Join(paths.SysBlock, disk, partition, "dev"))
if err != nil {
return nil, err
}
Expand All @@ -113,7 +112,7 @@ func udevInfoPartition(paths *linuxpath.Paths, disk string, partition string) (m
func udevInfo(paths *linuxpath.Paths, devNo string) (map[string]string, error) {
// Look up block device in udev runtime database
udevID := "b" + strings.TrimSpace(devNo)
udevBytes, err := ioutil.ReadFile(filepath.Join(paths.RunUdevData, udevID))
udevBytes, err := os.ReadFile(filepath.Join(paths.RunUdevData, udevID))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -196,7 +195,7 @@ func diskWWN(paths *linuxpath.Paths, disk string) string {
func diskPartitions(ctx *context.Context, paths *linuxpath.Paths, disk string) []*Partition {
out := make([]*Partition, 0)
path := filepath.Join(paths.SysBlock, disk)
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
ctx.Warn("failed to read disk partitions: %s\n", err)
return out
Expand Down Expand Up @@ -281,7 +280,7 @@ func diskPartUUID(paths *linuxpath.Paths, disk string, partition string) string

func diskIsRemovable(paths *linuxpath.Paths, disk string) bool {
path := filepath.Join(paths.SysBlock, disk, "removable")
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return false
}
Expand All @@ -295,7 +294,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
// run. We can get all of this information by examining the /sys/block
// and /sys/class/block files
disks := make([]*Disk, 0)
files, err := ioutil.ReadDir(paths.SysBlock)
files, err := os.ReadDir(paths.SysBlock)
if err != nil {
return nil
}
Expand Down Expand Up @@ -403,7 +402,7 @@ func diskIsRotational(ctx *context.Context, paths *linuxpath.Paths, devName stri
// paths.
func partitionSizeBytes(paths *linuxpath.Paths, disk string, part string) uint64 {
path := filepath.Join(paths.SysBlock, disk, part, "size")
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return 0
}
Expand Down
39 changes: 19 additions & 20 deletions pkg/block/block_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package block

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -199,7 +198,7 @@ func TestDiskPartLabel(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
baseDir, _ := ioutil.TempDir("", "test")
baseDir, _ := os.MkdirTemp("", "test")
defer os.RemoveAll(baseDir)
ctx := context.New()
ctx.Chroot = baseDir
Expand All @@ -212,8 +211,8 @@ func TestDiskPartLabel(t *testing.T) {
// Emulate a disk with one partition with label TEST_LABEL_GHW
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda"), 0755)
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda", "sda1"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_PART_ENTRY_NAME=%s\n", partLabel)), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_PART_ENTRY_NAME=%s\n", partLabel)), 0644)
label := diskPartLabel(paths, "sda", "sda1")
if label != partLabel {
t.Fatalf("Got label %s but expected %s", label, partLabel)
Expand All @@ -230,7 +229,7 @@ func TestDiskFSLabel(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
baseDir, _ := ioutil.TempDir("", "test")
baseDir, _ := os.MkdirTemp("", "test")
defer os.RemoveAll(baseDir)
ctx := context.New()
ctx.Chroot = baseDir
Expand All @@ -243,8 +242,8 @@ func TestDiskFSLabel(t *testing.T) {
// Emulate a disk with one partition with label TEST_LABEL_GHW
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda"), 0755)
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda", "sda1"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_LABEL=%s\n", fsLabel)), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_LABEL=%s\n", fsLabel)), 0644)
label := diskFSLabel(paths, "sda", "sda1")
if label != fsLabel {
t.Fatalf("Got label %s but expected %s", label, fsLabel)
Expand All @@ -261,7 +260,7 @@ func TestDiskTypeUdev(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
baseDir, _ := ioutil.TempDir("", "test")
baseDir, _ := os.MkdirTemp("", "test")
defer os.RemoveAll(baseDir)
ctx := context.New()
ctx.Chroot = baseDir
Expand All @@ -274,8 +273,8 @@ func TestDiskTypeUdev(t *testing.T) {
// Emulate a disk with one partition with label TEST_LABEL_GHW
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda"), 0755)
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda", "sda1"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_TYPE=%s\n", expectedPartType)), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_TYPE=%s\n", expectedPartType)), 0644)
pt := diskPartTypeUdev(paths, "sda", "sda1")
if pt != expectedPartType {
t.Fatalf("Got partition type %s but expected %s", pt, expectedPartType)
Expand All @@ -292,7 +291,7 @@ func TestDiskPartUUID(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
baseDir, _ := ioutil.TempDir("", "test")
baseDir, _ := os.MkdirTemp("", "test")
defer os.RemoveAll(baseDir)
ctx := context.New()
ctx.Chroot = baseDir
Expand All @@ -305,8 +304,8 @@ func TestDiskPartUUID(t *testing.T) {
// Emulate a disk with one partition with uuid
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda"), 0755)
_ = os.Mkdir(filepath.Join(paths.SysBlock, "sda", "sda1"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_PART_ENTRY_UUID=%s\n", partUUID)), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, "sda", "sda1", "dev"), []byte("259:0\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_PART_ENTRY_UUID=%s\n", partUUID)), 0644)
uuid := diskPartUUID(paths, "sda", "sda1")
if uuid != partUUID {
t.Fatalf("Got uuid %s but expected %s", uuid, partUUID)
Expand All @@ -324,7 +323,7 @@ func TestLoopDevicesWithOption(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
baseDir, _ := ioutil.TempDir("", "test")
baseDir, _ := os.MkdirTemp("", "test")
defer os.RemoveAll(baseDir)
ctx := context.New(option.WithNullAlerter(), option.WithDisableTools())
ctx.Chroot = baseDir
Expand All @@ -342,13 +341,13 @@ func TestLoopDevicesWithOption(t *testing.T) {
_ = os.Mkdir(filepath.Join(paths.SysBlock, loopNotUsed), 0755)
_ = os.Mkdir(filepath.Join(paths.SysBlock, expectedLoopName, "queue"), 0755)
_ = os.Mkdir(filepath.Join(paths.SysBlock, loopNotUsed, "queue"), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, "queue", "rotational"), []byte("1\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, "size"), []byte("62810112\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, loopNotUsed, "size"), []byte("0\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, "queue", "rotational"), []byte("1\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, "size"), []byte("62810112\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, loopNotUsed, "size"), []byte("0\n"), 0644)
_ = os.Mkdir(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName), 0755)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName, "dev"), []byte("259:0\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName, "size"), []byte("102400\n"), 0644)
_ = ioutil.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_TYPE=%s\n", fsType)), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName, "dev"), []byte("259:0\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName, "size"), []byte("102400\n"), 0644)
_ = os.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_TYPE=%s\n", fsType)), 0644)
d := disks(ctx, paths)
// There should be one disk, the other should be ignored due to 0 size
if len(d) != 1 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package block_test

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -112,7 +111,7 @@ func TestBlockUnmarshal(t *testing.T) {
t.Fatalf("Expected nil err when detecting the samples directory, but got %v", err)
}

data, err := ioutil.ReadFile(filepath.Join(testdataPath, "dell-r610-block.json"))
data, err := os.ReadFile(filepath.Join(testdataPath, "dell-r610-block.json"))
if err != nil {
t.Fatalf("Expected nil err when reading the sample data, but got %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cpu
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -47,7 +46,7 @@ func processorsGet(ctx *context.Context) []*Processor {
// /sys/devices/system/cpu pseudodir contains N number of pseudodirs with
// information about the logical processors on the host. These logical
// processor pseudodirs are of the pattern /sys/devices/system/cpu/cpu{N}
fnames, err := ioutil.ReadDir(paths.SysDevicesSystemCPU)
fnames, err := os.ReadDir(paths.SysDevicesSystemCPU)
if err != nil {
ctx.Warn("failed to read /sys/devices/system/cpu: %s", err)
return []*Processor{}
Expand Down Expand Up @@ -172,7 +171,7 @@ func CoresForNode(ctx *context.Context, nodeID int) ([]*ProcessorCore, error) {
return c
}

files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/gpu/gpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gpu

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (i *Info) load() error {
// subsystem (we query the modalias file of the PCI device's sysfs
// directory using the `ghw.PCIInfo.GetDevice()` function.
paths := linuxpath.New(i.ctx)
links, err := ioutil.ReadDir(paths.SysClassDRM)
links, err := os.ReadDir(paths.SysClassDRM)
if err != nil {
i.ctx.Warn(_WARN_NO_SYS_CLASS_DRM)
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/gpu/gpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package gpu_test

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestGPUWithoutNUMANodeInfo(t *testing.T) {
// snapshot to fully understand this test. Inspect it using
// GHW_SNAPSHOT_PATH="/path/to/linux-amd64-amd-ryzen-1600.tar.gz" ghwc gpu

tmpRoot, err := ioutil.TempDir("", "ghw-gpu-testing-*")
tmpRoot, err := os.MkdirTemp("", "ghw-gpu-testing-*")
if err != nil {
t.Fatalf("Unable to create temporary directory: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/linuxdmi/dmi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package linuxdmi

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

Expand All @@ -19,7 +19,7 @@ func Item(ctx *context.Context, value string) string {
paths := linuxpath.New(ctx)
path := filepath.Join(paths.SysClassDMI, "id", value)

b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
ctx.Warn("Unable to read %s: %s\n", value, err)
return util.UNKNOWN
Expand Down
Loading

0 comments on commit 02bdf58

Please sign in to comment.