ax(mining): remove banned fmt import from config_manager.go
Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
427f0e9fad
commit
fb1d4f74e2
1 changed files with 16 additions and 17 deletions
|
|
@ -2,7 +2,6 @@ package mining
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
|
@ -13,7 +12,7 @@ import (
|
|||
// configMutex protects concurrent access to config file operations
|
||||
var configMutex sync.RWMutex
|
||||
|
||||
// MinerAutostartConfig{MinerType: "xmrig", Autostart: true, Config: &cfg}
|
||||
// MinerAutostartConfig{MinerType: "xmrig", Autostart: true, Config: &config}
|
||||
type MinerAutostartConfig struct {
|
||||
MinerType string `json:"minerType"`
|
||||
Autostart bool `json:"autostart"`
|
||||
|
|
@ -47,16 +46,16 @@ func getMinersConfigPath() (string, error) {
|
|||
return xdg.ConfigFile("lethean-desktop/miners/config.json")
|
||||
}
|
||||
|
||||
// cfg, err := LoadMinersConfig()
|
||||
// configuration, err := LoadMinersConfig()
|
||||
// if err != nil { return err }
|
||||
// cfg.Database.Enabled = false
|
||||
// configuration.Database.Enabled = false
|
||||
func LoadMinersConfig() (*MinersConfig, error) {
|
||||
configMutex.RLock()
|
||||
defer configMutex.RUnlock()
|
||||
|
||||
configPath, err := getMinersConfigPath()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not determine miners config path: %w", err)
|
||||
return nil, ErrInternal("could not determine miners config path").WithCause(err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
|
|
@ -68,12 +67,12 @@ func LoadMinersConfig() (*MinersConfig, error) {
|
|||
Database: defaultDatabaseConfig(),
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to read miners config file: %w", err)
|
||||
return nil, ErrInternal("failed to read miners config file").WithCause(err)
|
||||
}
|
||||
|
||||
var configuration MinersConfig
|
||||
if err := json.Unmarshal(data, &configuration); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal miners config: %w", err)
|
||||
return nil, ErrInternal("failed to unmarshal miners config").WithCause(err)
|
||||
}
|
||||
|
||||
// Apply default database config if not set (for backwards compatibility)
|
||||
|
|
@ -84,25 +83,25 @@ func LoadMinersConfig() (*MinersConfig, error) {
|
|||
return &configuration, nil
|
||||
}
|
||||
|
||||
// cfg.Database.RetentionDays = 60
|
||||
// if err := SaveMinersConfig(cfg); err != nil { return err }
|
||||
// configuration.Database.RetentionDays = 60
|
||||
// if err := SaveMinersConfig(configuration); err != nil { return err }
|
||||
func SaveMinersConfig(configuration *MinersConfig) error {
|
||||
configMutex.Lock()
|
||||
defer configMutex.Unlock()
|
||||
|
||||
configPath, err := getMinersConfigPath()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not determine miners config path: %w", err)
|
||||
return ErrInternal("could not determine miners config path").WithCause(err)
|
||||
}
|
||||
|
||||
dir := filepath.Dir(configPath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create config directory: %w", err)
|
||||
return ErrInternal("failed to create config directory").WithCause(err)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(configuration, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal miners config: %w", err)
|
||||
return ErrInternal("failed to marshal miners config").WithCause(err)
|
||||
}
|
||||
|
||||
return AtomicWriteFile(configPath, data, 0600)
|
||||
|
|
@ -115,7 +114,7 @@ func UpdateMinersConfig(modifier func(*MinersConfig) error) error {
|
|||
|
||||
configPath, err := getMinersConfigPath()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not determine miners config path: %w", err)
|
||||
return ErrInternal("could not determine miners config path").WithCause(err)
|
||||
}
|
||||
|
||||
// Load current config
|
||||
|
|
@ -128,11 +127,11 @@ func UpdateMinersConfig(modifier func(*MinersConfig) error) error {
|
|||
Database: defaultDatabaseConfig(),
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("failed to read miners config file: %w", err)
|
||||
return ErrInternal("failed to read miners config file").WithCause(err)
|
||||
}
|
||||
} else {
|
||||
if err := json.Unmarshal(data, &configuration); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal miners config: %w", err)
|
||||
return ErrInternal("failed to unmarshal miners config").WithCause(err)
|
||||
}
|
||||
if configuration.Database.RetentionDays == 0 {
|
||||
configuration.Database = defaultDatabaseConfig()
|
||||
|
|
@ -147,12 +146,12 @@ func UpdateMinersConfig(modifier func(*MinersConfig) error) error {
|
|||
// Save atomically
|
||||
dir := filepath.Dir(configPath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create config directory: %w", err)
|
||||
return ErrInternal("failed to create config directory").WithCause(err)
|
||||
}
|
||||
|
||||
newData, err := json.MarshalIndent(configuration, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal miners config: %w", err)
|
||||
return ErrInternal("failed to marshal miners config").WithCause(err)
|
||||
}
|
||||
|
||||
return AtomicWriteFile(configPath, newData, 0600)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue