Skip to content

Commit a4a05e8

Browse files
authored
Feature/allow to group projects dirs issue #89 (#127)
1 parent ba6afbd commit a4a05e8

File tree

2 files changed

+100
-21
lines changed

2 files changed

+100
-21
lines changed

config.go

+51-5
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func setTmuxOptions(tmuxOpts *TmuxOptions, c Config) {
7474
if c.ConfigFile != "" {
7575
usr, err := user.Current()
7676
if err != nil {
77-
log.Fatalf("cannot expand user home dir: %s", err)
77+
log.Fatalf("cannot expand user home dir: %s", err)
7878
}
7979
path := c.ConfigFile
80-
if strings.HasPrefix(path,"~") {
80+
if strings.HasPrefix(path, "~") {
8181
path = filepath.Join(usr.HomeDir, path[1:])
8282
}
8383

@@ -130,7 +130,7 @@ func ParseConfig(data string, settings map[string]string) (Config, error) {
130130
return c, nil
131131
}
132132

133-
func ListConfigs(dir string) ([]string, error) {
133+
func ListConfigs(dir string, includeDirs bool) ([]string, error) {
134134
var result []string
135135
files, err := os.ReadDir(dir)
136136

@@ -140,7 +140,12 @@ func ListConfigs(dir string) ([]string, error) {
140140

141141
for _, file := range files {
142142
fileExt := path.Ext(file.Name())
143-
if fileExt != ".yml" && fileExt != ".yaml" {
143+
dirCheck := true
144+
if includeDirs {
145+
dirCheck = !file.IsDir()
146+
}
147+
if fileExt != ".yml" && fileExt != ".yaml" && dirCheck {
148+
144149
continue
145150
}
146151
result = append(result, file.Name())
@@ -150,7 +155,7 @@ func ListConfigs(dir string) ([]string, error) {
150155
}
151156

152157
func FindConfig(dir, project string) (string, error) {
153-
configs, err := ListConfigs(dir)
158+
configs, err := ListConfigs(dir, false)
154159
if err != nil {
155160
return "", err
156161
}
@@ -164,3 +169,44 @@ func FindConfig(dir, project string) (string, error) {
164169

165170
return "", ConfigNotFoundError{Project: project}
166171
}
172+
func FindConfigs(dir, project string) ([]string, error) {
173+
isDir, _ := IsDirectory(dir + "/" + project)
174+
175+
if isDir {
176+
configs, err := ListConfigs(dir+"/"+project, false)
177+
if err != nil {
178+
return configs, err
179+
}
180+
for configIndex, configName := range configs {
181+
configs[configIndex] = dir + "/" + project + "/" + configName
182+
}
183+
return configs, err
184+
}
185+
186+
configs, err := ListConfigs(dir, false)
187+
if err != nil {
188+
return configs, err
189+
}
190+
191+
if len(configs) == 0 {
192+
return configs, ConfigNotFoundError{Project: project}
193+
}
194+
195+
for _, config := range configs {
196+
fileExt := path.Ext(config)
197+
if strings.TrimSuffix(config, fileExt) == project {
198+
return []string{dir + "/" + config}, nil
199+
}
200+
}
201+
202+
return configs, ConfigNotFoundError{Project: project}
203+
}
204+
205+
func IsDirectory(path string) (bool, error) {
206+
207+
fileInfo, err := os.Stat(path)
208+
if os.IsNotExist(err) {
209+
return false, err
210+
}
211+
return fileInfo.IsDir(), err
212+
}

main.go

+49-16
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ func main() {
9292
if options.Config != "" {
9393
configPath = options.Config
9494
} else if options.Project != "" {
95+
9596
config, err := FindConfig(userConfigDir, options.Project)
96-
if err != nil && options.Command != CommandNew {
97+
98+
if err != nil && options.Command != CommandNew && options.Command != CommandStart && options.Command != CommandStop {
9799
fmt.Fprintln(os.Stderr, err.Error())
98100
os.Exit(1)
99101
}
@@ -117,35 +119,48 @@ func main() {
117119
} else {
118120
fmt.Println("Starting new windows...")
119121
}
120-
121-
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
122+
configs, err := FindConfigs(userConfigDir, options.Project)
122123
if err != nil {
123124
fmt.Fprint(os.Stderr, err.Error())
124125
os.Exit(1)
125126
}
126-
127-
err = smug.Start(config, options, context)
128-
if err != nil {
129-
fmt.Println("Oops, an error occurred! Rolling back...")
130-
smug.Stop(config, options, context)
131-
os.Exit(1)
127+
for configIndex, configPath := range configs {
128+
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
129+
if err != nil {
130+
fmt.Fprint(os.Stderr, err.Error())
131+
os.Exit(1)
132+
}
133+
options.Detach = configIndex != len(configs)-1
134+
err = smug.Start(config, options, context)
135+
if err != nil {
136+
fmt.Println("Oops, an error occurred! Rolling back...")
137+
smug.Stop(config, options, context)
138+
os.Exit(1)
139+
}
132140
}
133141
case CommandStop:
134142
if len(options.Windows) == 0 {
135143
fmt.Println("Terminating session...")
136144
} else {
137145
fmt.Println("Killing windows...")
138146
}
139-
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
147+
configs, err := FindConfigs(userConfigDir, options.Project)
140148
if err != nil {
141149
fmt.Fprint(os.Stderr, err.Error())
142150
os.Exit(1)
143151
}
144-
145-
err = smug.Stop(config, options, context)
146-
if err != nil {
147-
fmt.Fprint(os.Stderr, err.Error())
148-
os.Exit(1)
152+
for _, configPath := range configs {
153+
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
154+
if err != nil {
155+
fmt.Fprint(os.Stderr, err.Error())
156+
os.Exit(1)
157+
}
158+
159+
err = smug.Stop(config, options, context)
160+
if err != nil {
161+
fmt.Fprint(os.Stderr, err.Error())
162+
os.Exit(1)
163+
}
149164
}
150165
case CommandNew, CommandEdit:
151166
err := EditConfig(configPath)
@@ -154,7 +169,7 @@ func main() {
154169
os.Exit(1)
155170
}
156171
case CommandList:
157-
configs, err := ListConfigs(userConfigDir)
172+
configs, err := ListConfigs(userConfigDir, true)
158173
if err != nil {
159174
fmt.Fprint(os.Stderr, err.Error())
160175
os.Exit(1)
@@ -163,6 +178,24 @@ func main() {
163178
for _, config := range configs {
164179
fileExt := path.Ext(config)
165180
fmt.Println(strings.TrimSuffix(config, fileExt))
181+
isDir, err := IsDirectory(userConfigDir+"/"+config)
182+
if err != nil {
183+
continue
184+
}
185+
if isDir {
186+
187+
subConfigs, err := ListConfigs(userConfigDir+"/"+config, false)
188+
if err != nil {
189+
fmt.Fprint(os.Stderr, err.Error())
190+
os.Exit(1)
191+
}
192+
for _, subConfig := range subConfigs {
193+
fileExt := path.Ext(subConfig)
194+
fmt.Println("|--"+strings.TrimSuffix(subConfig, fileExt))
195+
}
196+
197+
}
198+
166199
}
167200

168201
case CommandPrint:

0 commit comments

Comments
 (0)