Skip to content

Commit

Permalink
fix default config resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan committed Dec 3, 2024
1 parent e6944c7 commit bf3f245
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
15 changes: 7 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func GetConfig(path string, settings map[string]string, tmuxOpts *TmuxOptions) (
setTmuxOptions(tmuxOpts, c)

return &c, err

}

func ParseConfig(data string, settings map[string]string) (Config, error) {
Expand Down Expand Up @@ -133,7 +132,6 @@ func ParseConfig(data string, settings map[string]string) (Config, error) {
func ListConfigs(dir string, includeDirs bool) ([]string, error) {
var result []string
files, err := os.ReadDir(dir)

if err != nil {
return result, err
}
Expand All @@ -145,7 +143,6 @@ func ListConfigs(dir string, includeDirs bool) ([]string, error) {
dirCheck = !file.IsDir()
}
if fileExt != ".yml" && fileExt != ".yaml" && dirCheck {

continue
}
result = append(result, file.Name())
Expand All @@ -169,6 +166,7 @@ func FindConfig(dir, project string) (string, error) {

return "", ConfigNotFoundError{Project: project}
}

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

Expand All @@ -177,19 +175,21 @@ func FindConfigs(dir, project string) ([]string, error) {
if err != nil {
return configs, err
}

for configIndex, configName := range configs {
configs[configIndex] = dir + "/" + project + "/" + configName
}
return configs, err

return nil, err
}

configs, err := ListConfigs(dir, false)
if err != nil {
return configs, err
return nil, fmt.Errorf("list configs %w", err)
}

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

for _, config := range configs {
Expand All @@ -199,11 +199,10 @@ func FindConfigs(dir, project string) ([]string, error) {
}
}

return configs, ConfigNotFoundError{Project: project}
return nil, ConfigNotFoundError{Project: project}
}

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

fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
return false, err
Expand Down
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func main() {
tmux := Tmux{commander, &TmuxOptions{}}
smug := Smug{tmux, commander}
context := CreateContext()
configs := []string{}

var configPath string
if options.Config != "" {
Expand All @@ -102,14 +103,18 @@ func main() {
if options.Command == CommandNew {
config = fmt.Sprintf("%s.yml", options.Project)
}

configPath = filepath.Join(userConfigDir, config)
configs = append(configs, configPath)
} else {
path, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

configPath = filepath.Join(path, defaultConfigFile)
configs = append(configs, configPath)
}

switch options.Command {
Expand All @@ -119,18 +124,26 @@ func main() {
} else {
fmt.Println("Starting new windows...")
}
configs, err := FindConfigs(userConfigDir, options.Project)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)

if options.Project != "" {
projectConfigs, err := FindConfigs(userConfigDir, options.Project)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}

configs = append(configs, projectConfigs...)
}

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...")
Expand Down Expand Up @@ -178,7 +191,7 @@ func main() {
for _, config := range configs {
fileExt := path.Ext(config)
fmt.Println(strings.TrimSuffix(config, fileExt))
isDir, err := IsDirectory(userConfigDir+"/"+config)
isDir, err := IsDirectory(userConfigDir + "/" + config)
if err != nil {
continue
}
Expand All @@ -191,7 +204,7 @@ func main() {
}
for _, subConfig := range subConfigs {
fileExt := path.Ext(subConfig)
fmt.Println("|--"+strings.TrimSuffix(subConfig, fileExt))
fmt.Println("|--" + strings.TrimSuffix(subConfig, fileExt))
}

}
Expand Down
6 changes: 4 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ type Options struct {
InsideCurrentSession bool
}

var ErrHelp = errors.New("help requested")
var ErrCommandNotFound = errors.New("command not found")
var (
ErrHelp = errors.New("help requested")
ErrCommandNotFound = errors.New("command not found")
)

const (
WindowsUsage = "List of windows to start. If session exists, those windows will be attached to current session"
Expand Down
1 change: 0 additions & 1 deletion smug.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error
}

newPane, err := smug.tmux.SplitWindow(window, p.Type, paneRoot)

if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
)

type TmuxOptions struct {

// Default socket name
SocketName string `yaml:"socket_name"`

Expand Down Expand Up @@ -50,7 +49,7 @@ func (tmux Tmux) cmd(args ...string) *exec.Cmd {
tmuxCmd = append(tmuxCmd, "-S", tmux.SocketPath)
} else if tmux.SocketName != "" {
tmuxCmd = append(tmuxCmd, "-L", tmux.SocketName)
}
}

if tmux.ConfigFile != "" {
tmuxCmd = append(tmuxCmd, "-f", tmux.ConfigFile)
Expand Down Expand Up @@ -148,10 +147,8 @@ func (tmux Tmux) SwitchClient(target string) error {
}

func (tmux Tmux) SessionName() (string, error) {

cmd := tmux.cmd("display-message", "-p", "#S")
sessionName, err := tmux.commander.Exec(cmd)

if err != nil {
return sessionName, err
}
Expand Down Expand Up @@ -182,7 +179,6 @@ func (tmux Tmux) ListWindows(target string) ([]TmuxWindow, error) {
}

return windows, nil

}

func (tmux Tmux) ListPanes(target string) ([]TmuxPane, error) {
Expand Down

0 comments on commit bf3f245

Please sign in to comment.