hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/internal/modrinth/modrinth.go
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-29 18:24:42 +0200
committerhachem <im@hachem.wtf>2026-08-29 18:24:42 +0200
commitfcbbeccaa80c7e120933682b330272a1490c1cf5 (patch)
tree11de4610f01a3861f7bbea94da63dd2d070f420a /internal/modrinth/modrinth.go
parent4806ee7ef95f0f04079856f7753399b7b7aa6330 (diff)
refactor: unify mods command with the search
Diffstat (limited to 'internal/modrinth/modrinth.go')
-rw-r--r--internal/modrinth/modrinth.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/internal/modrinth/modrinth.go b/internal/modrinth/modrinth.go
index 8060f3b..a6ed9eb 100644
--- a/internal/modrinth/modrinth.go
+++ b/internal/modrinth/modrinth.go
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
+ "net/url"
"os"
"time"
)
@@ -37,6 +38,43 @@ func NewClient() *Client {
return &Client{http: &http.Client{Timeout: 30 * time.Second}}
}
+type SearchHit struct {
+ Slug string `json:"slug"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+ Downloads int `json:"downloads"`
+}
+
+func (c *Client) Search(query, mcVersion, loader string) ([]SearchHit, error) {
+ facets := fmt.Sprintf(`[["project_type:mod"],["categories:%s"],["versions:%s"]]`, loader, mcVersion)
+ endpoint := fmt.Sprintf("%s/search?limit=20&index=relevance&query=%s&facets=%s",
+ apiBase, url.QueryEscape(query), url.QueryEscape(facets))
+
+ req, err := http.NewRequest(http.MethodGet, endpoint, nil)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("User-Agent", userAgent)
+
+ resp, err := c.http.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("modrinth: search returned %s", resp.Status)
+ }
+
+ var result struct {
+ Hits []SearchHit `json:"hits"`
+ }
+ if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
+ return nil, err
+ }
+ return result.Hits, nil
+}
+
func (c *Client) GetVersions(slug string) ([]Version, error) {
url := fmt.Sprintf("%s/project/%s/version", apiBase, slug)
@@ -77,6 +115,15 @@ func LatestVersion(versions []Version) *Version {
return &versions[best]
}
+func FindVersion(versions []Version, versionNumber string) *Version {
+ for i := range versions {
+ if versions[i].VersionNumber == versionNumber {
+ return &versions[i]
+ }
+ }
+ return nil
+}
+
func CompatibleVersion(versions []Version, mcVersion, loader string) *Version {
var compatible []Version
for _, v := range versions {