From fcbbeccaa80c7e120933682b330272a1490c1cf5 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 29 Aug 2026 18:24:42 +0200 Subject: refactor: unify mods command with the search --- internal/config/cache.go | 12 ++++++++ internal/config/config.go | 72 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 11 deletions(-) (limited to 'internal/config') diff --git a/internal/config/cache.go b/internal/config/cache.go index 825af7f..aaaa97b 100644 --- a/internal/config/cache.go +++ b/internal/config/cache.go @@ -23,6 +23,18 @@ func LoadAPI() (*API, error) { return &a, nil } +func SaveAPI(api *API) error { + if err := os.MkdirAll(Dir, 0o755); err != nil { + return err + } + + data, err := json.MarshalIndent(api, "", " ") + if err != nil { + return err + } + return os.WriteFile(APIPath(), append(data, '\n'), 0o600) +} + type ModState struct { Compatible string `json:"compatible"` Latest string `json:"latest"` diff --git a/internal/config/config.go b/internal/config/config.go index 35842a1..05d96ad 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,17 +7,55 @@ import ( ) type Config struct { - ChannelID string `json:"channel_id"` - CheckIntervalMinutes int `json:"check_interval_minutes"` - MinecraftVersion string `json:"minecraft_version"` - FabricLoaderVersion string `json:"fabric_loader_version"` - LoaderMetaVersion string `json:"loader_meta_version"` - JavaPath string `json:"java_path"` - ServerLocation string `json:"server_location"` - BackupLocation string `json:"backup_location"` - MergePreCommand string `json:"merge_pre_command"` - MergePostCommand string `json:"merge_post_command"` - ModrinthMods []string `json:"modrinth_mods"` + ChannelID string `json:"channel_id"` + CheckIntervalMinutes int `json:"check_interval_minutes"` + MinecraftVersion string `json:"minecraft_version"` + FabricLoaderVersion string `json:"fabric_loader_version"` + LoaderMetaVersion string `json:"loader_meta_version"` + JavaPath string `json:"java_path"` + ServerLocation string `json:"server_location"` + BackupLocation string `json:"backup_location"` + MergePreCommand string `json:"merge_pre_command"` + MergePostCommand string `json:"merge_post_command"` + ModrinthMods []Mod `json:"modrinth_mods"` +} + +type Mod struct { + Slug string + Version string +} + +func (m Mod) Pinned() bool { + return m.Version != "" +} + +func (m Mod) MarshalJSON() ([]byte, error) { + if m.Version == "" { + return json.Marshal(m.Slug) + } + return json.Marshal(modObject{Slug: m.Slug, Version: m.Version}) +} + +func (m *Mod) UnmarshalJSON(data []byte) error { + var slug string + if err := json.Unmarshal(data, &slug); err == nil { + m.Slug = slug + m.Version = "" + return nil + } + + var obj modObject + if err := json.Unmarshal(data, &obj); err != nil { + return err + } + m.Slug = obj.Slug + m.Version = obj.Version + return nil +} + +type modObject struct { + Slug string `json:"slug"` + Version string `json:"version"` } const Dir = "config" @@ -43,3 +81,15 @@ func Load() (*Config, error) { return &c, nil } + +func Save(cfg *Config) error { + if err := os.MkdirAll(Dir, 0o755); err != nil { + return err + } + + data, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + return err + } + return os.WriteFile(Path(), append(data, '\n'), 0o644) +} -- cgit v1.3