Skip to content

Commit

Permalink
[Bug] Fix #2569
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Mar 2, 2024
1 parent 02098d4 commit d246247
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 62 deletions.
4 changes: 3 additions & 1 deletion internal/config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package config

import (
"errors"
"fmt"
"io/fs"
"os"
"sync"

Expand Down Expand Up @@ -136,7 +138,7 @@ func (a *Aliases) LoadFile(path string) error {
if path == "" {
return nil
}
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config
import (
"errors"
"fmt"
"io/fs"
"os"

"github.com/derailed/k9s/internal/client"
Expand Down Expand Up @@ -52,14 +53,13 @@ func (c *Config) ContextAliasesPath() string {
}

// ContextPluginsPath returns a context specific plugins file spec.
func (c *Config) ContextPluginsPath() string {
func (c *Config) ContextPluginsPath() (string, error) {
ct, err := c.K9s.ActiveContext()
if err != nil {
log.Error().Err(err).Msgf("active context load failed")
return ""
return "", err
}

return AppContextPluginsFile(ct.GetClusterName(), c.K9s.activeContextName)
return AppContextPluginsFile(ct.GetClusterName(), c.K9s.activeContextName), nil
}

// Refine the configuration based on cli args.
Expand Down Expand Up @@ -210,7 +210,7 @@ func (c *Config) Merge(c1 *Config) {

// Load loads K9s configuration from file.
func (c *Config) Load(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
if err := c.Save(); err != nil {
return err
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *Config) Save() error {
if err := c.K9s.Save(); err != nil {
return err
}
if _, err := os.Stat(AppConfigFile); os.IsNotExist(err) {
if _, err := os.Stat(AppConfigFile); errors.Is(err, fs.ErrNotExist) {
return c.SaveFile(AppConfigFile)
}

Expand Down
24 changes: 13 additions & 11 deletions internal/config/data/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package data
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -34,20 +35,21 @@ func (d *Dir) Load(n string, ct *api.Context) (*Config, error) {
if ct == nil {
return nil, errors.New("api.Context must not be nil")
}
var (
path = filepath.Join(d.root, SanitizeContextSubpath(ct.Cluster, n), MainConfigFile)
cfg *Config
err error
)
if f, e := os.Stat(path); os.IsNotExist(e) || f.Size() == 0 {
var path = filepath.Join(d.root, SanitizeContextSubpath(ct.Cluster, n), MainConfigFile)

f, err := os.Stat(path)
if errors.Is(err, fs.ErrPermission) {
return nil, err
}
if errors.Is(err, fs.ErrNotExist) || (f != nil && f.Size() == 0) {
log.Debug().Msgf("Context config not found! Generating... %q", path)
cfg, err = d.genConfig(path, ct)
} else {
log.Debug().Msgf("Found existing context config: %q", path)
cfg, err = d.loadConfig(path)
return d.genConfig(path, ct)
}
if err != nil {
return nil, err
}

return cfg, err
return d.loadConfig(path)
}

func (d *Dir) genConfig(path string, ct *api.Context) (*Config, error) {
Expand Down
4 changes: 3 additions & 1 deletion internal/config/data/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package data

import (
"errors"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -38,7 +40,7 @@ func EnsureDirPath(path string, mod os.FileMode) error {

// EnsureFullPath ensures a directory exist from the given path.
func EnsureFullPath(path string, mod os.FileMode) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
if err = os.MkdirAll(path, mod); err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions internal/config/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package config

import (
_ "embed"
"errors"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -238,7 +240,7 @@ func EnsureBenchmarksCfgFile(cluster, context string) (string, error) {
if err := data.EnsureDirPath(f, data.DefaultDirMod); err != nil {
return "", err
}
if _, err := os.Stat(f); os.IsNotExist(err) {
if _, err := os.Stat(f); errors.Is(err, fs.ErrNotExist) {
return f, os.WriteFile(f, benchmarkTpl, data.DefaultFileMod)
}

Expand All @@ -251,7 +253,7 @@ func EnsureAliasesCfgFile() (string, error) {
if err := data.EnsureDirPath(f, data.DefaultDirMod); err != nil {
return "", err
}
if _, err := os.Stat(f); os.IsNotExist(err) {
if _, err := os.Stat(f); errors.Is(err, fs.ErrNotExist) {
return f, os.WriteFile(f, aliasesTpl, data.DefaultFileMod)
}

Expand All @@ -264,7 +266,7 @@ func EnsureHotkeysCfgFile() (string, error) {
if err := data.EnsureDirPath(f, data.DefaultDirMod); err != nil {
return "", err
}
if _, err := os.Stat(f); os.IsNotExist(err) {
if _, err := os.Stat(f); errors.Is(err, fs.ErrNotExist) {
return f, os.WriteFile(f, hotkeysTpl, data.DefaultFileMod)
}

Expand Down
6 changes: 4 additions & 2 deletions internal/config/hotkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package config

import (
"errors"
"fmt"
"io/fs"
"os"

"github.com/derailed/k9s/internal/config/data"
Expand Down Expand Up @@ -38,7 +40,7 @@ func (h HotKeys) Load(path string) error {
if err := h.LoadHotKeys(AppHotKeysFile); err != nil {
return err
}
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return nil
}

Expand All @@ -47,7 +49,7 @@ func (h HotKeys) Load(path string) error {

// LoadHotKeys loads plugins from a given file.
func (h HotKeys) LoadHotKeys(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return nil
}
bb, err := os.ReadFile(path)
Expand Down
4 changes: 3 additions & 1 deletion internal/config/mock/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package mock

import (
"errors"
"fmt"
"io/fs"
"os"
"strings"

Expand All @@ -21,7 +23,7 @@ import (
)

func EnsureDir(d string) error {
if _, err := os.Stat(d); os.IsNotExist(err) {
if _, err := os.Stat(d); errors.Is(err, fs.ErrNotExist) {
return os.MkdirAll(d, 0700)
}
if err := os.RemoveAll(d); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/config/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (p Plugins) loadPluginDir(dir string) error {
}

func (p *Plugins) load(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return nil
}
bb, err := os.ReadFile(path)
Expand Down
12 changes: 11 additions & 1 deletion internal/config/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package config

import (
"errors"
"fmt"
"io/fs"
"os"
"strings"

Expand All @@ -26,6 +28,14 @@ type ViewSetting struct {
SortColumn string `yaml:"sortColumn"`
}

func (v *ViewSetting) HasCols() bool {
return len(v.Columns) > 0
}

func (v *ViewSetting) IsBlank() bool {
return v == nil || len(v.Columns) == 0
}

func (v *ViewSetting) SortCol() (string, bool, error) {
if v == nil || v.SortColumn == "" {
return "", false, fmt.Errorf("no sort column specified")
Expand Down Expand Up @@ -61,7 +71,7 @@ func (v *CustomView) Reset() {

// Load loads view configurations.
func (v *CustomView) Load(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return nil
}
bb, err := os.ReadFile(path)
Expand Down
7 changes: 4 additions & 3 deletions internal/ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ui
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -91,7 +92,7 @@ func (c *Configurator) RefreshCustomViews() error {

// SkinsDirWatcher watches for skin directory file changes.
func (c *Configurator) SkinsDirWatcher(ctx context.Context, s synchronizer) error {
if _, err := os.Stat(config.AppSkinsDir); os.IsNotExist(err) {
if _, err := os.Stat(config.AppSkinsDir); errors.Is(err, fs.ErrNotExist) {
return err
}
w, err := fsnotify.NewWatcher()
Expand Down Expand Up @@ -190,14 +191,14 @@ func (c *Configurator) activeSkin() (string, bool) {
}

if ct, err := c.Config.K9s.ActiveContext(); err == nil && ct.Skin != "" {
if _, err := os.Stat(config.SkinFileFromName(ct.Skin)); !os.IsNotExist(err) {
if _, err := os.Stat(config.SkinFileFromName(ct.Skin)); err == nil {
skin = ct.Skin
log.Debug().Msgf("[Skin] Loading context skin (%q) from %q", skin, c.Config.K9s.ActiveContextName())
}
}

if sk := c.Config.K9s.UI.Skin; skin == "" && sk != "" {
if _, err := os.Stat(config.SkinFileFromName(sk)); !os.IsNotExist(err) {
if _, err := os.Stat(config.SkinFileFromName(sk)); err == nil {
skin = sk
log.Debug().Msgf("[Skin] Loading global skin (%q)", skin)
}
Expand Down
11 changes: 8 additions & 3 deletions internal/view/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ func pluginActions(r Runner, aa *ui.KeyActions) error {
}
})

var errs error
if err := pp.Load(r.App().Config.ContextPluginsPath()); err != nil {
errs = errors.Join(errs, err)
path, err := r.App().Config.ContextPluginsPath()
if err != nil {
return err
}
if err := pp.Load(path); err != nil {
return err
}

var errs error
aliases := r.Aliases()
for k, plugin := range pp.Plugins {
if !inScope(plugin.Scopes, aliases) {
Expand Down
4 changes: 3 additions & 1 deletion internal/view/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package view_test

import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"testing"

Expand Down Expand Up @@ -154,7 +156,7 @@ func makeApp() *view.App {

func ensureDumpDir(n string) error {
config.AppDumpsDir = n
if _, err := os.Stat(n); os.IsNotExist(err) {
if _, err := os.Stat(n); errors.Is(err, fs.ErrNotExist) {
return os.MkdirAll(n, 0700)
}
if err := os.RemoveAll(n); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions internal/view/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"strings"

Expand Down Expand Up @@ -35,6 +36,7 @@ const (
osBetaSelector = "beta." + osSelector
trUpload = "Upload"
trDownload = "Download"
pfIndicator = "[orange::b]Ⓕ"
)

// Pod represents a pod viewer.
Expand Down Expand Up @@ -63,15 +65,14 @@ func (p *Pod) portForwardIndicator(data *model1.TableData) {
ff := p.App().factory.Forwarders()

defer decorateCpuMemHeaderRows(p.App(), data)
col := data.IndexOfHeader("PF")
if col < 0 {
idx, ok := data.IndexOfHeader("PF")
if !ok {
return
}

const idc = "[orange::b]Ⓕ"
data.RowsRange(func(_ int, re model1.RowEvent) bool {
if ff.IsPodForwarded(re.Row.ID) {
re.Row.Fields[col] = idc
re.Row.Fields[idx] = pfIndicator
}
return true
})
Expand Down Expand Up @@ -314,7 +315,7 @@ func (p *Pod) transferCmd(evt *tcell.EventKey) *tcell.EventKey {
if !download {
local = from
}
if _, err := os.Stat(local); !download && os.IsNotExist(err) {
if _, err := os.Stat(local); !download && errors.Is(err, fs.ErrNotExist) {
p.App().Flash().Err(err)
return false
}
Expand Down
Loading

0 comments on commit d246247

Please sign in to comment.