-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config.go
163 lines (143 loc) · 4.15 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package ui
import (
"context"
"fmt"
"path/filepath"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/tview"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
)
// Synchronizer manages ui event queue.
type synchronizer interface {
QueueUpdateDraw(func()) *tview.Application
QueueUpdate(func()) *tview.Application
}
// Configurator represents an application configurationa.
type Configurator struct {
Config *config.Config
Styles *config.Styles
CustomView *config.CustomView
BenchFile string
skinFile string
}
// HasSkin returns true if a skin file was located.
func (c *Configurator) HasSkin() bool {
return c.skinFile != ""
}
// CustomViewsWatcher watches for view config file changes.
func (c *Configurator) CustomViewsWatcher(ctx context.Context, s synchronizer) error {
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case evt := <-w.Events:
_ = evt
s.QueueUpdateDraw(func() {
c.RefreshCustomViews()
})
case err := <-w.Errors:
log.Info().Err(err).Msg("CustomView watcher failed")
return
case <-ctx.Done():
log.Debug().Msgf("CustomViewWatcher Done `%s!!", config.K9sViewConfigFile)
if err := w.Close(); err != nil {
log.Error().Err(err).Msg("Closing CustomView watcher")
}
return
}
}
}()
log.Debug().Msgf("CustomView watching `%s", config.K9sViewConfigFile)
c.RefreshCustomViews()
return w.Add(config.K9sViewConfigFile)
}
// RefreshCustomViews load view configuration changes.
func (c *Configurator) RefreshCustomViews() {
if c.CustomView == nil {
c.CustomView = config.NewCustomView()
} else {
c.CustomView.Reset()
}
if err := c.CustomView.Load(config.K9sViewConfigFile); err != nil {
log.Error().Err(err).Msgf("Custom view load failed %s", config.K9sViewConfigFile)
return
}
}
// StylesWatcher watches for skin file changes.
func (c *Configurator) StylesWatcher(ctx context.Context, s synchronizer) error {
if !c.HasSkin() {
return nil
}
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case evt := <-w.Events:
_ = evt
s.QueueUpdateDraw(func() {
c.RefreshStyles(c.Config.K9s.CurrentCluster)
})
case err := <-w.Errors:
log.Info().Err(err).Msg("Skin watcher failed")
return
case <-ctx.Done():
log.Debug().Msgf("SkinWatcher Done `%s!!", c.skinFile)
if err := w.Close(); err != nil {
log.Error().Err(err).Msg("Closing Skin watcher")
}
return
}
}
}()
log.Debug().Msgf("SkinWatcher watching `%s", c.skinFile)
return w.Add(c.skinFile)
}
// BenchConfig location of the benchmarks configuration file.
func BenchConfig(context string) string {
return filepath.Join(config.K9sHome(), config.K9sBench+"-"+context+".yml")
}
// RefreshStyles load for skin configuration changes.
func (c *Configurator) RefreshStyles(context string) {
c.BenchFile = BenchConfig(context)
clusterSkins := filepath.Join(config.K9sHome(), fmt.Sprintf("%s_skin.yml", context))
if c.Styles == nil {
c.Styles = config.NewStyles()
} else {
c.Styles.Reset()
}
if err := c.Styles.Load(clusterSkins); err != nil {
log.Info().Msgf("No context specific skin file found -- %s", clusterSkins)
} else {
c.updateStyles(clusterSkins)
return
}
if err := c.Styles.Load(config.K9sStylesFile); err != nil {
log.Info().Msgf("No skin file found -- %s. Loading stock skins.", config.K9sStylesFile)
c.updateStyles("")
return
}
c.updateStyles(config.K9sStylesFile)
}
func (c *Configurator) updateStyles(f string) {
c.skinFile = f
if !c.HasSkin() {
c.Styles.DefaultSkin()
}
c.Styles.Update()
render.ModColor = c.Styles.Frame().Status.ModifyColor.Color()
render.AddColor = c.Styles.Frame().Status.AddColor.Color()
render.ErrColor = c.Styles.Frame().Status.ErrorColor.Color()
render.StdColor = c.Styles.Frame().Status.NewColor.Color()
render.PendingColor = c.Styles.Frame().Status.PendingColor.Color()
render.HighlightColor = c.Styles.Frame().Status.HighlightColor.Color()
render.KillColor = c.Styles.Frame().Status.KillColor.Color()
render.CompletedColor = c.Styles.Frame().Status.CompletedColor.Color()
}