Skip to content

Commit

Permalink
Deprecation of package ioutil in Go 1.16
Browse files Browse the repository at this point in the history
ioutil.ReadFile(...) became -> os.ReadFile(...)

go-critic/go-critic#1019
  • Loading branch information
ArangoGutierrez committed Feb 1, 2023
1 parent 4559ae9 commit 0a5e5da
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 71 deletions.
3 changes: 1 addition & 2 deletions altsrc/json_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -318,7 +317,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *tes
}

func writeTempFile(t *testing.T, name string, content string) func() {
if err := ioutil.WriteFile(name, []byte(content), 0666); err != nil {
if err := os.WriteFile(name, []byte(content), 0666); err != nil {
t.Fatalf("cannot write %q: %v", name, err)
}
return func() {
Expand Down
3 changes: 1 addition & 2 deletions altsrc/json_source_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"time"

Expand Down Expand Up @@ -40,7 +39,7 @@ func NewJSONSourceFromFile(f string) (InputSourceContext, error) {
// NewJSONSourceFromReader returns an InputSourceContext suitable for
// retrieving config variables from an io.Reader that returns JSON data.
func NewJSONSourceFromReader(r io.Reader) (InputSourceContext, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
19 changes: 9 additions & 10 deletions altsrc/toml_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand All @@ -12,7 +11,7 @@ import (
func TestCommandTomFileTest(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")
test := []string{"test-cmd", "--load", "current.toml"}
_ = set.Parse(test)
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestCommandTomFileTest(t *testing.T) {
func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "10")
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "10")
Expand Down Expand Up @@ -110,7 +109,7 @@ func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

test := []string{"test-cmd", "--load", "current.toml", "--test", "7"}
Expand Down Expand Up @@ -142,7 +141,7 @@ func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte(`[top]
_ = os.WriteFile("current.toml", []byte(`[top]
test = 15`), 0666)
defer os.Remove("current.toml")

Expand Down Expand Up @@ -175,7 +174,7 @@ func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

test := []string{"test-cmd", "--load", "current.toml"}
Expand Down Expand Up @@ -207,7 +206,7 @@ func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
defer os.Remove("current.toml")

test := []string{"test-cmd", "--load", "current.toml"}
Expand Down Expand Up @@ -239,7 +238,7 @@ func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "11")
Expand Down Expand Up @@ -273,7 +272,7 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
defer os.Remove("current.toml")

_ = os.Setenv("THE_TEST", "11")
Expand Down
19 changes: 9 additions & 10 deletions altsrc/yaml_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand All @@ -12,7 +11,7 @@ import (
func TestCommandYamlFileTest(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")
test := []string{"test-cmd", "--load", "current.yaml"}
_ = set.Parse(test)
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestCommandYamlFileTest(t *testing.T) {
func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

_ = os.Setenv("THE_TEST", "10")
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down Expand Up @@ -111,7 +110,7 @@ func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down Expand Up @@ -176,7 +175,7 @@ func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

test := []string{"test-cmd", "--load", "current.yaml"}
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down Expand Up @@ -241,7 +240,7 @@ func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
defer os.Remove("current.yaml")

_ = os.Setenv("THE_TEST", "11")
Expand Down Expand Up @@ -275,7 +274,7 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
_ = ioutil.WriteFile("current.yaml", []byte(`top:
_ = os.WriteFile("current.yaml", []byte(`top:
test: 15`), 0666)
defer os.Remove("current.yaml")

Expand Down
8 changes: 4 additions & 4 deletions altsrc/yaml_file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package altsrc

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -68,21 +68,21 @@ func loadDataFrom(filePath string) ([]byte, error) {
if err != nil {
return nil, err
}
return ioutil.ReadAll(res.Body)
return io.ReadAll(res.Body)
default:
return nil, fmt.Errorf("scheme of %s is unsupported", filePath)
}
} else if u.Path != "" { // i dont have a host, but I have a path. I am a local file.
if _, notFoundFileErr := os.Stat(filePath); notFoundFileErr != nil {
return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath)
}
return ioutil.ReadFile(filePath)
return os.ReadFile(filePath)
} else if runtime.GOOS == "windows" && strings.Contains(u.String(), "\\") {
// on Windows systems u.Path is always empty, so we need to check the string directly.
if _, notFoundFileErr := os.Stat(filePath); notFoundFileErr != nil {
return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath)
}
return ioutil.ReadFile(filePath)
return os.ReadFile(filePath)
}

return nil, fmt.Errorf("unable to determine how to load from path %s", filePath)
Expand Down
21 changes: 10 additions & 11 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -1278,7 +1277,7 @@ func TestApp_BeforeFunc(t *testing.T) {
Flags: []Flag{
&StringFlag{Name: "opt"},
},
Writer: ioutil.Discard,
Writer: io.Discard,
}

// run with the Before() func succeeding
Expand Down Expand Up @@ -1369,7 +1368,7 @@ func TestApp_BeforeAfterFuncShellCompletion(t *testing.T) {
Flags: []Flag{
&StringFlag{Name: "opt"},
},
Writer: ioutil.Discard,
Writer: io.Discard,
}

// run with the Before() func succeeding
Expand Down Expand Up @@ -1487,7 +1486,7 @@ func TestAppNoHelpFlag(t *testing.T) {

HelpFlag = nil

app := &App{Writer: ioutil.Discard}
app := &App{Writer: io.Discard}
err := app.Run([]string{"test", "-h"})

if err != flag.ErrHelp {
Expand Down Expand Up @@ -1718,7 +1717,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
counts.OnUsageError = counts.Total
return errors.New("hay OnUsageError")
},
Writer: ioutil.Discard,
Writer: io.Discard,
}

beforeNoError := func(c *Context) error {
Expand Down Expand Up @@ -2308,7 +2307,7 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
Action: func(c *Context) error { return nil },
Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") },
Writer: ioutil.Discard,
Writer: io.Discard,
}

err := app.Run([]string{"foo"})
Expand Down Expand Up @@ -2458,7 +2457,7 @@ func (c *customBoolFlag) IsSet() bool {
func TestCustomFlagsUnused(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
Writer: io.Discard,
}

err := app.Run([]string{"foo"})
Expand All @@ -2470,7 +2469,7 @@ func TestCustomFlagsUnused(t *testing.T) {
func TestCustomFlagsUsed(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
Writer: io.Discard,
}

err := app.Run([]string{"foo", "--custom=bar"})
Expand All @@ -2481,7 +2480,7 @@ func TestCustomFlagsUsed(t *testing.T) {

func TestCustomHelpVersionFlags(t *testing.T) {
app := &App{
Writer: ioutil.Discard,
Writer: io.Discard,
}

// Be sure to reset the global flags
Expand Down Expand Up @@ -2573,7 +2572,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
Action: func(ctx *Context) error {
return fmt.Errorf("should not get here")
},
Writer: ioutil.Discard,
Writer: io.Discard,
}
err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"})
if err != nil {
Expand Down Expand Up @@ -2636,7 +2635,7 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {

func newTestApp() *App {
a := NewApp()
a.Writer = ioutil.Discard
a.Writer = io.Discard
return a
}

Expand Down
Loading

0 comments on commit 0a5e5da

Please sign in to comment.