-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
99 lines (90 loc) · 2.45 KB
/
config.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
package chaakoo
import (
"fmt"
"strings"
)
import (
"errors"
)
// Config holds the entire config
type Config struct {
SessionName string `mapstructure:"name"`
Windows []*Window `mapstructure:"windows"`
DryRun bool
ExitOnError bool
}
// Validate validates the config
// The error messages contain contextual information related to the validation issues
func (c *Config) Validate() error {
if c == nil {
return errors.New("config is nil")
}
if len(c.SessionName) == 0 {
return errors.New("session name is required")
}
if len(c.Windows) == 0 {
return fmt.Errorf("atleast 1 window is required for session - %s", c.SessionName)
}
for _, window := range c.Windows {
if err := window.Validate(); err != nil {
return err
}
}
return nil
}
// Parse delegates to Window.Parse
func (c *Config) Parse() error {
for _, window := range c.Windows {
if err := window.Parse(); err != nil {
return fmt.Errorf("unable to parse grid for window - %s: %w", window.Name, err)
}
}
return nil
}
// Window represents one TMUX window from the config
type Window struct {
Name string `mapstructure:"name"`
Grid string `mapstructure:"grid"`
FirstPane *Pane
Commands []*Command `mapstructure:"commands"`
}
// Validate validates a Window related config
func (w *Window) Validate() error {
if w == nil {
return errors.New("window is nil")
}
if len(w.Name) == 0 {
return errors.New("window name is required")
}
if len(strings.TrimSpace(w.Grid)) == 0 {
return fmt.Errorf("grid for window, %s, is empty", w.Name)
}
return nil
}
// Parse - parses the config
func (w *Window) Parse() error {
if w == nil {
return errors.New("window is nil")
}
grid, err := PrepareGrid(w.Grid)
if err != nil {
return err
}
pane, err := PrepareGraph(grid)
if err != nil {
return err
}
w.FirstPane = pane
return nil
}
// Command represents a command fragment that will be executed in the pane whose name will be same as name in this
// struct.
// WorkingDirectory is the location in which all the commands will be executed.
// The working directory can be passed to tmux split-window command with -c flag but doing that will not create the
// pane if the working directory is wrong. So, in this implementation, passing the working directory is deferred until
// the pane has been created.
type Command struct {
Name string `mapstructure:"pane"`
CommandText string `mapstructure:"command"`
WorkingDirectory string `mapstructure:"workdir"`
}