config_paths.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package csconfig
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. )
  6. type ConfigurationPaths struct {
  7. ConfigDir string `yaml:"config_dir"`
  8. DataDir string `yaml:"data_dir,omitempty"`
  9. SimulationFilePath string `yaml:"simulation_path,omitempty"`
  10. HubIndexFile string `yaml:"index_path,omitempty"` //path of the .index.json
  11. HubDir string `yaml:"hub_dir,omitempty"`
  12. PluginDir string `yaml:"plugin_dir,omitempty"`
  13. NotificationDir string `yaml:"notification_dir,omitempty"`
  14. PatternDir string `yaml:"pattern_dir,omitempty"`
  15. }
  16. func (c *Config) loadConfigurationPaths() error {
  17. var err error
  18. if c.ConfigPaths == nil {
  19. return fmt.Errorf("no configuration paths provided")
  20. }
  21. if c.ConfigPaths.DataDir == "" {
  22. return fmt.Errorf("please provide a data directory with the 'data_dir' directive in the 'config_paths' section")
  23. }
  24. if c.ConfigPaths.HubDir == "" {
  25. c.ConfigPaths.HubDir = filepath.Clean(c.ConfigPaths.ConfigDir + "/hub")
  26. }
  27. if c.ConfigPaths.HubIndexFile == "" {
  28. c.ConfigPaths.HubIndexFile = filepath.Clean(c.ConfigPaths.HubDir + "/.index.json")
  29. }
  30. if c.ConfigPaths.PatternDir == "" {
  31. c.ConfigPaths.PatternDir = filepath.Join(c.ConfigPaths.ConfigDir, "patterns/")
  32. }
  33. var configPathsCleanup = []*string{
  34. &c.ConfigPaths.HubDir,
  35. &c.ConfigPaths.HubIndexFile,
  36. &c.ConfigPaths.ConfigDir,
  37. &c.ConfigPaths.DataDir,
  38. &c.ConfigPaths.SimulationFilePath,
  39. &c.ConfigPaths.PluginDir,
  40. &c.ConfigPaths.NotificationDir,
  41. &c.ConfigPaths.PatternDir,
  42. }
  43. for _, k := range configPathsCleanup {
  44. if *k == "" {
  45. continue
  46. }
  47. *k, err = filepath.Abs(*k)
  48. if err != nil {
  49. return fmt.Errorf("failed to get absolute path of '%s': %w", *k, err)
  50. }
  51. }
  52. return nil
  53. }