Allow both .yaml and .yml yaml config files (#2284)

mine
Alexandru Placinta 2023-11-26 15:45:03 +01:00 committed by GitHub
parent ac8395dded
commit 38275b25ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 14 deletions

View File

@ -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

View File

@ -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"
}

View File

@ -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 {

View File

@ -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"
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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 {