Skip to content

Commit

Permalink
🧹 Remove deprecated ioutil (#1698)
Browse files Browse the repository at this point in the history
These were all deprecated since Go 1.16: https://pkg.go.dev/io/ioutil

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Sep 12, 2023
1 parent b2e2175 commit a6cc076
Show file tree
Hide file tree
Showing 39 changed files with 65 additions and 96 deletions.
5 changes: 2 additions & 3 deletions logger/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package logger
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/hokaccha/go-prettyjson"
Expand Down Expand Up @@ -59,7 +58,7 @@ func DebugDumpJSON(name string, obj interface{}) {
log.Error().Err(err).Msg("failed to dump JSON")
}

err = ioutil.WriteFile(DumpLocal+name+".json", []byte(raw), 0644)
err = os.WriteFile(DumpLocal+name+".json", []byte(raw), 0o644)
if err != nil {
log.Error().Err(err).Msg("failed to dump JSON")
}
Expand All @@ -85,7 +84,7 @@ func DebugDumpYAML(name string, obj interface{}) {
log.Error().Err(err).Msg("failed to dump YAML")
}

err = ioutil.WriteFile(DumpLocal+name+".yaml", []byte(raw), 0644)
err = os.WriteFile(DumpLocal+name+".yaml", []byte(raw), 0o644)
if err != nil {
log.Error().Err(err).Msg("failed to dump JSON")
}
Expand Down
2 changes: 1 addition & 1 deletion providers-sdk/v1/inventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestPreprocess(t *testing.T) {
//data, err := inventory.ToYAML()
//require.NoError(t, err)
//
//err = ioutil.WriteFile("./testdata/inventory.parsed.yml", data, 0o700)
//err = os.WriteFile("./testdata/inventory.parsed.yml", data, 0o700)
//require.NoError(t, err)
})

Expand Down
3 changes: 1 addition & 2 deletions providers-sdk/v1/lr/cli/cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -137,7 +136,7 @@ var docsYamlCmd = &cobra.Command{
}

