From b1fc7e1f38300267edf44c7d2afb502bd588f983 Mon Sep 17 00:00:00 2001 From: chblodg Date: Mon, 28 Feb 2022 11:41:39 -0800 Subject: [PATCH] Adding Partial matching, Filename Matching, and minor bug fixes (#9) Co-authored-by: christopher blodgett --- README.md | 9 +++++--- forgecli/forgecli.go | 51 ++++++++++++++++++++++++++++++++------------ forgecli/helpers.go | 15 ++++++++++++- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0f9cee8..f72f105 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,8 @@ To get started make sure you have a [CursedForge API Key](https://docs.curseforg - `destination:` Default is OS Client Mod folder, Target folder for the downloaded mods. - `family:` Used to filter mods based on server type. Options are Forge, Fabric, and Bukkit - `release:` Default is Release, Used to allow for Beta and Alpha mod downloads. -- `version:` Default is LATEST, but this is Minecraft VERSION. e.g. 1.18.2 +- `version:` Default is LATEST, but this is Minecraft VERSION. e.g. 1.18.2, + - PARTIAL matching is enabled, e.g. use 1.18 to pull back 1.18.2, 1.18.1, 1.18 mods - `clear:` Default is false, allows CLI to remove all mods before downloading new Mods. - `dependencies:` Default is True, this uses the mods required dependencies to download missing mods. - `debug:` Enable extra logging. @@ -72,7 +73,7 @@ To get started make sure you have a [CursedForge API Key](https://docs.curseforg **Basic Usage Command** ```bash -. ./forgecli.exe -forgekey '$2a$10...' -file "forgeMods.json" -family "fabric" -debug +. ./forgecli.exe -forgekey '$2a$10...' -file "forgeMods.json" -family "fabric" ``` **Field Description** @@ -128,4 +129,6 @@ go build ## TODO List: -- add fileName filter from json mods. Currently we have no method to pin versions. +- ~~add fileName filter from json mods. Currently we have no method to pin versions.~~ +- ~~add partial matching for versions 1.18 to pull 1.18.1 and 1.18.2~~ +- ~~better output for normal use~~ diff --git a/forgecli/forgecli.go b/forgecli/forgecli.go index 39f0575..d22625f 100644 --- a/forgecli/forgecli.go +++ b/forgecli/forgecli.go @@ -132,8 +132,10 @@ func (app *appEnv) GetModsByProjectIDs() { } projectIDs := strings.Split(app.projectIDs, ",") for _, projectID := range projectIDs { - logrus.Debugf("Getting Mod: %s", projectID) - err := app.GetModsFromForge(projectID, app.modReleaseType) + var convertedJSONMod JSONMod + convertedJSONMod.ProjectID = projectID + logrus.Debugf("Getting Mod: %s", convertedJSONMod.ProjectID) + err := app.GetModsFromForge(convertedJSONMod, app.modReleaseType) check(err) } } @@ -151,7 +153,7 @@ func (app *appEnv) GetModsByJSONFile() { } else { releaseType = app.modReleaseType } - err := app.GetModsFromForge(fileMod.ProjectID, releaseType) + err := app.GetModsFromForge(fileMod, releaseType) check(err) } } @@ -163,8 +165,10 @@ func (app *appEnv) GetModsDependencies() error { for _, mod := range app.modsToDownload { for _, modDep := range mod.Dependencies { if modDep.RelationType == 3 { - logrus.Debugf("Getting Mod dependency: %s", strconv.Itoa(modDep.ModID)) - err := app.GetModsFromForge(strconv.Itoa(modDep.ModID), releaseLookup["release"]) + var convertedJSONMod JSONMod + convertedJSONMod.ProjectID = strconv.Itoa(modDep.ModID) + logrus.Debugf("Getting Mod dependency: %s", convertedJSONMod.ProjectID) + err := app.GetModsFromForge(convertedJSONMod, releaseLookup["release"]) check(err) } } @@ -172,13 +176,13 @@ func (app *appEnv) GetModsDependencies() error { return nil } -func (app *appEnv) GetModsFromForge(modID string, releaseType ReleaseType) error { +func (app *appEnv) GetModsFromForge(modToGet JSONMod, releaseType ReleaseType) error { var resp ForgeMods pageIndex := 0 pageSize := 999 url := fmt.Sprintf( "https://api.curseforge.com/v1/mods/%s/files?gameVersionTypeID=%d&index=%d&pageSize=%d", - modID, app.forgeGameVersionType, pageIndex, pageSize, + modToGet.ProjectID, app.forgeGameVersionType, pageIndex, pageSize, ) err := app.FetchForgeAPIJSON(url, &resp) check(err) @@ -186,27 +190,46 @@ func (app *appEnv) GetModsFromForge(modID string, releaseType ReleaseType) error foundID := 0 var foundMod ForgeMod for _, currMod := range resp.Data { - if currMod.ID > foundID && app.ModFilter(currMod) { + if currMod.ID > foundID && app.ModFilter(currMod, modToGet) { foundID = currMod.ID foundMod = currMod } } if foundID == 0 { - return fmt.Errorf("could not find %s for minecraft version: %s or family: %s", modID, app.version, app.modfamily) + return fmt.Errorf("could not find %s for minecraft version: %s or family: %s", modToGet.ProjectID, app.version, app.modfamily) } app.modsToDownload[foundID] = foundMod - logrus.Infof("Found Lastest FileID: %d for Mod: %s", foundID, modID) + logrus.Infof("Found Lastest FileID: %d for Mod: %s", foundID, modToGet.ProjectID) return nil } -func (app *appEnv) ModFilter(currMod ForgeMod) bool { - if app.modfamily != "" { - if contains(currMod.GameVersions, string(app.version)) && contains(currMod.GameVersions, string(app.modfamily)) { +func (app *appEnv) ModFilter(currMod ForgeMod, modToGet JSONMod) bool { + // Filtering on Version, Filename, and Family + if modToGet.Filename != "" && app.modfamily != "" { + if containsPrefix(currMod.GameVersions, string(app.version)) && contains(currMod.GameVersions, string(app.modfamily)) && currMod.Filename == modToGet.Filename { return true } return false } - if contains(currMod.GameVersions, string(app.version)) { + + // Filtering on Version and Family + if app.modfamily != "" && modToGet.Filename == "" { + if containsPrefix(currMod.GameVersions, string(app.version)) && contains(currMod.GameVersions, string(app.modfamily)) { + return true + } + return false + } + + // Filtering on Version and Filename + if modToGet.Filename != "" && app.modfamily == "" { + if containsPrefix(currMod.GameVersions, string(app.version)) && currMod.Filename == modToGet.Filename { + return true + } + return false + } + + // Filter on just Version + if containsPrefix(currMod.GameVersions, string(app.version)) { return true } return false diff --git a/forgecli/helpers.go b/forgecli/helpers.go index 9454bf8..e8da929 100644 --- a/forgecli/helpers.go +++ b/forgecli/helpers.go @@ -36,6 +36,18 @@ func contains(s []string, e string) bool { return false } +func containsPrefix(s []string, e string) bool { + for _, a := range s { + logrus.Debugf("Checking has prefix: %s for Mod: %s", a, e) + if strings.HasPrefix(a, e) { + logrus.Debug("true") + return true + } + } + logrus.Debug("false") + return false +} + func (app *appEnv) GetTargetDirectory() { if app.destination != "" { return @@ -52,7 +64,8 @@ func (app *appEnv) GetTargetDirectory() { case "linux": app.destination = fmt.Sprintf("%s/Library/Application Support/minecraft/mods", user.HomeDir) default: - log.Fatalf(fmt.Sprintf("%s does not have a default directory, please provide target directory", os)) + err := fmt.Errorf("%s does not have a default directory, please provide target directory", os) + check(err) } }