2
0

configmanager.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package config
  2. import (
  3. "sync"
  4. "github.com/opencost/opencost/core/pkg/storage"
  5. )
  6. //--------------------------------------------------------------------------
  7. // ConfigFileManager
  8. //--------------------------------------------------------------------------
  9. // ConfigFileManager is a fascade for a central API used to create and watch
  10. // config files.
  11. type ConfigFileManager struct {
  12. lock sync.Mutex
  13. store storage.Storage
  14. files map[string]*ConfigFile
  15. }
  16. // NewConfigFileManager creates a new backing storage and configuration file manager
  17. func NewConfigFileManager(configStore storage.Storage) *ConfigFileManager {
  18. if configStore == nil {
  19. configStore = storage.NewFileStorage("/")
  20. }
  21. return &ConfigFileManager{
  22. store: configStore,
  23. files: make(map[string]*ConfigFile),
  24. }
  25. }
  26. // ConfigFileAt returns an existing configuration file for the provided path if it exists. Otherwise,
  27. // a new instance is created and returned. Note that the path does not have to exist in order for the
  28. // instance to be created. It can exist as a potential file path on the storage, and be written to
  29. // later
  30. func (cfm *ConfigFileManager) ConfigFileAt(path string) *ConfigFile {
  31. cfm.lock.Lock()
  32. defer cfm.lock.Unlock()
  33. if cf, ok := cfm.files[path]; ok {
  34. return cf
  35. }
  36. cf := NewConfigFile(cfm.store, path)
  37. cfm.files[path] = cf
  38. return cf
  39. }