Skip to content

Commit

Permalink
Allow both .yaml and .yml yaml config files (derailed#2284)
Browse files Browse the repository at this point in the history
  • Loading branch information
placintaalexandru authored and thejoeejoee committed Jan 31, 2024
1 parent 406e2b5 commit 8282891
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// K9sAlias manages K9s aliases.
var K9sAlias = filepath.Join(K9sHome(), "alias.yml")
var K9sAlias = YamlExtension(filepath.Join(K9sHome(), "alias.yml"))

// Alias tracks shortname to GVR mappings.
type Alias map[string]string
Expand Down
24 changes: 24 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/adrg/xdg"
"github.com/derailed/k9s/internal/client"
Expand Down Expand Up @@ -274,9 +275,32 @@ func (c *Config) Dump(msg string) {
}
}

// YamlExtension tries to find the correct extension for a YAML file
func YamlExtension(path string) string {
if !isYamlFile(path) {
log.Error().Msgf("Config: File %s is not a yaml file", path)
return path
}

// Strip any extension, if there is no extension the path will remain unchanged
path = strings.TrimSuffix(path, filepath.Ext(path))
result := path + ".yml"

if _, err := os.Stat(result); os.IsNotExist(err) {
return path + ".yaml"
}

return result
}

// ----------------------------------------------------------------------------
// Helpers...

func isSet(s *string) bool {
return s != nil && len(*s) > 0
}

func isYamlFile(file string) bool {
ext := filepath.Ext(file)
return ext == ".yml" || ext == ".yaml"
}
2 changes: 1 addition & 1 deletion internal/config/hotkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// K9sHotKeys manages K9s hotKeys.
var K9sHotKeys = filepath.Join(K9sHome(), "hotkey.yml")
var K9sHotKeys = YamlExtension(filepath.Join(K9sHome(), "hotkey.yml"))

// HotKeys represents a collection of plugins.
type HotKeys struct {
Expand Down
9 changes: 2 additions & 7 deletions internal/config/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// K9sPluginsFilePath manages K9s plugins.
var K9sPluginsFilePath = filepath.Join(K9sHome(), "plugin.yml")
var K9sPluginsFilePath = YamlExtension(filepath.Join(K9sHome(), "plugin.yml"))
var K9sPluginDirectory = filepath.Join("k9s", "plugins")

// Plugins represents a collection of plugins.
Expand Down Expand Up @@ -73,7 +73,7 @@ func (p Plugins) LoadPlugins(path string, pluginDirs []string) error {
continue
}
for _, file := range pluginFiles {
if file.IsDir() || !isYamlFile(file) {
if file.IsDir() || !isYamlFile(file.Name()) {
continue
}
pluginFile, err := os.ReadFile(filepath.Join(pluginDir, file.Name()))
Expand All @@ -94,8 +94,3 @@ func (p Plugins) LoadPlugins(path string, pluginDirs []string) error {

return nil
}

func isYamlFile(file os.DirEntry) bool {
ext := filepath.Ext(file.Name())
return ext == ".yml" || ext == ".yaml"
}
2 changes: 1 addition & 1 deletion internal/config/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// K9sStylesFile represents K9s skins file location.
var K9sStylesFile = filepath.Join(K9sHome(), "skin.yml")
var K9sStylesFile = YamlExtension(filepath.Join(K9sHome(), "skin.yml"))

// StyleListener represents a skin's listener.
type StyleListener interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// K9sViewConfigFile represents the location for the views configuration.
var K9sViewConfigFile = filepath.Join(K9sHome(), "views.yml")
var K9sViewConfigFile = YamlExtension(filepath.Join(K9sHome(), "views.yml"))

// ViewConfigListener represents a view config listener.
type ViewConfigListener interface {
Expand Down
4 changes: 2 additions & 2 deletions internal/dao/popeye.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func (p *Popeye) List(ctx context.Context, ns string) ([]runtime.Object, error)
flags.Sections = &sections
flags.ActiveNamespace = &ns
}
spinach := filepath.Join(cfg.K9sHome(), "spinach.yml")
spinach := cfg.YamlExtension(filepath.Join(cfg.K9sHome(), "spinach.yml"))
if c, err := p.GetFactory().Client().Config().CurrentContextName(); err == nil {
spinach = filepath.Join(cfg.K9sHome(), fmt.Sprintf("%s_spinach.yml", c))
spinach = cfg.YamlExtension(filepath.Join(cfg.K9sHome(), fmt.Sprintf("%s_spinach.yml", c)))
}
if _, err := os.Stat(spinach); err == nil {
flags.Spinach = &spinach
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func BenchConfig(context string) string {
func (c *Configurator) RefreshStyles(context string) {
c.BenchFile = BenchConfig(context)

clusterSkins := filepath.Join(config.K9sHome(), fmt.Sprintf("%s_skin.yml", context))
clusterSkins := config.YamlExtension(filepath.Join(config.K9sHome(), fmt.Sprintf("%s_skin.yml", context)))
if c.Styles == nil {
c.Styles = config.NewStyles()
} else {
Expand Down

0 comments on commit 8282891

Please sign in to comment.