/
main.go
158 lines (123 loc) · 3.55 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"github.com/eko/monday/internal/runtime"
"github.com/eko/monday/pkg/config"
"github.com/eko/monday/pkg/forwarder"
"github.com/eko/monday/pkg/hostfile"
"github.com/eko/monday/pkg/proxy"
"github.com/eko/monday/pkg/runner"
"github.com/eko/monday/pkg/ui"
"github.com/eko/monday/pkg/watcher"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/jroimartin/gocui"
)
const (
name = "Monday"
)
var (
Version string
proxyComponent *proxy.Proxy
forwarderComponent *forwarder.Forwarder
runnerComponent *runner.Runner
watcherComponent *watcher.Watcher
uiEnabled = len(os.Getenv("MONDAY_ENABLE_UI")) > 0
)
func main() {
runtime.InitRuntimeEnvironment()
rootCmd := &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
if !uiEnabled {
uiEnabled, _ = strconv.ParseBool(cmd.Flag("ui").Value.String())
}
conf, err := config.Load()
if err != nil {
fmt.Printf("❌ %v", err)
return
}
choice := selectProject(conf)
run(conf, choice)
handleExitSignal()
},
}
// UI-enable flag (for both root and run commands)
runCmd.Flags().Bool("ui", false, "Enable the terminal UI")
rootCmd.Flags().Bool("ui", false, "Enable the terminal UI")
rootCmd.AddCommand(completionCmd)
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(upgradeCmd)
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Printf("❌ An error has occured during 'edit' command: %v\n", err)
os.Exit(1)
}
}
func selectProject(conf *config.Config) string {
prompt := promptui.Select{
Label: "Which project do you want to work on?",
Items: conf.GetProjectNames(),
Size: 20,
}
_, choice, err := prompt.Run()
if err != nil {
panic(fmt.Sprintf("Prompt failed:\n%v", err))
}
fmt.Print("\n")
return choice
}
func run(conf *config.Config, choice string) {
layout := ui.NewLayout(uiEnabled)
layout.Init()
// Retrieve selected project configuration by its name
project, err := conf.GetProjectByName(choice)
if err != nil {
panic(err)
}
// Initializes hosts file manager
hostfile, err := hostfile.NewClient()
if err != nil {
panic(err)
}
proxyComponent = proxy.NewProxy(layout.GetProxyView(), hostfile)
runnerComponent = runner.NewRunner(layout.GetLogsView(), proxyComponent, project)
forwarderComponent = forwarder.NewForwarder(layout.GetForwardsView(), proxyComponent, project)
watcherComponent = watcher.NewWatcher(runnerComponent, forwarderComponent, conf.Watcher, project)
go watcherComponent.Watch()
if uiEnabled {
defer layout.GetGui().Close()
if err := layout.GetGui().SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
panic(err)
}
layout.GetStatusView().Writef(" ⇢ %s | Commands: ←/→: select view | ↑/↓: scroll up/down | a: toggle autoscroll | f: toggle fullscreen", choice)
if err := layout.GetGui().MainLoop(); err != nil && err != gocui.ErrQuit {
fmt.Println(err)
stopAll()
}
}
}
// Handle for an exit signal in order to quit application on a proper way (shutting down connections and servers).
func handleExitSignal() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, os.Kill)
<-stop
stopAll()
}
func stopAll() {
fmt.Println("\n👋 Bye, closing your local applications and remote connections now")
watcherComponent.Stop()
forwarderComponent.Stop()
proxyComponent.Stop()
runnerComponent.Stop()
os.Exit(0)
}
func quit(g *gocui.Gui, v *gocui.View) error {
g.Close()
stopAll()
return gocui.ErrQuit
}