Skip to content

Commit

Permalink
fix: re-add support for config imports
Browse files Browse the repository at this point in the history
closes #175
  • Loading branch information
acburdine authored and joshmedeski committed Oct 7, 2024
1 parent 4a7352e commit f9a5318
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions configurator/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configurator

import (
"fmt"
"path"
"strings"

"github.com/joshmedeski/sesh/model"
"github.com/joshmedeski/sesh/oswrap"
Expand All @@ -29,13 +29,21 @@ func (c *RealConfigurator) configFilePath(rootDir string) string {
return c.path.Join(rootDir, "sesh", "sesh.toml")
}

func (c *RealConfigurator) fullImportPath(homeDir, importPath string) (string, error) {
if !strings.HasPrefix(importPath, "~") {
return c.path.Abs(importPath)
}

return c.path.Join(homeDir, importPath[1:]), nil
}

func (c *RealConfigurator) getConfigFileFromUserConfigDir() (model.Config, error) {
config := model.Config{}
userHomeDir, err := c.os.UserHomeDir()
if err != nil {
return config, fmt.Errorf("couldn't get user config dir: %q", err)
}
userConfigDir := path.Join(userHomeDir, ".config")
userConfigDir := c.path.Join(userHomeDir, ".config")
configFilePath := c.configFilePath(userConfigDir)
file, _ := c.os.ReadFile(configFilePath)
// TODO: add to debugging logs
Expand All @@ -46,6 +54,26 @@ func (c *RealConfigurator) getConfigFileFromUserConfigDir() (model.Config, error
if err != nil {
return config, fmt.Errorf("couldn't unmarshal config file: %q", err)
}

for _, importPath := range config.ImportPaths {
importFilePath, err := c.fullImportPath(userHomeDir, importPath)
if err != nil {
return config, fmt.Errorf("couldn't get full import path: %q", err)
}

importFile, err := c.os.ReadFile(importFilePath)
if err != nil {
return config, fmt.Errorf("couldn't read import file %s: %q", importFilePath, err)
}

importConfig := model.Config{}
if err := toml.Unmarshal(importFile, &importConfig); err != nil {
return config, fmt.Errorf("couldn't unmarshal import file %s: %q", importFilePath, err)
}

config.SessionConfigs = append(config.SessionConfigs, importConfig.SessionConfigs...)
}

return config, nil
}

Expand Down

0 comments on commit f9a5318

Please sign in to comment.