mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
Seperate Definitions by Sub System
This commit is contained in:
parent
14c8da64cc
commit
72bf96295d
31 changed files with 120 additions and 108 deletions
|
@ -5,7 +5,7 @@ import (
|
|||
"os"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
// ApplyPendingChanges Takes all pending Changes and Tries to Apply them using the Registered Apply Functions.
|
||||
|
@ -47,6 +47,6 @@ func revertToCurrent(m *ConfigManager) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (m *ConfigManager) RegisterApplyFunction(fn func(currentConfig definitions.Config, pendingConfig definitions.Config) error) {
|
||||
func (m *ConfigManager) RegisterApplyFunction(fn func(currentConfig config.Config, pendingConfig config.Config) error) {
|
||||
m.applyFunctions = append(m.applyFunctions, fn)
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package config
|
||||
|
||||
import "nfsense.net/nfsense/internal/definitions"
|
||||
import (
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func (m *ConfigManager) GetCurrentConfig() definitions.Config {
|
||||
func (m *ConfigManager) GetCurrentConfig() config.Config {
|
||||
return *m.currentConfig.Clone()
|
||||
}
|
||||
|
||||
func (m *ConfigManager) GetPendingConfig() definitions.Config {
|
||||
func (m *ConfigManager) GetPendingConfig() config.Config {
|
||||
return *m.pendingConfig.Clone()
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func (m *ConfigManager) LoadCurrentConfigFromDisk() error {
|
||||
var config definitions.Config
|
||||
var conf config.Config
|
||||
configFile, err := os.Open(m.currentConfigFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening Config File %w", err)
|
||||
|
@ -18,22 +18,22 @@ func (m *ConfigManager) LoadCurrentConfigFromDisk() error {
|
|||
|
||||
jsonParser := json.NewDecoder(configFile)
|
||||
jsonParser.DisallowUnknownFields()
|
||||
err = jsonParser.Decode(&config)
|
||||
err = jsonParser.Decode(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decoding Config File %w", err)
|
||||
}
|
||||
|
||||
err = definitions.ValidateConfig(&config)
|
||||
err = config.ValidateConfig(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating Config: %w", err)
|
||||
}
|
||||
|
||||
m.currentConfig = &config
|
||||
m.currentConfig = &conf
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ConfigManager) LoadPendingConfigFromDisk() error {
|
||||
var config definitions.Config
|
||||
var conf config.Config
|
||||
configFile, err := os.Open(m.pendingConfigFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening Config File %w", err)
|
||||
|
@ -42,16 +42,16 @@ func (m *ConfigManager) LoadPendingConfigFromDisk() error {
|
|||
|
||||
jsonParser := json.NewDecoder(configFile)
|
||||
jsonParser.DisallowUnknownFields()
|
||||
err = jsonParser.Decode(&config)
|
||||
err = jsonParser.Decode(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decoding Config File %w", err)
|
||||
}
|
||||
|
||||
err = definitions.ValidateConfig(&config)
|
||||
err = config.ValidateConfig(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating Config: %w", err)
|
||||
}
|
||||
|
||||
m.pendingConfig = &config
|
||||
m.pendingConfig = &conf
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,27 +3,27 @@ package config
|
|||
import (
|
||||
"sync"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
type ConfigManager struct {
|
||||
currentConfigFilePath string
|
||||
pendingConfigFilePath string
|
||||
|
||||
currentConfig *definitions.Config
|
||||
pendingConfig *definitions.Config
|
||||
currentConfig *config.Config
|
||||
pendingConfig *config.Config
|
||||
|
||||
transactionMutex sync.Mutex
|
||||
|
||||
applyFunctions []func(currentConfig definitions.Config, pendingConfig definitions.Config) error
|
||||
applyFunctions []func(currentConfig config.Config, pendingConfig config.Config) error
|
||||
}
|
||||
|
||||
func CreateConfigManager() *ConfigManager {
|
||||
manager := ConfigManager{
|
||||
currentConfigFilePath: "config.json",
|
||||
pendingConfigFilePath: "pending.json",
|
||||
currentConfig: &definitions.Config{},
|
||||
pendingConfig: &definitions.Config{},
|
||||
currentConfig: &config.Config{},
|
||||
pendingConfig: &config.Config{},
|
||||
}
|
||||
return &manager
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func (m *ConfigManager) saveConfig(path string, conf *definitions.Config) error {
|
||||
func (m *ConfigManager) saveConfig(path string, conf *config.Config) error {
|
||||
data, err := json.MarshalIndent(conf, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Marshal Config: %w", err)
|
||||
|
|
|
@ -4,17 +4,17 @@ import (
|
|||
"fmt"
|
||||
"sync"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
type ConfigTransaction struct {
|
||||
finished bool
|
||||
mutex sync.Mutex
|
||||
configManager *ConfigManager
|
||||
changes *definitions.Config
|
||||
changes *config.Config
|
||||
}
|
||||
|
||||
func (m *ConfigManager) StartTransaction() (*ConfigTransaction, *definitions.Config) {
|
||||
func (m *ConfigManager) StartTransaction() (*ConfigTransaction, *config.Config) {
|
||||
m.transactionMutex.Lock()
|
||||
confCopy := m.pendingConfig.Clone()
|
||||
return &ConfigTransaction{
|
||||
|
@ -34,7 +34,7 @@ func (t *ConfigTransaction) Commit() error {
|
|||
t.finished = true
|
||||
defer t.configManager.transactionMutex.Unlock()
|
||||
|
||||
err := definitions.ValidateConfig(t.changes)
|
||||
err := config.ValidateConfig(t.changes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating Config before Apply: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue