From 5166adb7c1ccc269aef22714f52ab7cab4ab235c Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Mon, 8 Nov 2021 23:47:18 +0800 Subject: [PATCH] refactor: move from io/ioutil to io and os packages (#1300) The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun --- internal/config/alias.go | 6 +++--- internal/config/bench.go | 4 ++-- internal/config/config.go | 5 ++--- internal/config/config_test.go | 6 +++--- internal/config/hotkey.go | 4 ++-- internal/config/plugin.go | 4 ++-- internal/config/styles.go | 4 ++-- internal/config/views.go | 4 ++-- internal/dao/benchmark.go | 8 +++++--- internal/dao/cruiser_test.go | 4 ++-- internal/dao/dir.go | 8 ++++---- internal/dao/ofaas.go | 4 ++-- internal/dao/registry_test.go | 4 ++-- internal/dao/screen_dump.go | 7 ++++--- internal/model/cluster_info.go | 4 ++-- internal/model/table_int_test.go | 8 ++++---- internal/model/table_test.go | 4 ++-- internal/perf/benchmark.go | 3 +-- internal/render/benchmark.go | 3 +-- internal/render/benchmark_int_test.go | 4 ++-- internal/render/dir.go | 8 ++++---- internal/render/render_test.go | 4 ++-- internal/view/benchmark.go | 4 ++-- internal/view/dir.go | 8 ++++---- internal/view/log_test.go | 6 +++--- internal/view/table_int_test.go | 6 +++--- internal/xray/container_test.go | 4 ++-- 27 files changed, 69 insertions(+), 69 deletions(-) diff --git a/internal/config/alias.go b/internal/config/alias.go index a06b0c7f..8ded5654 100644 --- a/internal/config/alias.go +++ b/internal/config/alias.go @@ -1,7 +1,7 @@ package config import ( - "io/ioutil" + "os" "path/filepath" "sync" @@ -105,7 +105,7 @@ func (a *Aliases) Load() error { // LoadFileAliases loads alias from a given file. func (a *Aliases) LoadFileAliases(path string) error { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err == nil { var aa Aliases if err := yaml.Unmarshal(f, &aa); err != nil { @@ -171,5 +171,5 @@ func (a *Aliases) SaveAliases(path string) error { if err != nil { return err } - return ioutil.WriteFile(path, cfg, 0644) + return os.WriteFile(path, cfg, 0644) } diff --git a/internal/config/bench.go b/internal/config/bench.go index 40999d6d..c3f6c4c9 100644 --- a/internal/config/bench.go +++ b/internal/config/bench.go @@ -1,8 +1,8 @@ package config import ( - "io/ioutil" "net/http" + "os" "gopkg.in/yaml.v2" ) @@ -96,7 +96,7 @@ func (s *Bench) Reload(path string) error { // Load K9s benchmark configs from file. func (s *Bench) load(path string) error { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err != nil { return err } diff --git a/internal/config/config.go b/internal/config/config.go index 0118da7f..90f7c73f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,7 +3,6 @@ package config import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" @@ -218,7 +217,7 @@ func (c *Config) SetConnection(conn client.Connection) { // Load K9s configuration from file. func (c *Config) Load(path string) error { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err != nil { return err } @@ -252,7 +251,7 @@ func (c *Config) SaveFile(path string) error { log.Error().Msgf("[Config] Unable to save K9s config file: %v", err) return err } - return ioutil.WriteFile(path, cfg, 0644) + return os.WriteFile(path, cfg, 0644) } // Validate the configuration. diff --git a/internal/config/config_test.go b/internal/config/config_test.go index eb6ec179..55f37e4d 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -2,7 +2,7 @@ package config_test import ( "fmt" - "io/ioutil" + "os" "path/filepath" "testing" @@ -216,7 +216,7 @@ func TestConfigSaveFile(t *testing.T) { err := cfg.SaveFile(path) assert.Nil(t, err) - raw, err := ioutil.ReadFile(path) + raw, err := os.ReadFile(path) assert.Nil(t, err) assert.Equal(t, expectedConfig, string(raw)) } @@ -242,7 +242,7 @@ func TestConfigReset(t *testing.T) { err := cfg.SaveFile(path) assert.Nil(t, err) - raw, err := ioutil.ReadFile(path) + raw, err := os.ReadFile(path) assert.Nil(t, err) assert.Equal(t, resetConfig, string(raw)) } diff --git a/internal/config/hotkey.go b/internal/config/hotkey.go index 5a6988ea..707bc0c7 100644 --- a/internal/config/hotkey.go +++ b/internal/config/hotkey.go @@ -1,7 +1,7 @@ package config import ( - "io/ioutil" + "os" "path/filepath" "gopkg.in/yaml.v2" @@ -36,7 +36,7 @@ func (h HotKeys) Load() error { // LoadHotKeys loads plugins from a given file. func (h HotKeys) LoadHotKeys(path string) error { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err != nil { return err } diff --git a/internal/config/plugin.go b/internal/config/plugin.go index f4e3a0a4..8cb8f48a 100644 --- a/internal/config/plugin.go +++ b/internal/config/plugin.go @@ -1,7 +1,7 @@ package config import ( - "io/ioutil" + "os" "path/filepath" "gopkg.in/yaml.v2" @@ -40,7 +40,7 @@ func (p Plugins) Load() error { // LoadPlugins loads plugins from a given file. func (p Plugins) LoadPlugins(path string) error { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err != nil { return err } diff --git a/internal/config/styles.go b/internal/config/styles.go index e1ca257b..4a856e39 100644 --- a/internal/config/styles.go +++ b/internal/config/styles.go @@ -2,7 +2,7 @@ package config import ( "fmt" - "io/ioutil" + "os" "path/filepath" "github.com/derailed/tview" @@ -541,7 +541,7 @@ func (s *Styles) Views() Views { // Load K9s configuration from file. func (s *Styles) Load(path string) error { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err != nil { return err } diff --git a/internal/config/views.go b/internal/config/views.go index 78664f1a..0d2a8407 100644 --- a/internal/config/views.go +++ b/internal/config/views.go @@ -1,7 +1,7 @@ package config import ( - "io/ioutil" + "os" "path/filepath" "gopkg.in/yaml.v2" @@ -56,7 +56,7 @@ func (v *CustomView) Reset() { // Load loads view configurations. func (v *CustomView) Load(path string) error { - raw, err := ioutil.ReadFile(path) + raw, err := os.ReadFile(path) if err != nil { return err } diff --git a/internal/dao/benchmark.go b/internal/dao/benchmark.go index 0c25af78..65f85a8e 100644 --- a/internal/dao/benchmark.go +++ b/internal/dao/benchmark.go @@ -3,7 +3,6 @@ package dao import ( "context" "errors" - "io/ioutil" "os" "path/filepath" "strings" @@ -41,7 +40,7 @@ func (b *Benchmark) List(ctx context.Context, _ string) ([]runtime.Object, error } path, _ := ctx.Value(internal.KeyPath).(string) - ff, err := ioutil.ReadDir(dir) + ff, err := os.ReadDir(dir) if err != nil { return nil, err } @@ -51,7 +50,10 @@ func (b *Benchmark) List(ctx context.Context, _ string) ([]runtime.Object, error if path != "" && !strings.HasPrefix(f.Name(), strings.Replace(path, "/", "_", 1)) { continue } - oo = append(oo, render.BenchInfo{File: f, Path: filepath.Join(dir, f.Name())}) + + if fi, err := f.Info(); err == nil { + oo = append(oo, render.BenchInfo{File: fi, Path: filepath.Join(dir, f.Name())}) + } } return oo, nil diff --git a/internal/dao/cruiser_test.go b/internal/dao/cruiser_test.go index 8574d8a4..da8769e7 100644 --- a/internal/dao/cruiser_test.go +++ b/internal/dao/cruiser_test.go @@ -3,7 +3,7 @@ package dao import ( "encoding/json" "fmt" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -29,7 +29,7 @@ func TestCruiserSlice(t *testing.T) { // Helpers... func loadJSON(t assert.TestingT, n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) assert.Nil(t, err) var o unstructured.Unstructured diff --git a/internal/dao/dir.go b/internal/dao/dir.go index d395c365..b5a3a456 100644 --- a/internal/dao/dir.go +++ b/internal/dao/dir.go @@ -3,7 +3,7 @@ package dao import ( "context" "errors" - "io/ioutil" + "os" "path/filepath" "regexp" "strings" @@ -37,7 +37,7 @@ func (a *Dir) List(ctx context.Context, _ string) ([]runtime.Object, error) { return nil, errors.New("No dir in context") } - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { return nil, err } @@ -48,8 +48,8 @@ func (a *Dir) List(ctx context.Context, _ string) ([]runtime.Object, error) { continue } oo = append(oo, render.DirRes{ - Path: filepath.Join(dir, f.Name()), - Info: f, + Path: filepath.Join(dir, f.Name()), + Entry: f, }) } diff --git a/internal/dao/ofaas.go b/internal/dao/ofaas.go index 425d07aa..4716a484 100644 --- a/internal/dao/ofaas.go +++ b/internal/dao/ofaas.go @@ -8,7 +8,7 @@ package dao // "encoding/json" // "errors" // "fmt" -// "io/ioutil" +// "io" // "net/http" // "net/url" // "os" @@ -193,7 +193,7 @@ package dao // case http.StatusUnauthorized: // return fmt.Errorf("unauthorized access, run \"faas-cli login\" to setup authentication for this server") // default: -// bytesOut, err := ioutil.ReadAll(delRes.Body) +// bytesOut, err := io.ReadAll(delRes.Body) // if err != nil { // return err // } diff --git a/internal/dao/registry_test.go b/internal/dao/registry_test.go index 28ff4ef8..4d28895b 100644 --- a/internal/dao/registry_test.go +++ b/internal/dao/registry_test.go @@ -3,7 +3,7 @@ package dao import ( "encoding/json" "fmt" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -89,7 +89,7 @@ func TestExtractString(t *testing.T) { // Helpers... func load(t *testing.T, n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) assert.Nil(t, err) var o unstructured.Unstructured diff --git a/internal/dao/screen_dump.go b/internal/dao/screen_dump.go index 09c0742b..644b958d 100644 --- a/internal/dao/screen_dump.go +++ b/internal/dao/screen_dump.go @@ -3,7 +3,6 @@ package dao import ( "context" "errors" - "io/ioutil" "os" "regexp" @@ -37,14 +36,16 @@ func (d *ScreenDump) List(ctx context.Context, _ string) ([]runtime.Object, erro return nil, errors.New("no screendump dir found in context") } - ff, err := ioutil.ReadDir(SanitizeFilename(dir)) + ff, err := os.ReadDir(SanitizeFilename(dir)) if err != nil { return nil, err } oo := make([]runtime.Object, len(ff)) for i, f := range ff { - oo[i] = render.FileRes{File: f, Dir: dir} + if fi, err := f.Info(); err == nil { + oo[i] = render.FileRes{File: fi, Dir: dir} + } } return oo, nil diff --git a/internal/model/cluster_info.go b/internal/model/cluster_info.go index f34645d6..57164e5d 100644 --- a/internal/model/cluster_info.go +++ b/internal/model/cluster_info.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "time" @@ -200,7 +200,7 @@ func fetchLatestRev() (string, error) { } }() - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { return "", err } diff --git a/internal/model/table_int_test.go b/internal/model/table_int_test.go index 9247256c..8c6c5891 100644 --- a/internal/model/table_int_test.go +++ b/internal/model/table_int_test.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "os" "testing" "github.com/derailed/k9s/internal" @@ -139,7 +139,7 @@ func TestTableGenericHydrate(t *testing.T) { // Helpers... func mustLoad(n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) if err != nil { panic(err) } @@ -151,7 +151,7 @@ func mustLoad(n string) *unstructured.Unstructured { } func load(t *testing.T, n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) assert.Nil(t, err) var o unstructured.Unstructured err = json.Unmarshal(raw, &o) @@ -160,7 +160,7 @@ func load(t *testing.T, n string) *unstructured.Unstructured { } func raw(t *testing.T, n string) []byte { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) assert.Nil(t, err) return raw } diff --git a/internal/model/table_test.go b/internal/model/table_test.go index e41b584b..08d31f44 100644 --- a/internal/model/table_test.go +++ b/internal/model/table_test.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "os" "testing" "github.com/derailed/k9s/internal" @@ -122,7 +122,7 @@ func makeTableFactory() tableFactory { } func mustLoad(n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) if err != nil { panic(err) } diff --git a/internal/perf/benchmark.go b/internal/perf/benchmark.go index f5ec8294..c5fd19d9 100644 --- a/internal/perf/benchmark.go +++ b/internal/perf/benchmark.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -138,7 +137,7 @@ func (b *Benchmark) save(cluster string, r io.Reader) error { } }() - bb, err := ioutil.ReadAll(r) + bb, err := io.ReadAll(r) if err != nil { return err } diff --git a/internal/render/benchmark.go b/internal/render/benchmark.go index 4b586262..710660ea 100644 --- a/internal/render/benchmark.go +++ b/internal/render/benchmark.go @@ -3,7 +3,6 @@ package render import ( "errors" "fmt" - "io/ioutil" "os" "regexp" "strconv" @@ -97,7 +96,7 @@ func (Benchmark) diagnose(ns string, ff Fields) error { // Helpers... func (Benchmark) readFile(file string) (string, error) { - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { return "", err } diff --git a/internal/render/benchmark_int_test.go b/internal/render/benchmark_int_test.go index 6c7c5384..4fe09639 100644 --- a/internal/render/benchmark_int_test.go +++ b/internal/render/benchmark_int_test.go @@ -1,7 +1,7 @@ package render import ( - "io/ioutil" + "os" "testing" "github.com/rs/zerolog" @@ -38,7 +38,7 @@ func TestAugmentRow(t *testing.T) { for k := range uu { u := uu[k] t.Run(k, func(t *testing.T) { - data, err := ioutil.ReadFile(u.file) + data, err := os.ReadFile(u.file) assert.Nil(t, err) fields := make(Fields, 8) diff --git a/internal/render/dir.go b/internal/render/dir.go index 12a3e941..d955fb98 100644 --- a/internal/render/dir.go +++ b/internal/render/dir.go @@ -35,10 +35,10 @@ func (Dir) Render(o interface{}, ns string, r *Row) error { } name := "🦄 " - if d.Info.IsDir() { + if d.Entry.IsDir() { name = "📁 " } - name += d.Info.Name() + name += d.Entry.Name() r.ID, r.Fields = d.Path, append(r.Fields, name) return nil @@ -49,8 +49,8 @@ func (Dir) Render(o interface{}, ns string, r *Row) error { // DirRes represents an alias resource. type DirRes struct { - Info os.FileInfo - Path string + Entry os.DirEntry + Path string } // GetObjectKind returns a schema object. diff --git a/internal/render/render_test.go b/internal/render/render_test.go index cd2f6492..20a1c331 100644 --- a/internal/render/render_test.go +++ b/internal/render/render_test.go @@ -3,7 +3,7 @@ package render_test import ( "encoding/json" "fmt" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -13,7 +13,7 @@ import ( // Helpers... func load(t testing.TB, n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) assert.Nil(t, err) var o unstructured.Unstructured diff --git a/internal/view/benchmark.go b/internal/view/benchmark.go index 892cdb23..f9b795d0 100644 --- a/internal/view/benchmark.go +++ b/internal/view/benchmark.go @@ -2,7 +2,7 @@ package view import ( "context" - "io/ioutil" + "os" "path/filepath" "strings" @@ -71,7 +71,7 @@ func benchDir(cfg *config.Config) string { } func readBenchFile(cfg *config.Config, n string) (string, error) { - data, err := ioutil.ReadFile(filepath.Join(benchDir(cfg), n)) + data, err := os.ReadFile(filepath.Join(benchDir(cfg), n)) if err != nil { return "", err } diff --git a/internal/view/dir.go b/internal/view/dir.go index 13c35d8b..c151714b 100644 --- a/internal/view/dir.go +++ b/internal/view/dir.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "os" "path" "strings" @@ -90,7 +90,7 @@ func (d *Dir) viewCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } - yaml, err := ioutil.ReadFile(sel) + yaml, err := os.ReadFile(sel) if err != nil { d.App().Flash().Err(err) return nil @@ -157,7 +157,7 @@ func isKustomized(sel string) bool { return false } - ff, err := ioutil.ReadDir(sel) + ff, err := os.ReadDir(sel) if err != nil { return false } @@ -176,7 +176,7 @@ func containsDir(sel string) bool { return false } - ff, err := ioutil.ReadDir(sel) + ff, err := os.ReadDir(sel) if err != nil { return false } diff --git a/internal/view/log_test.go b/internal/view/log_test.go index c0454bb1..501f8e92 100644 --- a/internal/view/log_test.go +++ b/internal/view/log_test.go @@ -3,7 +3,7 @@ package view_test import ( "bytes" "fmt" - "io/ioutil" + "os" "path/filepath" "testing" @@ -79,9 +79,9 @@ func TestLogViewSave(t *testing.T) { v.Flush(ii.Lines(false)) config.K9sDumpDir = "/tmp" dir := filepath.Join(config.K9sDumpDir, app.Config.K9s.CurrentCluster) - c1, _ := ioutil.ReadDir(dir) + c1, _ := os.ReadDir(dir) v.SaveCmd(nil) - c2, _ := ioutil.ReadDir(dir) + c2, _ := os.ReadDir(dir) assert.Equal(t, len(c2), len(c1)+1) } diff --git a/internal/view/table_int_test.go b/internal/view/table_int_test.go index 0ddab5fd..6209f194 100644 --- a/internal/view/table_int_test.go +++ b/internal/view/table_int_test.go @@ -2,7 +2,7 @@ package view import ( "context" - "io/ioutil" + "os" "path/filepath" "testing" "time" @@ -25,10 +25,10 @@ func TestTableSave(t *testing.T) { v.SetTitle("k9s-test") dir := filepath.Join(config.K9sDumpDir, v.app.Config.K9s.CurrentCluster) - c1, _ := ioutil.ReadDir(dir) + c1, _ := os.ReadDir(dir) v.saveCmd(nil) - c2, _ := ioutil.ReadDir(dir) + c2, _ := os.ReadDir(dir) assert.Equal(t, len(c2), len(c1)+1) } diff --git a/internal/xray/container_test.go b/internal/xray/container_test.go index 4e66d0f8..d635525a 100644 --- a/internal/xray/container_test.go +++ b/internal/xray/container_test.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "os" "testing" "github.com/derailed/k9s/internal" @@ -238,7 +238,7 @@ func makeDoubleCMKeysContainer(n string, optional bool) *v1.Container { } func load(t *testing.T, n string) *unstructured.Unstructured { - raw, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.json", n)) + raw, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) assert.Nil(t, err) var o unstructured.Unstructured