mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
Write Config Changes to Disk
This commit is contained in:
parent
eb3ee9465c
commit
53e2e1f48c
4 changed files with 49 additions and 0 deletions
|
@ -2,6 +2,7 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"nfsense.net/nfsense/internal/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
|
@ -23,6 +24,16 @@ func (m *ConfigManager) ApplyPendingChanges() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
m.currentConfig = m.pendingConfig.Clone()
|
||||||
|
|
||||||
|
err := m.saveConfig(m.currentConfigFilePath, m.pendingConfig)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Save Current Config: %w", err)
|
||||||
|
}
|
||||||
|
err = os.Remove(m.pendingConfigFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Delete Pending Config: %w", err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,16 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
func (m *ConfigManager) DiscardPendingConfig() error {
|
func (m *ConfigManager) DiscardPendingConfig() error {
|
||||||
m.pendingConfig = m.currentConfig.Clone()
|
m.pendingConfig = m.currentConfig.Clone()
|
||||||
|
|
||||||
|
err := os.Remove(m.pendingConfigFilePath)
|
||||||
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
23
internal/config/save.go
Normal file
23
internal/config/save.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m *ConfigManager) saveConfig(path string, conf *definitions.Config) error {
|
||||||
|
data, err := json.MarshalIndent(conf, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Marshal Config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(path, data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Write Config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -39,6 +39,11 @@ func (t *ConfigTransaction) Commit() error {
|
||||||
return fmt.Errorf("validating Config before Apply: %w", err)
|
return fmt.Errorf("validating Config before Apply: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = t.configManager.saveConfig(t.configManager.pendingConfigFilePath, t.changes)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Save Current Config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
t.configManager.pendingConfig = t.changes.Clone()
|
t.configManager.pendingConfig = t.changes.Clone()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue