-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
smug.go
267 lines (219 loc) · 5.45 KB
/
smug.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
const defaultWindowName = "smug_def"
func ExpandPath(path string) string {
if strings.HasPrefix(path, "~/") {
userHome, err := os.UserHomeDir()
if err != nil {
return path
}
return strings.Replace(path, "~", userHome, 1)
}
return path
}
func Contains(slice []string, s string) bool {
for _, e := range slice {
if e == s {
return true
}
}
return false
}
type Smug struct {
tmux Tmux
commander Commander
}
func (smug Smug) execShellCommands(commands []string, path string) error {
for _, c := range commands {
cmd := exec.Command("/bin/sh", "-c", c)
cmd.Dir = path
_, err := smug.commander.Exec(cmd)
if err != nil {
return err
}
}
return nil
}
func (smug Smug) setEnvVariables(target string, env map[string]string) error {
for key, value := range env {
_, err := smug.tmux.SetEnv(target, key, value)
if err != nil {
return err
}
}
return nil
}
func (smug Smug) switchOrAttach(target string, attach bool, insideTmuxSession bool) error {
if insideTmuxSession && attach {
return smug.tmux.SwitchClient(target)
} else if !insideTmuxSession {
return smug.tmux.Attach(target, os.Stdin, os.Stdout, os.Stderr)
}
return nil
}
func (smug Smug) Stop(config *Config, options *Options, context Context) error {
windows := options.Windows
if len(windows) == 0 {
sessionRoot := ExpandPath(config.Root)
err := smug.execShellCommands(config.Stop, sessionRoot)
if err != nil {
return err
}
_, err = smug.tmux.StopSession(config.Session)
return err
}
for _, w := range windows {
err := smug.tmux.KillWindow(config.Session + ":" + w)
if err != nil {
return err
}
}
return nil
}
func (smug Smug) Start(config *Config, options *Options, context Context) error {
var sessionName string
var err error
createWindowsInsideCurrSession := options.InsideCurrentSession
if createWindowsInsideCurrSession && !context.InsideTmuxSession {
return errors.New("cannot use -i flag outside of a tmux session")
}
sessionName = config.Session
if createWindowsInsideCurrSession {
sessionName, err = smug.tmux.SessionName()
if err != nil {
return err
}
}
sessionName = sessionName + ":"
sessionExists := smug.tmux.SessionExists(sessionName)
sessionRoot := ExpandPath(config.Root)
windows := options.Windows
attach := options.Attach
if !sessionExists && !createWindowsInsideCurrSession {
err := smug.execShellCommands(config.BeforeStart, sessionRoot)
if err != nil {
return err
}
_, err = smug.tmux.NewSession(config.Session, sessionRoot, defaultWindowName)
if err != nil {
return err
}
err = smug.setEnvVariables(config.Session, config.Env)
if err != nil {
return err
}
} else if len(windows) == 0 && !createWindowsInsideCurrSession {
return smug.switchOrAttach(sessionName, attach, context.InsideTmuxSession)
}
for _, w := range config.Windows {
if (len(windows) == 0 && w.Manual) || (len(windows) > 0 && !Contains(windows, w.Name)) {
continue
}
windowRoot := ExpandPath(w.Root)
if windowRoot == "" || !filepath.IsAbs(windowRoot) {
windowRoot = filepath.Join(sessionRoot, w.Root)
}
window, err := smug.tmux.NewWindow(sessionName, w.Name, windowRoot)
if err != nil {
return err
}
for _, c := range w.Commands {
time.Sleep(time.Millisecond * time.Duration(config.SendKeysTimeout))
err := smug.tmux.SendKeys(window, c)
if err != nil {
return err
}
}
for i, p := range w.Panes {
paneRoot := ExpandPath(p.Root)
if paneRoot == "" || !filepath.IsAbs(p.Root) {
paneRoot = filepath.Join(windowRoot, p.Root)
}
newPane, err := smug.tmux.SplitWindow(window, p.Type, paneRoot)
if err != nil {
return err
}
if i%2 == 0 {
_, err = smug.tmux.SelectLayout(window, Tiled)
if err != nil {
return err
}
}
for _, c := range p.Commands {
time.Sleep(time.Millisecond * time.Duration(config.SendKeysTimeout))
err = smug.tmux.SendKeys(window+"."+newPane, c)
if err != nil {
return err
}
}
}
layout := w.Layout
if layout == "" {
layout = EvenHorizontal
}
_, err = smug.tmux.SelectLayout(window, layout)
if err != nil {
return err
}
}
if !options.InsideCurrentSession && !sessionExists {
err := smug.tmux.KillWindow(sessionName + defaultWindowName)
if err != nil {
return err
}
err = smug.tmux.RenumberWindows(sessionName)
if err != nil {
return err
}
}
if len(config.Windows) > 0 && !options.Detach {
w := config.Windows[0].Name
if len(options.Windows) > 0 {
w = options.Windows[0]
}
return smug.switchOrAttach(sessionName+w, attach, context.InsideTmuxSession)
}
return nil
}
func (smug Smug) GetConfigFromSession(options *Options, context Context) (Config, error) {
config := Config{}
tmuxSession, err := smug.tmux.SessionName()
if err != nil {
return Config{}, err
}
config.Session = tmuxSession
tmuxWindows, err := smug.tmux.ListWindows(options.Project)
if err != nil {
return Config{}, err
}
for _, w := range tmuxWindows {
tmuxPanes, err := smug.tmux.ListPanes(options.Project + ":" + w.ID)
if err != nil {
return Config{}, err
}
panes := []Pane{}
for _, p := range tmuxPanes {
root := p.Root
if root == w.Root {
root = ""
}
panes = append(panes, Pane{
Root: root,
})
}
config.Windows = append(config.Windows, Window{
Name: w.Name,
Layout: w.Layout,
Root: w.Root,
Panes: panes,
})
}
return config, nil
}