init commit
This commit is contained in:
44
core/settings.go
Normal file
44
core/settings.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// AppSettings represents persistent application settings.
|
||||
type AppSettings struct {
|
||||
RunOnStartup bool `json:"RunOnStartup"`
|
||||
AutoConnect bool `json:"AutoConnect"`
|
||||
AutoReconnect bool `json:"AutoReconnect"`
|
||||
LastConfigID string `json:"LastConfigId,omitempty"`
|
||||
}
|
||||
|
||||
func settingsPath() string {
|
||||
appData, _ := os.UserConfigDir()
|
||||
return filepath.Join(appData, "kettuRay", "settings.json")
|
||||
}
|
||||
|
||||
// LoadSettings loads settings from disk, returning defaults on error.
|
||||
func LoadSettings() *AppSettings {
|
||||
data, err := os.ReadFile(settingsPath())
|
||||
if err != nil {
|
||||
return &AppSettings{}
|
||||
}
|
||||
var s AppSettings
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return &AppSettings{}
|
||||
}
|
||||
return &s
|
||||
}
|
||||
|
||||
// Save writes settings to disk.
|
||||
func (s *AppSettings) Save() error {
|
||||
path := settingsPath()
|
||||
_ = os.MkdirAll(filepath.Dir(path), 0o755)
|
||||
data, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
Reference in New Issue
Block a user