From a4a05e8a62410c3cdee726f2f8d4bb894dd6ff26 Mon Sep 17 00:00:00 2001 From: Yousef <43806260+yousef-ysph@users.noreply.github.com> Date: Fri, 11 Oct 2024 23:18:01 +1300 Subject: [PATCH] Feature/allow to group projects dirs issue #89 (#127) --- config.go | 56 ++++++++++++++++++++++++++++++++++++++++++----- main.go | 65 +++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 100 insertions(+), 21 deletions(-) diff --git a/config.go b/config.go index cee1a10..1a99046 100644 --- a/config.go +++ b/config.go @@ -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:]) } @@ -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) @@ -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()) @@ -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 } @@ -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 +} diff --git a/main.go b/main.go index 3944e7e..5e07953 100644 --- a/main.go +++ b/main.go @@ -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) } @@ -117,18 +119,24 @@ 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 { @@ -136,16 +144,23 @@ func main() { } 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) @@ -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) @@ -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: