From 8282891f0e540e8862737d31d99f5aced20008ce Mon Sep 17 00:00:00 2001 From: Alexandru Placinta Date: Sun, 26 Nov 2023 15:45:03 +0100 Subject: [PATCH] Allow both .yaml and .yml yaml config files (#2284) --- internal/config/alias.go | 2 +- internal/config/config.go | 24 ++++++++++++++++++++++++ internal/config/hotkey.go | 2 +- internal/config/plugin.go | 9 ++------- internal/config/styles.go | 2 +- internal/config/views.go | 2 +- internal/dao/popeye.go | 4 ++-- internal/ui/config.go | 2 +- 8 files changed, 33 insertions(+), 14 deletions(-) diff --git a/internal/config/alias.go b/internal/config/alias.go index 1b5848c2f1..f8900a018a 100644 --- a/internal/config/alias.go +++ b/internal/config/alias.go @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index 7342c1df8a..9ba4a61be7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/adrg/xdg" "github.com/derailed/k9s/internal/client" @@ -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" +} diff --git a/internal/config/hotkey.go b/internal/config/hotkey.go index ea7bd715a4..ef3041de59 100644 --- a/internal/config/hotkey.go +++ b/internal/config/hotkey.go @@ -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 { diff --git a/internal/config/plugin.go b/internal/config/plugin.go index 2deca3a4b2..b7b2fa67e0 100644 --- a/internal/config/plugin.go +++ b/internal/config/plugin.go @@ -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. @@ -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())) @@ -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" -} diff --git a/internal/config/styles.go b/internal/config/styles.go index 26bb653350..62a192a0d3 100644 --- a/internal/config/styles.go +++ b/internal/config/styles.go @@ -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 { diff --git a/internal/config/views.go b/internal/config/views.go index 929c14b284..74907e4347 100644 --- a/internal/config/views.go +++ b/internal/config/views.go @@ -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 { diff --git a/internal/dao/popeye.go b/internal/dao/popeye.go index 962f0816eb..2f50906c74 100644 --- a/internal/dao/popeye.go +++ b/internal/dao/popeye.go @@ -68,9 +68,9 @@ func (p *Popeye) List(ctx context.Context, ns string) ([]runtime.Object, error) flags.Sections = §ions 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 diff --git a/internal/ui/config.go b/internal/ui/config.go index cc177fa6f8..6ab5af1f50 100644 --- a/internal/ui/config.go +++ b/internal/ui/config.go @@ -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 {