log.Info().Str("file", filepath).Msg("write file")
err = ioutil.WriteFile(filepath, data, 0o700)
err = os.WriteFile(filepath, data, 0o700)
if err != nil {
log.Fatal().Err(err).Msg("could not write docs file")
}
Expand Down
4 changes: 2 additions & 2 deletions providers/arista/resources/eos/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -39,7 +39,7 @@ func TestGetSection(t *testing.T) {
require.NoError(t, err)
defer f.Close()

data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
require.NoError(t, err)

section := GetSection(bytes.NewReader(data), "cvx service openstack")
Expand Down
3 changes: 1 addition & 2 deletions providers/gcp/connection/gcloud_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package connection
import (
"encoding/json"
"io"
"io/ioutil"
)

// https://github.com/golang/oauth2/issues/241
Expand Down Expand Up @@ -34,7 +33,7 @@ func GetCurrentProject() (string, error) {
func ParseGcloudConfig(r io.Reader) (GCloudConfig, error) {
var gcloudconfig GCloudConfig

data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return gcloudconfig, err
}
Expand Down
4 changes: 2 additions & 2 deletions providers/k8s/connection/shared/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package resources
import (
"context"
"fmt"
"io/ioutil"
"io"
"os"
"sync"
"time"
Expand All @@ -29,7 +29,7 @@ func NewDiscovery(restConfig *rest.Config) (*Discovery, error) {
// hide deprecation warnings for go api
// see https://kubernetes.io/blog/2020/09/03/warnings/#customize-client-handling
rest.SetDefaultWarningHandler(
rest.NewWarningWriter(ioutil.Discard, rest.WarningWriterOptions{}),
rest.NewWarningWriter(io.Discard, rest.WarningWriterOptions{}),
)

dynClient, err := dynamic.NewForConfig(restConfig)
Expand Down
4 changes: 2 additions & 2 deletions providers/network/resources/dnsshake/dkim.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"crypto/x509"
"encoding/base64"
"errors"
"io/ioutil"
"io"
"mime/quotedprintable"
"strings"
)
Expand Down Expand Up @@ -107,7 +107,7 @@ func NewDkimPublicKeyRepresentation(dkimRecord string) (*DkimPublicKeyRepresenta
case "n":
pkr.Notes = val
// parse quote printable
qp, err := ioutil.ReadAll(quotedprintable.NewReader(strings.NewReader(val)))
qp, err := io.ReadAll(quotedprintable.NewReader(strings.NewReader(val)))
if err == nil {
pkr.Notes = string(qp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package docker_engine
import (
"context"
"io"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -76,9 +75,9 @@ func TestDockerCommand(t *testing.T) {
assert.Equal(t, "echo 'test'", cmd.Command, "they should be equal")
assert.Equal(t, nil, err, "should execute without error")

stdout, _ := ioutil.ReadAll(cmd.Stdout)
stdout, _ := io.ReadAll(cmd.Stdout)
assert.Equal(t, "test\n", string(stdout), "output should be correct")
stderr, _ := ioutil.ReadAll(cmd.Stderr)
stderr, _ := io.ReadAll(cmd.Stderr)
assert.Equal(t, "", string(stderr), "output should be correct")
})

Expand All @@ -91,10 +90,10 @@ func TestDockerCommand(t *testing.T) {
assert.Equal(t, "echo 'This message goes to stderr' >&2", cmd.Command, "they should be equal")
assert.Equal(t, nil, err, "should execute without error")

stdout, _ := ioutil.ReadAll(cmd.Stdout)
stdout, _ := io.ReadAll(cmd.Stdout)
assert.Equal(t, "", string(stdout), "output should be correct")

stderr, _ := ioutil.ReadAll(cmd.Stderr)
stderr, _ := io.ReadAll(cmd.Stderr)
assert.Equal(t, "This message goes to stderr\n", string(stderr), "output should be correct")
})
}
2 changes: 1 addition & 1 deletion providers/os/connection/ssh/scp/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s Fs) Rename(oldname, newname string) error {
}

func (s Fs) Stat(path string) (os.FileInfo, error) {
// NOTE we cannot use s.scpClient.Receive(path, ioutil.Discard) since it would not work with directories
// NOTE we cannot use s.scpClient.Receive(path, io.Discard) since it would not work with directories
return statutil.New(s.commandRunner).Stat(path)
}

Expand Down
4 changes: 2 additions & 2 deletions providers/os/connection/ssh/scp/fs_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package scp
import (
"bytes"
"errors"
"io/ioutil"
"io"
"os"

scp_client "github.com/hnakamur/go-scp"
Expand Down Expand Up @@ -44,7 +44,7 @@ func (f *File) Name() string {
}

func (f *File) Stat() (os.FileInfo, error) {
return f.scpClient.Receive(f.path, ioutil.Discard)
return f.scpClient.Receive(f.path, io.Discard)
}

func (f *File) Sync() error {
Expand Down
3 changes: 1 addition & 2 deletions providers/os/resources/groups/ps1getlocalgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package groups
import (
"encoding/json"
"io"
"io/ioutil"

"go.mondoo.com/cnquery/providers/os/connection/shared"
"go.mondoo.com/cnquery/providers/os/resources/powershell"
Expand All @@ -27,7 +26,7 @@ type WindowsLocalGroup struct {
}

func ParseWindowsLocalGroups(r io.Reader) ([]WindowsLocalGroup, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
18 changes: 9 additions & 9 deletions providers/os/resources/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package resources
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -42,12 +42,12 @@ func (i *mqlIptables) output() ([]interface{}, error) {
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(cmd.Stdout)
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return nil, err
}
if cmd.ExitStatus != 0 {
outErr, _ := ioutil.ReadAll(cmd.Stderr)
outErr, _ := io.ReadAll(cmd.Stderr)
return nil, errors.New(string(outErr))
}
lines := getLines(string(data))
Expand Down Expand Up @@ -86,12 +86,12 @@ func (i *mqlIptables) input() ([]interface{}, error) {
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(cmd.Stdout)
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return nil, err
}
if cmd.ExitStatus != 0 {
outErr, _ := ioutil.ReadAll(cmd.Stderr)
outErr, _ := io.ReadAll(cmd.Stderr)
return nil, errors.New(string(outErr))
}
lines := getLines(string(data))
Expand Down Expand Up @@ -130,12 +130,12 @@ func (i *mqlIp6tables) output() ([]interface{}, error) {
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(cmd.Stdout)
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return nil, err
}
if cmd.ExitStatus != 0 {
outErr, _ := ioutil.ReadAll(cmd.Stderr)
outErr, _ := io.ReadAll(cmd.Stderr)
return nil, errors.New(string(outErr))
}
lines := getLines(string(data))
Expand Down Expand Up @@ -174,13 +174,13 @@ func (i *mqlIp6tables) input() ([]interface{}, error) {
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(cmd.Stdout)
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return nil, err
}

if cmd.ExitStatus != 0 {
outErr, _ := ioutil.ReadAll(cmd.Stderr)
outErr, _ := io.ReadAll(cmd.Stderr)
return nil, errors.New(string(outErr))
}
lines := getLines(string(data))
Expand Down
9 changes: 4 additions & 5 deletions providers/os/resources/kernel/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package kernel

import (
"github.com/cockroachdb/errors"
"io"
"io/ioutil"
"regexp"
"strings"

"github.com/cockroachdb/errors"
)

var LINUX_KERNEL_ARGUMENTS_REGEX = regexp.MustCompile(`(?:^BOOT_IMAGE=([^\s]*)\s)?(?:root=([^\s]*)\s)?(.*)`)
Expand All @@ -24,7 +24,7 @@ func ParseLinuxKernelArguments(r io.Reader) (LinuxKernelArguments, error) {
Arguments: map[string]string{},
}

data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return res, err
}
Expand Down Expand Up @@ -57,8 +57,7 @@ func ParseLinuxKernelArguments(r io.Reader) (LinuxKernelArguments, error) {

// kernel version includes the kernel version, build data, buildhost, compiler version and an optional build date
func ParseLinuxKernelVersion(r io.Reader) (string, error) {

data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions providers/os/resources/kernel/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package kernel

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

Expand Down Expand Up @@ -174,7 +173,7 @@ func (s *OSXKernelManager) Info() (KernelInfo, error) {
return KernelInfo{}, errors.Wrap(err, "could not read kernel parameters")
}

data, err := ioutil.ReadAll(cmd.Stdout)
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return KernelInfo{}, errors.Wrap(err, "could not read kernel parameters")
}
Expand Down
3 changes: 1 addition & 2 deletions providers/os/resources/kernel/sysctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"archive/tar"
"bufio"
"io"
"io/ioutil"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -47,7 +46,7 @@ func ParseLinuxSysctlProc(sysctlRootPath string, reader io.Reader) (map[string]s
}

if !h.FileInfo().IsDir() {
content, _ := ioutil.ReadAll(tr)
content, _ := io.ReadAll(tr)
// remove leading sysctl path
k := strings.Replace(h.Name, sysctlRootPath, "", -1)
k = strings.Replace(k, "/", ".", -1)
Expand Down
3 changes: 1 addition & 2 deletions providers/os/resources/packages/cos_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"

"go.mondoo.com/cnquery/providers/os/connection/shared"
)
Expand Down Expand Up @@ -57,7 +56,7 @@ func (mpm *CosPkgManager) Available() (map[string]PackageUpdate, error) {
}

func ParseCosPackages(input io.Reader) ([]Package, error) {
data, err := ioutil.ReadAll(input)
data, err := io.ReadAll(input)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions providers/os/resources/packages/macos_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package packages
import (
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/cockroachdb/errors"
Expand All @@ -25,7 +24,7 @@ func ParseMacOSPackages(input io.Reader) ([]Package, error) {

// if the read seaker is not implemented lets cache stdout in-memory
if !ok {
packageList, err := ioutil.ReadAll(input)
packageList, err := io.ReadAll(input)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions providers/os/resources/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package resources
import (
"bytes"
"io"
"io/ioutil"
"sync"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -76,7 +75,7 @@ func convertToUtf8Encoding(out []byte) (string, error) {
enc, name, _ := charset.DetermineEncoding(out, "")
log.Trace().Str("encoding", name).Msg("check powershell results charset")
r := transform.NewReader(bytes.NewReader(out), enc.NewDecoder())
utf8out, err := ioutil.ReadAll(r)
utf8out, err := io.ReadAll(r)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit a6cc076

Please sign in to comment.