Move locating mods to index

This commit is contained in:
comp500 2019-06-15 00:40:56 +01:00
parent 91693cd3eb
commit adcde05693
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5
3 changed files with 47 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"sort" "sort"
"time" "time"
"strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/vbauerster/mpb/v4" "github.com/vbauerster/mpb/v4"
@ -266,3 +267,17 @@ func (in *Index) RefreshFileWithHash(path, format, hash string, mod bool) error
in.resortIndex() in.resortIndex()
return nil return nil
} }
// FindMod finds a mod in the index and returns it's path and whether it has been found
func (in Index) FindMod(modName string) (string, bool) {
for _, v := range in.Files {
if v.MetaFile {
_, file := filepath.Split(v.File);
fileTrimmed := strings.TrimSuffix(file, ModExtension)
if fileTrimmed == modName {
return v.File, true
}
}
}
return "", false
}

View File

@ -29,7 +29,8 @@ func init() {
return cmdImport(core.FlagsFromContext(c), c.Args().Get(0)) return cmdImport(core.FlagsFromContext(c), c.Args().Get(0))
}, },
}, { }, {
Name: "open", Name: "open",
// TODO: change semantics to "project" rather than "mod", as this supports texture packs and misc content as well
Usage: "Open the project page for a curseforge mod in your browser", Usage: "Open the project page for a curseforge mod in your browser",
Aliases: []string{"doc"}, Aliases: []string{"doc"},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
@ -143,7 +144,21 @@ func cmdDoc(flags core.Flags, mod string) error {
return cli.NewExitError("You must specify a mod.", 1) return cli.NewExitError("You must specify a mod.", 1)
} }
modData, err := core.LoadMod(core.ResolveMod(mod, flags)) fmt.Println("Loading modpack...")
pack, err := core.LoadPack(flags)
if err != nil {
return cli.NewExitError(err, 1)
}
index, err := pack.LoadIndex()
if err != nil {
return cli.NewExitError(err, 1)
}
resolvedMod, ok := index.FindMod(mod)
if !ok {
// TODO: should this auto-refresh???????
return cli.NewExitError("You don't have this mod installed.", 1)
}
modData, err := core.LoadMod(resolvedMod)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -152,6 +167,7 @@ func cmdDoc(flags core.Flags, mod string) error {
return cli.NewExitError("This mod doesn't seem to be a curseforge mod!", 1) return cli.NewExitError("This mod doesn't seem to be a curseforge mod!", 1)
} }
cfUpdateData := updateData.(cfUpdater) cfUpdateData := updateData.(cfUpdater)
fmt.Println("Opening browser...")
url := "https://minecraft.curseforge.com/projects/" + strconv.Itoa(cfUpdateData.ProjectID) url := "https://minecraft.curseforge.com/projects/" + strconv.Itoa(cfUpdateData.ProjectID)
err = open.Start(url) err = open.Start(url)
if err != nil { if err != nil {
@ -178,6 +194,11 @@ type cfUpdater struct {
func (u cfUpdater) DoUpdate(mod core.Mod) (bool, error) { func (u cfUpdater) DoUpdate(mod core.Mod) (bool, error) {
// TODO: implement updating // TODO: implement updating
// modInfoData, err := getModInfo(u.ProjectID)
// if err != nil {
// return false, err
// }
return false, nil return false, nil
} }

15
main.go
View File

@ -1,4 +1,5 @@
package main package main
import ( import (
"fmt" "fmt"
"log" "log"
@ -52,11 +53,6 @@ func cmdDelete(flags core.Flags, mod string) error {
if len(mod) == 0 { if len(mod) == 0 {
return cli.NewExitError("You must specify a mod.", 1) return cli.NewExitError("You must specify a mod.", 1)
} }
resolvedMod := core.ResolveMod(mod, flags)
err := os.Remove(resolvedMod)
if err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println("Loading modpack...") fmt.Println("Loading modpack...")
pack, err := core.LoadPack(flags) pack, err := core.LoadPack(flags)
if err != nil { if err != nil {
@ -66,6 +62,14 @@ func cmdDelete(flags core.Flags, mod string) error {
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
resolvedMod, ok := index.FindMod(mod)
if !ok {
return cli.NewExitError("You don't have this mod installed.", 1)
}
err = os.Remove(resolvedMod)
if err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println("Removing mod from index...") fmt.Println("Removing mod from index...")
err = index.RemoveFile(resolvedMod) err = index.RemoveFile(resolvedMod)
if err != nil { if err != nil {
@ -117,4 +121,3 @@ func cmdRefresh(flags core.Flags) error {
fmt.Println("Index refreshed!") fmt.Println("Index refreshed!")
return nil return nil
} }