-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
216 lines (188 loc) · 4.92 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
package main
import (
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
var version = "[dev build]"
var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s
Usage:
smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug] [--detach] [-i, --inside-current-session] [<key>=<value>]...
Options:
-f, --file %s
-w, --windows %s
-a, --attach %s
-i, --inside-current-session %s
-d, --debug %s
--detach %s
Commands:
list list available project configurations
edit edit project configuration
new new project configuration
start start project session
stop stop project session
print session configuration to stdout
Examples:
$ smug list
$ smug edit blog
$ smug new blog
$ smug start blog
$ smug start blog:win1
$ smug start blog -w win1
$ smug start blog:win1,win2
$ smug stop blog
$ smug start blog --attach
$ smug print > ~/.config/smug/blog.yml
`, version, FileUsage, WindowsUsage, AttachUsage, InsideCurrentSessionUsage, DebugUsage, DetachUsage)
const defaultConfigFile = ".smug.yml"
func newLogger(path string) *log.Logger {
logFile, err := os.Create(filepath.Join(path, "smug.log"))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
return log.New(logFile, "", 0)
}
func main() {
options, err := ParseOptions(os.Args[1:])
if errors.Is(err, ErrHelp) {
fmt.Fprint(os.Stdout, usage)
os.Exit(0)
}
if err != nil {
fmt.Fprintf(
os.Stderr,
"Cannot parse command line options: %q",
err.Error(),
)
os.Exit(1)
}
userConfigDir := filepath.Join(ExpandPath("~/"), ".config/smug")
var logger *log.Logger
if options.Debug {
logger = newLogger(userConfigDir)
}
commander := DefaultCommander{logger}
tmux := Tmux{commander, &TmuxOptions{}}
smug := Smug{tmux, commander}
context := CreateContext()
var configPath string
if options.Config != "" {
configPath = options.Config
} else if options.Project != "" {
config, err := FindConfig(userConfigDir, options.Project)
if err != nil && options.Command != CommandNew && options.Command != CommandStart && options.Command != CommandStop {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if options.Command == CommandNew {
config = fmt.Sprintf("%s.yml", options.Project)
}
configPath = filepath.Join(userConfigDir, config)
} else {
path, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
configPath = filepath.Join(path, defaultConfigFile)
}
switch options.Command {
case CommandStart:
if len(options.Windows) == 0 {
fmt.Println("Starting a new session...")
} else {
fmt.Println("Starting new windows...")
}
configs, err := FindConfigs(userConfigDir, options.Project)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
for configIndex, configPath := range configs {
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
options.Detach = configIndex != len(configs)-1
err = smug.Start(config, options, context)
if err != nil {
fmt.Println("Oops, an error occurred! Rolling back...")
smug.Stop(config, options, context)
os.Exit(1)
}
}
case CommandStop:
if len(options.Windows) == 0 {
fmt.Println("Terminating session...")
} else {
fmt.Println("Killing windows...")
}
configs, err := FindConfigs(userConfigDir, options.Project)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
for _, configPath := range configs {
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
err = smug.Stop(config, options, context)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
}
case CommandNew, CommandEdit:
err := EditConfig(configPath)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
case CommandList:
configs, err := ListConfigs(userConfigDir, true)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
for _, config := range configs {
fileExt := path.Ext(config)
fmt.Println(strings.TrimSuffix(config, fileExt))
isDir, err := IsDirectory(userConfigDir+"/"+config)
if err != nil {
continue
}
if isDir {
subConfigs, err := ListConfigs(userConfigDir+"/"+config, false)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
for _, subConfig := range subConfigs {
fileExt := path.Ext(subConfig)
fmt.Println("|--"+strings.TrimSuffix(subConfig, fileExt))
}
}
}
case CommandPrint:
config, err := smug.GetConfigFromSession(options, context)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
d, err := yaml.Marshal(&config)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Println(string(d))
}
}