-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (122 loc) · 3.35 KB
/
main.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
package main
import (
"io"
"os"
"path/filepath"
"runtime"
"time"
"github.com/getlantern/systray"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
log "github.com/sirupsen/logrus"
"github.com/solacens/kube-tray/icon"
"github.com/spf13/viper"
"k8s.io/client-go/util/homedir"
)
var (
contextDirectory string
existingContext []string
rootElement *Element
autoRefresh bool
trayLog *log.Entry
kubeLog *log.Entry
)
func init() {
// Home directory
home := homedir.HomeDir()
// Logger setting
logFilePath := filepath.Join(home, ".kube-tray", "log.")
r, _ := rotatelogs.New(logFilePath + "%Y%m%d")
mw := io.MultiWriter(os.Stdout, r)
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(mw)
log.SetLevel(log.InfoLevel)
trayLog = log.WithFields(log.Fields{
"type": "tray",
})
kubeLog = log.WithFields(log.Fields{
"type": "kube",
})
// Config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(filepath.Join(home, ".kube-tray"))
err := viper.ReadInConfig()
if err != nil {
// Default terminal command
if runtime.GOOS == "windows" {
// cmd /c wt -w 0 nt
viper.Set("shell.command", []string{"cmd", "/c", "wt", "-w", "0", "nt"})
} else {
// bash
viper.Set("shell.command", []string{"bash"}) // Untested: Darwin & Linux
}
// Auto refresh
viper.Set("auto-refresh.enabled", false)
viper.Set("auto-refresh.interval", 3600)
// WriteConfigAs requried for first time creation
viper.WriteConfigAs(filepath.Join(home, ".kube-tray", "config.yaml"))
}
autoRefresh = viper.GetBool("auto-refresh.enabled")
}
func main() {
LoadKubeconfig(false)
systray.Run(onTrayReady, func() {})
}
func onTrayReady() {
systray.SetTemplateIcon(icon.Data, icon.Data)
systray.SetTitle("K8S Tray")
systray.SetTooltip("Kubernetes Tray")
//////////////////////////////////
quitMenuItem := systray.AddMenuItem("Quit", "Quit")
//////////////////////////////////
systray.AddSeparator()
//////////////////////////////////
reloadMenuItem := systray.AddMenuItem("Reload Kubeconfig", "Reload Kubeconfig")
reloadMenuItemFunc := func() {
trayLog.Info("Reloading config")
LoadKubeconfig(true)
go rootElement.UpdateData()
trayLog.Info("Reloaded")
}
//////////////////////////////////
autoRefreshMenuItem := systray.AddMenuItemCheckbox("Auto Refresh", "Auto Refresh", autoRefresh)
autoRefreshTicker := time.NewTicker(viper.GetDuration("auto-refresh.interval") * time.Second)
autoRefreshMenuItemFunc := func() {
if autoRefreshMenuItem.Checked() {
trayLog.Info("Disable auto refresh")
autoRefresh = false
viper.Set("auto-refresh.enabled", false)
viper.WriteConfig()
autoRefreshMenuItem.Uncheck()
} else {
trayLog.Info("Enable auto refresh")
autoRefresh = true
viper.Set("auto-refresh.enabled", true)
viper.WriteConfig()
autoRefreshMenuItem.Check()
}
}
//////////////////////////////////
systray.AddSeparator()
//////////////////////////////////
rootElement = NewRoot()
go rootElement.UpdateData()
//////////////////////////////////
trayLog.Info("Ready")
//////////////////////////////////
for {
select {
case <-quitMenuItem.ClickedCh:
trayLog.Info("Quit")
systray.Quit()
case <-reloadMenuItem.ClickedCh:
reloadMenuItemFunc()
case <-autoRefreshTicker.C:
if autoRefresh {
go rootElement.UpdateData()
}
case <-autoRefreshMenuItem.ClickedCh:
autoRefreshMenuItemFunc()
}
}
}