This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
/
main.go
130 lines (107 loc) · 3.01 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/hanjunlee/argocui/internal/config"
svc "github.com/hanjunlee/argocui/internal/runtime"
"github.com/hanjunlee/argocui/internal/runtime/namespace"
"github.com/hanjunlee/argocui/internal/runtime/workflow"
"github.com/hanjunlee/argocui/internal/ui"
argoutil "github.com/hanjunlee/argocui/pkg/util/argo"
colorutil "github.com/hanjunlee/argocui/pkg/util/color"
"github.com/argoproj/argo/pkg/client/clientset/versioned"
ai "github.com/argoproj/argo/pkg/client/informers/externalversions"
"github.com/jroimartin/gocui"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
ki "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
const (
noResyncPeriod = 0
)
var (
version = flag.Bool("version", false, "Check the version.")
debug = flag.Bool("debug", false, "Debug mode.")
trace = flag.Bool("trace", false, "Debug as trace level.")
)
func main() {
// flag command
flag.Parse()
if *version {
currentVersion()
return
}
setLog()
g := newGui()
defer g.Close()
kc := argoutil.GetKubeClientset()
kfactory := ki.NewSharedInformerFactory(kc, 1*time.Second)
ac := argoutil.GetClientset()
afactory := ai.NewSharedInformerFactory(ac, 1*time.Second)
svcs := getRuntimeServices(kc, kfactory, ac, afactory)
neverStop := make(<-chan struct{})
kfactory.WaitForCacheSync(neverStop)
afactory.WaitForCacheSync(neverStop)
m := ui.NewManager(svcs["mock"], svcs["ns"], svcs["wf"])
g.SetManager(m)
if err := m.Keybinding(g); err != nil {
log.Panic(err)
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panic(err)
}
}
func currentVersion() {
fmt.Println(colorutil.ChangeColor(config.Logo, gocui.ColorYellow))
fmt.Printf("Version: %s\n", config.Version)
fmt.Printf("Argo Version: %s\n", config.ArgoVersion)
}
func setLog() {
log.SetLevel(log.InfoLevel)
if *debug {
log.SetLevel(log.DebugLevel)
}
if *trace {
log.SetLevel(log.TraceLevel)
}
path := filepath.Join(os.Getenv("HOME"), "/.argocui/log")
log.SetOutput(&lumberjack.Logger{
Filename: path,
MaxSize: 500,
MaxBackups: 1,
MaxAge: 7,
Compress: true,
})
}
func newGui() *gocui.Gui {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panic(err)
}
g.Highlight = false
g.InputEsc = true
return g
}
func getRuntimeServices(kc kubernetes.Interface, kfactory ki.SharedInformerFactory, ac versioned.Interface, afactory ai.SharedInformerFactory) map[string]svc.UseCase {
neverStop := make(<-chan struct{})
// namespace service
ni := kfactory.Core().V1().Namespaces()
go ni.Informer().Run(neverStop)
nr := namespace.NewRepo(ni.Lister())
ns := svc.NewService(nr)
// workflow service
wi := afactory.Argoproj().V1alpha1().Workflows()
go wi.Informer().Run(neverStop)
wr := workflow.NewRepo(ac, wi.Lister(), kc)
ws := svc.NewService(wr)
cache.WaitForCacheSync(neverStop, ni.Informer().HasSynced, wi.Informer().HasSynced)
return map[string]svc.UseCase{
"ns": ns,
"wf": ws,
}
}