From 04be6eb5fc4042fb6fd1dd49312ec05d487723d9 Mon Sep 17 00:00:00 2001 From: Hamish Bultitude Date: Thu, 5 Sep 2024 19:42:44 +1000 Subject: [PATCH 1/3] init commit --- cmd/info.go | 1 + internal/config/files.go | 6 +++++- internal/model/log.go | 44 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/cmd/info.go b/cmd/info.go index 8fbb01e500..52454b2fef 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -36,6 +36,7 @@ func printInfo(cmd *cobra.Command, args []string) error { printTuple(fmat, "Plugins", config.AppPluginsFile, color.Cyan) printTuple(fmat, "Hotkeys", config.AppHotKeysFile, color.Cyan) printTuple(fmat, "Aliases", config.AppAliasesFile, color.Cyan) + printTuple(fmat, "Filters", config.AppFilterFile, color.Cyan) printTuple(fmat, "Skins", config.AppSkinsDir, color.Cyan) printTuple(fmat, "Context Configs", config.AppContextsDir, color.Cyan) printTuple(fmat, "Logs", config.AppLogFile, color.Cyan) diff --git a/internal/config/files.go b/internal/config/files.go index 2b246d5172..5f3b3c050f 100644 --- a/internal/config/files.go +++ b/internal/config/files.go @@ -80,6 +80,9 @@ var ( // AppHotKeysFile tracks hotkeys config file. AppHotKeysFile string + + // AppFilterFile tracks filter config file. + AppFilterFile string ) // InitLogLoc initializes K9s logs location. @@ -146,7 +149,7 @@ func initK9sEnvLocs() error { AppAliasesFile = filepath.Join(AppConfigDir, "aliases.yaml") AppPluginsFile = filepath.Join(AppConfigDir, "plugins.yaml") AppViewsFile = filepath.Join(AppConfigDir, "views.yaml") - + AppFilterFile = filepath.Join(AppConfigDir, "filters.yaml") return nil } @@ -167,6 +170,7 @@ func initXDGLocs() error { AppAliasesFile = filepath.Join(AppConfigDir, "aliases.yaml") AppPluginsFile = filepath.Join(AppConfigDir, "plugins.yaml") AppViewsFile = filepath.Join(AppConfigDir, "views.yaml") + AppFilterFile = filepath.Join(AppConfigDir, "filters.yaml") AppSkinsDir = filepath.Join(AppConfigDir, "skins") if err := data.EnsureFullPath(AppSkinsDir, data.DefaultDirMod); err != nil { diff --git a/internal/model/log.go b/internal/model/log.go index d194fcf017..9ecdc0a3f7 100644 --- a/internal/model/log.go +++ b/internal/model/log.go @@ -6,15 +6,19 @@ package model import ( "context" "fmt" + "os" "sync" "time" + "strings" + "github.com/derailed/k9s/internal" "github.com/derailed/k9s/internal/client" "github.com/derailed/k9s/internal/color" "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/dao" "github.com/rs/zerolog/log" + "gopkg.in/yaml.v2" ) // LogsListener represents a log model listener. @@ -38,6 +42,11 @@ type LogsListener interface { LogCanceled() } +// FilterConfig represents the structure of the filter.yaml file +type FilterConfig struct { + Filters map[string]string `yaml:"filters"` +} + // Log represents a resource logger. type Log struct { factory dao.Factory @@ -50,16 +59,19 @@ type Log struct { filter string lastSent int flushTimeout time.Duration + filterConfig FilterConfig } // NewLog returns a new model. func NewLog(gvr client.GVR, opts *dao.LogOptions, flushTimeout time.Duration) *Log { - return &Log{ + l := &Log{ gvr: gvr, logOptions: opts, lines: dao.NewLogItems(), flushTimeout: flushTimeout, } + l.loadFilterConfig() + return l } func (l *Log) GVR() client.GVR { @@ -202,14 +214,23 @@ func (l *Log) ClearFilter() { l.fireLogChanged(ll) } -// Filter filters the model using either fuzzy or regexp. +// Filter filters the model using either fuzzy, regexp, or predefined filters. func (l *Log) Filter(q string) { l.mx.Lock() { - l.filter = q + if strings.HasPrefix(q, "@") { + if customFilter, ok := l.filterConfig.Filters[q[1:]]; ok { + log.Debug().Msgf("Using custom filter: %s", customFilter) + l.filter = customFilter + } else { + log.Debug().Msgf("Failed to find custom filter for: %s", q) + l.filter = q + } + } else { + l.filter = q + } } l.mx.Unlock() - l.fireLogCleared() l.fireLogBuffChanged(0) } @@ -428,3 +449,18 @@ func (l *Log) fireLogCleared() { lis.LogCleared() } } + +// loadFilterConfig loads the filter configuration from filters.yaml +func (l *Log) loadFilterConfig() { + data, err := os.ReadFile(config.AppFilterFile) + if err != nil { + log.Error().Err(err).Msg("Failed to read filter config") + return + } + + log.Info().Msgf("Filter config: %s", string(data)) + + if err := yaml.Unmarshal(data, &l.filterConfig); err != nil { + log.Error().Err(err).Msg("Failed to parse filter config") + } +} From 185e610adec63f8e26426eb4511257d3a59a2069 Mon Sep 17 00:00:00 2001 From: Hamish Bultitude Date: Thu, 5 Sep 2024 19:55:27 +1000 Subject: [PATCH 2/3] update title --- internal/model/log.go | 5 +++++ internal/view/log.go | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/model/log.go b/internal/model/log.go index 9ecdc0a3f7..be87b445ba 100644 --- a/internal/model/log.go +++ b/internal/model/log.go @@ -214,6 +214,11 @@ func (l *Log) ClearFilter() { l.fireLogChanged(ll) } +// GetFilter returns the current filter. +func (l *Log) GetFilter() string { + return l.filter +} + // Filter filters the model using either fuzzy, regexp, or predefined filters. func (l *Log) Filter(q string) { l.mx.Lock() diff --git a/internal/view/log.go b/internal/view/log.go index be01ed5a5d..9a7053fc39 100644 --- a/internal/view/log.go +++ b/internal/view/log.go @@ -321,9 +321,9 @@ func (l *Log) updateTitle() { title += ui.SkinTitle(fmt.Sprintf(logCoFmt, path, co, since), l.app.Styles.Frame()) } - buff := l.logs.cmdBuff.GetText() - if buff != "" { - title += ui.SkinTitle(fmt.Sprintf(ui.SearchFmt, buff), l.app.Styles.Frame()) + filter := l.model.GetFilter() + if filter != "" { + title += ui.SkinTitle(fmt.Sprintf(ui.SearchFmt, filter), l.app.Styles.Frame()) } l.SetTitle(title) } From 955b602fadb12da29b2a029ba283ed4852be1dbf Mon Sep 17 00:00:00 2001 From: Hamish Bultitude Date: Thu, 5 Sep 2024 20:17:34 +1000 Subject: [PATCH 3/3] reduce warnings --- internal/model/log.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/internal/model/log.go b/internal/model/log.go index be87b445ba..812c8fb534 100644 --- a/internal/model/log.go +++ b/internal/model/log.go @@ -222,17 +222,16 @@ func (l *Log) GetFilter() string { // Filter filters the model using either fuzzy, regexp, or predefined filters. func (l *Log) Filter(q string) { l.mx.Lock() - { - if strings.HasPrefix(q, "@") { - if customFilter, ok := l.filterConfig.Filters[q[1:]]; ok { - log.Debug().Msgf("Using custom filter: %s", customFilter) - l.filter = customFilter - } else { - log.Debug().Msgf("Failed to find custom filter for: %s", q) - l.filter = q - } - } else { + if !strings.HasPrefix(q, "@") { + l.filter = q + } else { + customFilter, ok := l.filterConfig.Filters[q[1:]] + if !ok { + log.Debug().Msgf("Failed to find custom filter for: %s", q) l.filter = q + } else { + log.Debug().Msgf("Using custom filter: %s", customFilter) + l.filter = customFilter } } l.mx.Unlock() @@ -463,8 +462,6 @@ func (l *Log) loadFilterConfig() { return } - log.Info().Msgf("Filter config: %s", string(data)) - if err := yaml.Unmarshal(data, &l.filterConfig); err != nil { log.Error().Err(err).Msg("Failed to parse filter config") }