Skip to content

Commit

Permalink
VersionFromMrpackFile
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Sep 8, 2023
1 parent 343d0f6 commit e427528
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions modrinth/api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

import (
"errors"
"github.com/nothub/mrpack-install/web"
"os"
"testing"
)

Expand Down Expand Up @@ -157,6 +160,27 @@ func TestClient_VersionFromHash(t *testing.T) {
}
}

func TestClient_VersionFromMrpackFile(t *testing.T) {
t.Parallel()
if _, err := os.Stat("adrena.mrpack"); err != nil {
if errors.Is(err, os.ErrNotExist) {
_, err := web.DefaultClient.DownloadFile("https://cdn.modrinth.com/data/H9OFWiay/versions/GUstmAjF/Adrenaserver-1.4.0%2B1.20.1.fabric.mrpack", ".", "adrena.mrpack")
if err != nil {
t.Fatal(err)
}
} else {
t.Fatal(err)
}
}
version, err := client.VersionFromMrpackFile("adrena.mrpack")
if err != nil {
t.Fatal(err)
}
if version.Id != "GUstmAjF" {
t.Fatalf("wrong id! actual=%q expected=%q\n", version.Id, "GUstmAjF")
}
}

func TestClient_GetLatestGameVersion(t *testing.T) {
t.Parallel()
version, err := client.GetLatestGameVersion()
Expand Down
12 changes: 12 additions & 0 deletions modrinth/api/endpoints.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package api

import (
"crypto/sha512"
"errors"
"fmt"
"net/url"
"os"
)

const apiVersion = "v2"
Expand Down Expand Up @@ -188,6 +191,15 @@ func (client *ModrinthClient) VersionFromHash(hash string, algorithm HashAlgo) (
return version, nil
}

func (client *ModrinthClient) VersionFromMrpackFile(path string) (*Version, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
hash := fmt.Sprintf("%x", sha512.Sum512(b))
return client.VersionFromHash(hash, "sha512")
}

// GetLatestGameVersion https://docs.modrinth.com/api-spec/#tag/tags/operation/versionList
func (client *ModrinthClient) GetLatestGameVersion() (string, error) {
u, err := url.Parse(client.BaseUrl + apiVersion + "/tag/game_version")
Expand Down

0 comments on commit e427528

Please sign in to comment.