configFile

package
v0.0.0-...-126e10b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 15 Imported by: 0

README

Config File Manager

The purpose of this runtime service is to provide a unified method of managing configuration files in a common root directory, and to provide other services a way to integrate with it to manage their own config files.

Events

This service uses the global runtime event bus. There is only a single event EventConfigChanged, which is emitted when a config file is changed. The arguments that are passed with this event are the config group, the key, and the value.

Other services can listen for this event to respond to their own config file being changed internally, or even externally if a file is edited outside of the application.

Here is an example of binding to this event during the Init of a service:

func (s *Service) Init(rt runtime.R) {
    rt.Events().On(config_file.EventConfigChanged, func(...any){
	    // handler logic here	
    })
}

Integration with other services

This service exports an integration interface Manager with an alias Dependencncy which are intended to be used by other services for dependency resolution (see runtime.HasDependencies), and expose just the methods which other services should use.

// Manager represents something that manages configurations.
type Manager interface {
    GetPath(path string) string
    ConfigDirectory() string
    SetConfigDirectory(string) error
    Configs() map[string]*Config
    GetConfig(string) (*Config, error)
    CreateConfig(path string) (*Config, error)
    LoadConfig(string) (*Config, error)
    SaveConfig(string) error
}

Other services should use the Manager or Dependency interfaces to resolve their dependency on this service.


This runtime service operates primarily by looking for other services which implement the following interfaces:

// HasConfig represents a something with a configuration file path and retrieval methods.
type HasConfig interface {
    ConfigFileName() string   // ConfigFilePath returns the path to the configuration file.
}
// HasDefaultConfig represents something with a default configuration.
type HasDefaultConfig interface {
    HasConfig
    DefaultConfig() Config // DefaultConfig returns the default configuration.
}

If another service implements HasConfig it will be used to create an empty config (if it doesnt exist). The Config method should use a reference to the Manager interface (obtained through implementing runtime.HasDependencies) to yield it's own config file.

Additionally, another service can implement HasDefaultConfig in order to declare what the default values of its own config file should be.

Documentation

Index

Constants

View Source
const (
	EventConfigChanged = "config file changed"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func (*Config) Group

func (c *Config) Group(s string) Object

func (*Config) GroupKeys

func (c *Config) GroupKeys() []string

func (*Config) Load

func (c *Config) Load() error

func (*Config) Marshal

func (c *Config) Marshal() ([]byte, error)

func (*Config) RootDirectory

func (c *Config) RootDirectory() string

func (*Config) Save

func (c *Config) Save() error

func (*Config) Unmarshal

func (c *Config) Unmarshal(data []byte) error

type Dependency

type Dependency = Manager

type HasConfig

type HasConfig interface {
	ConfigFileName() string // ConfigFilePath returns the path to the configuration file.
}

HasConfig represents a something with a configuration file path and retrieval methods.

type HasDefaultConfig

type HasDefaultConfig interface {
	HasConfig
	LoadConfig(*Config)          // the config, or default config, after it's been loaded
	DefaultConfig() (cfg Config) // DefaultConfig returns the default configuration.
}

HasDefaultConfig represents something with a default configuration.

type Manager

type Manager interface {
	servicemesh.Service
	GetFilePath(path string) string
	ConfigDirectory() string                               // ConfigDirectory returns the directory path where configurations are stored.
	SetConfigDirectory(string) error                       // SetConfigDirectory sets the directory path for configurations.
	Configs() map[string]*Config                           // Configs returns all configurations stored in the service.
	GetConfigByFileName(name string) (*Config, error)      // GetConfig retrieves a configuration by its path.
	CreateConfigWithFileName(name string) (*Config, error) // CreateConfig creates a new configuration file at the specified path.
	LoadConfigWithFileName(name string) (*Config, error)   // LoadConfig loads a configuration from the specified path.
	SaveConfigWithFileName(name string) error              // SaveConfig saves a configuration to the specified path.
}

Manager represents something that manages configurations.

type Object

type Object map[string]interface{}

func (Object) Get

func (n Object) Get(key string) interface{}

func (Object) GetBool

func (n Object) GetBool(key string) bool

func (Object) GetDuration

func (n Object) GetDuration(key string) time.Duration

func (Object) GetFloat

func (n Object) GetFloat(key string) float64

func (Object) GetInt

func (n Object) GetInt(key string) int

func (Object) GetJson

func (n Object) GetJson(key string) []byte

func (Object) GetString

func (n Object) GetString(key string) string

func (Object) GetStrings

func (n Object) GetStrings(key string) []string

func (Object) IsSet

func (n Object) IsSet(key string) bool

func (Object) Keys

func (n Object) Keys() (keys []string)

func (Object) Marshal

func (n Object) Marshal() ([]byte, error)

func (Object) Set

func (n Object) Set(key string, value interface{})

func (Object) SetDefault

func (n Object) SetDefault(key string, defaultValue interface{})

func (Object) Unmarshal

func (n Object) Unmarshal(data []byte) error

type Service

type Service struct {
	RootDirectory string
	// contains filtered or unexported fields
}

Service is a config file manager that marshals to and from json files.

func (*Service) ConfigDirectory

func (s *Service) ConfigDirectory() string

ConfigDirectory returns the directory path where the service's configurations are stored. If the directory is not set, it returns a default.

func (*Service) Configs

func (s *Service) Configs() map[string]*Config

Configs returns a map of all configurations stored in the service.

func (*Service) CreateConfigWithFileName

func (s *Service) CreateConfigWithFileName(path string) (*Config, error)

CreateConfigWithFileName creates a new configuration file at the specified path. It locks the service's mutex to ensure safe concurrent access.

func (*Service) DependenciesResolved

func (s *Service) DependenciesResolved() bool

func (*Service) GetConfigByFileName

func (s *Service) GetConfigByFileName(path string) (*Config, error)

GetConfigByFileName retrieves a configuration by its path from the service's internal map. It locks the service's mutex to ensure safe concurrent access.

func (*Service) GetFilePath

func (s *Service) GetFilePath(name string) string

GetFilePath returns the absolute path for a given incoming file

func (*Service) Init

func (s *Service) Init(mesh servicemesh.Mesh)

Init satisfies the runtime.IsRuntimeService interface

func (*Service) LoadConfigWithFileName

func (s *Service) LoadConfigWithFileName(path string) (*Config, error)

LoadConfigWithFileName loads a configuration from the specified path. It locks the service's mutex to ensure safe concurrent access.

func (*Service) Logger

func (s *Service) Logger() *slog.Logger

Logger satisfies the runtime.HasLogger interface

func (*Service) ModalTui

func (s *Service) ModalTui() (name string, model tea.Model)

func (*Service) Name

func (s *Service) Name() string

Name satisfies the runtime.IsRuntimeService interface

func (*Service) OnServiceAdded

func (s *Service) OnServiceAdded(service servicemesh.Service)

func (*Service) Ready

func (s *Service) Ready() bool

func (*Service) ResolveDependencies

func (s *Service) ResolveDependencies(services []servicemesh.Service)

func (*Service) SaveConfigWithFileName

func (s *Service) SaveConfigWithFileName(path string) error

SaveConfigWithFileName saves a configuration to the specified path. It locks the service's mutex to ensure safe concurrent access.

func (*Service) SetConfigDirectory

func (s *Service) SetConfigDirectory(dir string) error

SetConfigDirectory sets the directory path where the service's configurations should be stored. It locks the service's mutex to ensure safe concurrent access.

func (*Service) SetLogger

func (s *Service) SetLogger(l *slog.Logger)

BindLogger satisfies the runtime.HasLogger interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL