feat: add Arch update helper and web UI
This commit is contained in:
65
internal/config/config.go
Normal file
65
internal/config/config.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
CheckAUR bool `json:"check_aur"`
|
||||
ReminderIntervalHours int `json:"reminder_interval_hours"`
|
||||
Terminal string `json:"terminal"`
|
||||
}
|
||||
|
||||
func Default() Config {
|
||||
return Config{
|
||||
CheckAUR: true,
|
||||
ReminderIntervalHours: 168,
|
||||
Terminal: "auto",
|
||||
}
|
||||
}
|
||||
|
||||
func Load(path string) (Config, error) {
|
||||
cfg := Default()
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return cfg, nil
|
||||
}
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
cfg = normalize(cfg)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func Save(path string, cfg Config) error {
|
||||
cfg = normalize(cfg)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data = append(data, '\n')
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
|
||||
func normalize(cfg Config) Config {
|
||||
if cfg.ReminderIntervalHours <= 0 {
|
||||
cfg.ReminderIntervalHours = Default().ReminderIntervalHours
|
||||
}
|
||||
if cfg.Terminal == "" {
|
||||
cfg.Terminal = Default().Terminal
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
Reference in New Issue
Block a user