|
| 1 | +package ficsitcli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/spf13/viper" |
| 11 | +) |
| 12 | + |
| 13 | +type ModpackReleaseResponse struct { |
| 14 | + GetModpackRelease struct { |
| 15 | + Lockfile string `json:"lockfile"` |
| 16 | + } `json:"getModpackRelease"` |
| 17 | +} |
| 18 | + |
| 19 | +type PlatformTarget struct { |
| 20 | + Hash string `json:"hash"` |
| 21 | + Link string `json:"link"` |
| 22 | +} |
| 23 | + |
| 24 | +type ModEntry struct { |
| 25 | + Dependencies interface{} `json:"dependencies"` |
| 26 | + Targets map[string]PlatformTarget `json:"targets"` |
| 27 | + Version string `json:"version"` |
| 28 | +} |
| 29 | + |
| 30 | +type Lockfile struct { |
| 31 | + Mods map[string]ModEntry `json:"mods"` |
| 32 | + Version int `json:"version"` |
| 33 | +} |
| 34 | + |
| 35 | +func (f *ficsitCLI) InstallModpackRelease(modpackID string, release string, name string) error { |
| 36 | + return f.action(ActionInstall, newItem(modpackID, release), func(l *slog.Logger, taskUpdates chan<- taskUpdate) error { |
| 37 | + selectedInstallation := f.GetSelectedInstall() |
| 38 | + if selectedInstallation == nil { |
| 39 | + return fmt.Errorf("no installation selected") |
| 40 | + } |
| 41 | + |
| 42 | + l = l.With( |
| 43 | + slog.String("install", selectedInstallation.Path), |
| 44 | + slog.String("profile", selectedInstallation.Profile), |
| 45 | + ) |
| 46 | + f.AddProfile(name + "-" + release) |
| 47 | + profileErr := f.setProfileModpack(l, name+"-"+release) |
| 48 | + if profileErr != nil { |
| 49 | + l.Error("failed to set profile", slog.Any("error", profileErr)) |
| 50 | + return fmt.Errorf("failed to set profile: %w", profileErr) |
| 51 | + } |
| 52 | + |
| 53 | + lockfile, err := getLockfile(modpackID, release) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get lockfile: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + for modID, mod := range lockfile.Mods { |
| 59 | + modErr := f.installModVersionModpack(l, modID, mod.Version) |
| 60 | + if modErr != nil { |
| 61 | + l.Error("failed to install mod", |
| 62 | + slog.String("mod", modID), |
| 63 | + slog.String("version", mod.Version), |
| 64 | + slog.Any("error", modErr)) |
| 65 | + |
| 66 | + return fmt.Errorf("failed to install mod: %s@%s: %w", |
| 67 | + modID, mod.Version, modErr) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + installErr := f.apply(l, taskUpdates) |
| 72 | + if installErr != nil { |
| 73 | + l.Error("failed to install", slog.Any("error", installErr)) |
| 74 | + return installErr |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +type graphQLResponse struct { |
| 82 | + Data struct { |
| 83 | + GetModpackRelease struct { |
| 84 | + Lockfile string `json:"lockfile"` |
| 85 | + } `json:"getModpackRelease"` |
| 86 | + } `json:"data"` |
| 87 | + Errors []struct { |
| 88 | + Message string `json:"message"` |
| 89 | + } `json:"errors"` |
| 90 | +} |
| 91 | + |
| 92 | +func getLockfile(modpackID string, release string) (Lockfile, error) { |
| 93 | + endpoint := viper.GetString("api-base") + viper.GetString("graphql-api") |
| 94 | + |
| 95 | + body := map[string]interface{}{ |
| 96 | + "query": `query GetModpackRelease($modpackID: ModpackID!, $version: String!) { |
| 97 | + getModpackRelease(modpackID: $modpackID, version: $version) { |
| 98 | + lockfile |
| 99 | + } |
| 100 | + }`, |
| 101 | + "variables": map[string]interface{}{ |
| 102 | + "modpackID": modpackID, |
| 103 | + "version": release, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + jsonBody, _ := json.Marshal(body) |
| 108 | + resp, err := (&http.Client{}).Post(endpoint, "application/json", bytes.NewBuffer(jsonBody)) |
| 109 | + if err != nil { |
| 110 | + return Lockfile{}, fmt.Errorf("failed to query GraphQL: %w", err) |
| 111 | + } |
| 112 | + defer resp.Body.Close() |
| 113 | + |
| 114 | + var gqlResp graphQLResponse |
| 115 | + if err := json.NewDecoder(resp.Body).Decode(&gqlResp); err != nil { |
| 116 | + return Lockfile{}, fmt.Errorf("failed to decode response: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + if len(gqlResp.Errors) > 0 { |
| 120 | + return Lockfile{}, fmt.Errorf("GraphQL error: %s", gqlResp.Errors[0].Message) |
| 121 | + } |
| 122 | + if gqlResp.Data.GetModpackRelease.Lockfile == "" { |
| 123 | + return Lockfile{}, fmt.Errorf("lockfile not found for %s@%s", modpackID, release) |
| 124 | + } |
| 125 | + |
| 126 | + var lockfile Lockfile |
| 127 | + if err := json.Unmarshal([]byte(gqlResp.Data.GetModpackRelease.Lockfile), &lockfile); err != nil { |
| 128 | + return Lockfile{}, fmt.Errorf("failed to parse lockfile: %w", err) |
| 129 | + } |
| 130 | + return lockfile, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (f *ficsitCLI) setProfileModpack(l *slog.Logger, profile string) error { |
| 134 | + selectedInstallation := f.GetSelectedInstall() |
| 135 | + |
| 136 | + if selectedInstallation == nil { |
| 137 | + l.Error("no installation selected") |
| 138 | + return fmt.Errorf("no installation selected") |
| 139 | + } |
| 140 | + |
| 141 | + if selectedInstallation.Profile == profile { |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + err := selectedInstallation.SetProfile(f.ficsitCli, profile) |
| 146 | + if err != nil { |
| 147 | + l.Error("failed to set profile", slog.Any("error", err)) |
| 148 | + return fmt.Errorf("failed to set profile: %w", err) |
| 149 | + } |
| 150 | + |
| 151 | + err = f.ficsitCli.Installations.Save() |
| 152 | + if err != nil { |
| 153 | + l.Error("failed to save installations", slog.Any("error", err)) |
| 154 | + } |
| 155 | + |
| 156 | + f.EmitGlobals() |
| 157 | + f.EmitModsChange() |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (f *ficsitCLI) installModVersionModpack(l *slog.Logger, mod string, version string) error { |
| 163 | + selectedInstallation := f.GetSelectedInstall() |
| 164 | + |
| 165 | + if selectedInstallation == nil { |
| 166 | + return fmt.Errorf("no installation selected") |
| 167 | + } |
| 168 | + |
| 169 | + l = l.With( |
| 170 | + slog.String("install", selectedInstallation.Path), |
| 171 | + slog.String("profile", selectedInstallation.Profile), |
| 172 | + ) |
| 173 | + |
| 174 | + profile := f.GetProfile(selectedInstallation.Profile) |
| 175 | + |
| 176 | + profileErr := profile.AddMod(mod, version) |
| 177 | + if profileErr != nil { |
| 178 | + l.Error("failed to add mod", slog.Any("error", profileErr)) |
| 179 | + return fmt.Errorf("failed to add mod: %s@%s: %w", mod, version, profileErr) |
| 180 | + } |
| 181 | + |
| 182 | + err := f.ficsitCli.Profiles.Save() |
| 183 | + if err != nil { |
| 184 | + l.Error("failed to save profile", slog.Any("error", err)) |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
0 commit comments