Skip to content

Commit

Permalink
Feature/allow to group projects dirs issue #89 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
yousef-ysph authored Oct 11, 2024
1 parent ba6afbd commit a4a05e8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 21 deletions.
56 changes: 51 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func setTmuxOptions(tmuxOpts *TmuxOptions, c Config) {
if c.ConfigFile != "" {
usr, err := user.Current()
if err != nil {
log.Fatalf("cannot expand user home dir: %s", err)
log.Fatalf("cannot expand user home dir: %s", err)
}
path := c.ConfigFile
if strings.HasPrefix(path,"~") {
if strings.HasPrefix(path, "~") {
path = filepath.Join(usr.HomeDir, path[1:])
}

Expand Down Expand Up @@ -130,7 +130,7 @@ func ParseConfig(data string, settings map[string]string) (Config, error) {
return c, nil
}

func ListConfigs(dir string) ([]string, error) {
func ListConfigs(dir string, includeDirs bool) ([]string, error) {
var result []string
files, err := os.ReadDir(dir)

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

for _, file := range files {
fileExt := path.Ext(file.Name())
if fileExt != ".yml" && fileExt != ".yaml" {
dirCheck := true
if includeDirs {
dirCheck = !file.IsDir()
}
if fileExt != ".yml" && fileExt != ".yaml" && dirCheck {

continue
}
result = append(result, file.Name())
Expand All @@ -150,7 +155,7 @@ func ListConfigs(dir string) ([]string, error) {
}

func FindConfig(dir, project string) (string, error) {
configs, err := ListConfigs(dir)
configs, err := ListConfigs(dir, false)
if err != nil {
return "", err
}
Expand All @@ -164,3 +169,44 @@ func FindConfig(dir, project string) (string, error) {

return "", ConfigNotFoundError{Project: project}
}
func FindConfigs(dir, project string) ([]string, error) {
isDir, _ := IsDirectory(dir + "/" + project)

if isDir {
configs, err := ListConfigs(dir+"/"+project, false)
if err != nil {
return configs, err
}
for configIndex, configName := range configs {
configs[configIndex] = dir + "/" + project + "/" + configName
}
return configs, err
}

configs, err := ListConfigs(dir, false)
if err != nil {
return configs, err
}

if len(configs) == 0 {
return configs, ConfigNotFoundError{Project: project}
}

for _, config := range configs {
fileExt := path.Ext(config)
if strings.TrimSuffix(config, fileExt) == project {
return []string{dir + "/" + config}, nil
}
}

return configs, ConfigNotFoundError{Project: project}
}

func IsDirectory(path string) (bool, error) {

fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
return false, err
}
return fileInfo.IsDir(), err
}
65 changes: 49 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ func main() {
if options.Config != "" {
configPath = options.Config
} else if options.Project != "" {

config, err := FindConfig(userConfigDir, options.Project)
if err != nil && options.Command != CommandNew {

if err != nil && options.Command != CommandNew && options.Command != CommandStart && options.Command != CommandStop {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
Expand All @@ -117,35 +119,48 @@ func main() {
} else {
fmt.Println("Starting new windows...")
}

config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
configs, err := FindConfigs(userConfigDir, options.Project)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(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)
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...")
}
config, err := GetConfig(configPath, options.Settings, smug.tmux.TmuxOptions)
configs, err := FindConfigs(userConfigDir, options.Project)
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)
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)
Expand All @@ -154,7 +169,7 @@ func main() {
os.Exit(1)
}
case CommandList:
configs, err := ListConfigs(userConfigDir)
configs, err := ListConfigs(userConfigDir, true)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
Expand All @@ -163,6 +178,24 @@ func main() {
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:
Expand Down

0 comments on commit a4a05e8

Please sign in to comment.