Improve import code, add promptui dep

This commit is contained in:
comp500
2019-06-13 21:44:49 +01:00
parent 25747ad8bd
commit 683e125987
5 changed files with 102 additions and 18 deletions

View File

@@ -110,6 +110,7 @@ func createModFile(flags core.Flags, modInfo modInfo, fileInfo modFileInfo) erro
Download: core.ModDownload{
URL: fileInfo.DownloadURL,
// TODO: murmur2 hashing may be unstable in curse api, calculate the hash manually?
// TODO: check if the hash is invalid (e.g. 0)
HashFormat: "murmur2",
Hash: strconv.Itoa(fileInfo.Fingerprint),
},
@@ -117,9 +118,11 @@ func createModFile(flags core.Flags, modInfo modInfo, fileInfo modFileInfo) erro
}
modMeta.SetMetaName(modInfo.Slug, flags)
fmt.Printf("%#v\n", modMeta)
// If the file already exists, this will overwrite it!!!
// TODO: Should this be improved?
// Current strategy is to go ahead and do stuff without asking, with the assumption that you are using
// VCS anyway.
// TODO: what to do if it already exists?
// TODO: add to index
return modMeta.Write()
}
@@ -212,6 +215,7 @@ type cfUpdater struct {
}
func (u cfUpdater) DoUpdate(mod core.Mod) (bool, error) {
// TODO: implement updating
return false, nil
}
@@ -220,4 +224,3 @@ func (u cfUpdater) ToMap() (map[string]interface{}, error) {
err := mapstructure.Decode(u, &newMap)
return newMap, err
}

View File

@@ -1,4 +1,5 @@
package curseforge
import (
"encoding/json"
"fmt"
@@ -42,38 +43,55 @@ type twitchPackMeta struct {
}
func cmdImport(flags core.Flags, file string) error {
// TODO: implement
var packMeta twitchPackMeta
// TODO: is this relative to something?
f, err := os.Open(file)
if err != nil {
return err
return cli.NewExitError(err, 1)
}
err = json.NewDecoder(f).Decode(&packMeta)
if err != nil {
return err
return cli.NewExitError(err, 1)
}
// TODO: magic involving existing files
modIDs := make([]int, len(packMeta.Mods))
for i, v := range packMeta.Mods {
modIDs[i] = v.ID
}
fmt.Println("Querying Curse API...")
modInfos, err := getModInfoMultiple(modIDs)
if err != nil {
return cli.NewExitError(err, 1)
}
modInfosMap := make(map[int]modInfo)
for _, v := range modInfos {
modInfosMap[v.ID] = v
}
// TODO: multithreading????
for _, v := range packMeta.Mods {
// TODO: progress bar?
// TODO: batch requests?
modInfo, err := getModInfo(v.ID)
if err != nil {
// TODO: Fail more quietly?
return cli.NewExitError(err, 1)
modInfoValue, ok := modInfosMap[v.ID]
if !ok {
if len(v.File.FriendlyName) > 0 {
fmt.Printf("Failed to obtain mod information for \"%s\"\n", v.File.FriendlyName)
} else {
fmt.Printf("Failed to obtain mod information for \"%s\"\n", v.File.FileName)
}
continue
}
fmt.Println(v)
fmt.Println(modFileInfo(v.File))
err = createModFile(flags, modInfo, modFileInfo(v.File))
fmt.Printf("Imported \"%s\" successfully!\n", modInfoValue.Name)
err = createModFile(flags, modInfoValue, modFileInfo(v.File))
if err != nil {
return cli.NewExitError(err, 1)
}
}
// TODO: import existing files (config etc.)
return nil
}

View File

@@ -147,6 +147,38 @@ func getModInfo(modID int) (modInfo, error) {
return infoRes, nil
}
func getModInfoMultiple(modIDs []int) ([]modInfo, error) {
var infoRes []modInfo
client := &http.Client{}
modIDsData, err := json.Marshal(modIDs)
if err != nil {
return []modInfo{}, err
}
req, err := http.NewRequest("POST", "https://addons-ecs.forgesvc.net/api/v2/addon/", bytes.NewBuffer(modIDsData))
if err != nil {
return []modInfo{}, err
}
// TODO: make this configurable application-wide
req.Header.Set("User-Agent", "comp500/packwiz client")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return []modInfo{}, err
}
err = json.NewDecoder(resp.Body).Decode(&infoRes)
if err != nil && err != io.EOF {
return []modInfo{}, err
}
return infoRes, nil
}
const cfDateFormatString = "2006-01-02T15:04:05.999"
type cfDateFormat struct {