-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
232 lines (200 loc) · 8.38 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"fmt"
"os"
"path"
"strings"
"github.com/DarthSim/overmind/v2/start"
"github.com/Envek/godotenv"
"github.com/urfave/cli"
)
const version = "2.5.1"
func socketFlags(s, n *string) []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "socket, s",
EnvVar: "OVERMIND_SOCKET",
Usage: "Path to overmind socket (in case of using unix socket) or address to bind (in other cases)",
Value: "./.overmind.sock",
Destination: s,
},
cli.StringFlag{
Name: "network, S",
EnvVar: "OVERMIND_NETWORK",
Usage: "Network to use for commands. Can be 'tcp', 'tcp4', 'tcp6' or 'unix'",
Value: "unix",
Destination: n,
},
}
}
func setupStartCmd() cli.Command {
c := start.Handler{}
return cli.Command{
Name: "start",
Aliases: []string{"s"},
Usage: "Run procfile",
Action: c.Run,
Flags: append(
[]cli.Flag{
cli.StringFlag{Name: "title, w", EnvVar: "OVERMIND_TITLE", Usage: "Specify a title of the application", Destination: &c.Title},
cli.StringFlag{Name: "procfile, f", EnvVar: "OVERMIND_PROCFILE", Usage: "Specify a Procfile to load", Value: "./Procfile", Destination: &c.Procfile},
cli.StringFlag{Name: "processes, l", EnvVar: "OVERMIND_PROCESSES", Usage: "Specify process names to launch. Divide names with comma", Destination: &c.ProcNames},
cli.StringFlag{Name: "root, d", Usage: "Specify a working directory of application. Default: directory containing the Procfile", Destination: &c.Root},
cli.IntFlag{Name: "timeout, t", EnvVar: "OVERMIND_TIMEOUT", Usage: "Specify the amount of time (in seconds) processes have to shut down gracefully before being brutally killed", Value: 5, Destination: &c.Timeout},
cli.IntFlag{Name: "port, p", EnvVar: "OVERMIND_PORT,PORT", Usage: "Specify a port to use as the base", Value: 5000, Destination: &c.PortBase},
cli.IntFlag{Name: "port-step, P", EnvVar: "OVERMIND_PORT_STEP", Usage: "Specify a step to increase port number", Value: 100, Destination: &c.PortStep},
cli.BoolFlag{Name: "no-port, N", EnvVar: "OVERMIND_NO_PORT", Usage: "Don't set $PORT variable for processes", Destination: &c.NoPort},
cli.StringFlag{Name: "can-die, c", EnvVar: "OVERMIND_CAN_DIE", Usage: "Specify names of process which can die without interrupting the other processes. Divide names with comma", Destination: &c.CanDie},
cli.BoolFlag{Name: "any-can-die", EnvVar: "OVERMIND_ANY_CAN_DIE", Usage: "No dead processes should stop Overmind. Overrides can-die", Destination: &c.AnyCanDie},
cli.StringFlag{Name: "auto-restart, r", EnvVar: "OVERMIND_AUTO_RESTART", Usage: "Specify names of process which will be auto restarted on death. Divide names with comma. Use 'all' as a process name to auto restart all processes on death.", Destination: &c.AutoRestart},
cli.StringFlag{Name: "colors, b", EnvVar: "OVERMIND_COLORS", Usage: "Specify the xterm color codes that will be used to colorize process names. Divide codes with comma"},
cli.BoolFlag{Name: "show-timestamps, T", EnvVar: "OVERMIND_SHOW_TIMESTAMPS", Usage: "Add timestamps to the output", Destination: &c.ShowTimestamps},
cli.StringFlag{Name: "formation, m", EnvVar: "OVERMIND_FORMATION", Usage: "Specify the number of each process type to run. The value passed in should be in the format process=num,process=num. Use 'all' as a process name to set value for all processes"},
cli.IntFlag{Name: "formation-port-step", EnvVar: "OVERMIND_FORMATION_PORT_STEP", Usage: "Specify a step to increase port number for the next instance of a process", Value: 10, Destination: &c.FormationPortStep},
cli.StringFlag{Name: "stop-signals, i", EnvVar: "OVERMIND_STOP_SIGNALS", Usage: "Specify a signal that will be sent to each process when Overmind will try to stop them. The value passed in should be in the format process=signal,process=signal. Supported signals are: ABRT, INT, KILL, QUIT, STOP, TERM, USR1, USR2"},
cli.BoolFlag{Name: "daemonize, D", EnvVar: "OVERMIND_DAEMONIZE", Usage: "Launch Overmind as a daemon. Use 'overmind echo' to view logs and 'overmind quit' to gracefully quit daemonized instance", Destination: &c.Daemonize},
cli.StringFlag{Name: "tmux-config, F", EnvVar: "OVERMIND_TMUX_CONFIG", Usage: "Specify an alternative tmux config path to be used by Overmind", Destination: &c.TmuxConfigPath},
cli.StringFlag{Name: "ignored-processes, x", EnvVar: "OVERMIND_IGNORED_PROCESSES", Usage: "Specify process names to prevent from launching. Useful if you want to run all but one or two processes. Divide names with comma. Takes precedence over the 'processes' flag.", Destination: &c.IgnoredProcNames},
cli.StringFlag{Name: "shell, H", EnvVar: "OVERMIND_SHELL", Usage: "Specify shell to run processes with.", Value: "sh", Destination: &c.Shell},
},
socketFlags(&c.SocketPath, &c.Network)...,
),
}
}
func setupRestartCmd() cli.Command {
c := cmdRestartHandler{}
return cli.Command{
Name: "restart",
Aliases: []string{"r"},
Usage: "Restart specified processes",
Action: c.Run,
ArgsUsage: "[process name...]",
Flags: socketFlags(&c.SocketPath, &c.Network),
}
}
func setupStopCmd() cli.Command {
c := cmdStopHandler{}
return cli.Command{
Name: "stop",
Aliases: []string{"interrupt", "i"},
Usage: "Stop specified processes without quitting Overmind itself",
Action: c.Run,
ArgsUsage: "[process name...]",
Flags: socketFlags(&c.SocketPath, &c.Network),
}
}
func setupConnectCmd() cli.Command {
c := cmdConnectHandler{}
return cli.Command{
Name: "connect",
Aliases: []string{"c"},
Usage: "Connect to the tmux session of the specified process",
Action: c.Run,
ArgsUsage: "[process name]",
Flags: append(
[]cli.Flag{
cli.BoolFlag{Name: "control-mode, c", EnvVar: "OVERMIND_CONTROL_MODE", Usage: "Connect to the tmux session in control mode", Destination: &c.ControlMode},
},
socketFlags(&c.SocketPath, &c.Network)...,
),
}
}
func setupQuitCmd() cli.Command {
c := cmdQuitHandler{}
return cli.Command{
Name: "quit",
Aliases: []string{"q"},
Usage: "Gracefully quits Overmind. Same as sending SIGINT",
Action: c.Run,
Flags: socketFlags(&c.SocketPath, &c.Network),
}
}
func setupKillCmd() cli.Command {
c := cmdKillHandler{}
return cli.Command{
Name: "kill",
Aliases: []string{"k"},
Usage: "Kills all processes",
Action: c.Run,
Flags: socketFlags(&c.SocketPath, &c.Network),
}
}
func setupRunCmd() cli.Command {
c := cmdRunHandler{}
return cli.Command{
Name: "run",
Aliases: []string{"exec", "e"},
Usage: "Runs provided command within the Overmind environment",
Action: c.Run,
SkipFlagParsing: true,
}
}
func setupEchoCmd() cli.Command {
c := cmdEchoHandler{}
return cli.Command{
Name: "echo",
Usage: "Echoes output from master Overmind instance",
Action: c.Run,
Flags: socketFlags(&c.SocketPath, &c.Network),
}
}
func setupStatusCmd() cli.Command {
c := cmdStatusHandler{}
return cli.Command{
Name: "status",
Aliases: []string{"ps"},
Usage: "Prints process statuses",
Action: c.Run,
Flags: socketFlags(&c.SocketPath, &c.Network),
}
}
func main() {
loadEnvFiles()
app := cli.NewApp()
app.Name = "Overmind"
app.HelpName = "overmind"
app.Usage = "The mind to rule processes of your development environment"
app.Description = strings.Join([]string{
"Overmind runs commands specified in procfile in a tmux session.",
"This allows to connect to each process and manage processes on fly.",
}, " ")
app.Author = "Sergey \"DarthSim\" Alexandrovich"
app.Email = "darthsim@gmail.com"
app.Version = version
app.Commands = []cli.Command{
setupStartCmd(),
setupRestartCmd(),
setupStopCmd(),
setupConnectCmd(),
setupQuitCmd(),
setupKillCmd(),
setupRunCmd(),
setupEchoCmd(),
setupStatusCmd(),
}
app.Run(os.Args)
}
func loadEnvFiles() {
// First load the specifically named overmind env files
userHomeDir, _ := os.UserHomeDir()
loadEnvFile(path.Join(userHomeDir, ".overmind.env"))
loadEnvFile("./.overmind.env")
_, skipEnv := os.LookupEnv("OVERMIND_SKIP_ENV")
if !skipEnv {
loadEnvFile("./.env")
}
envs := strings.Split(os.Getenv("OVERMIND_ENV"), ",")
for _, e := range envs {
if len(e) > 0 {
loadEnvFile(e)
}
}
}
func loadEnvFile(file string) {
err := godotenv.Overload(file)
if err != nil {
if !os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "overmind: skipping", file, "due to error:", err)
}
}
}