-y/--yes flag for non-interactive mode (fixes #46)

Also makes Modrinth auto-accept search results with only 1 result, to be
consistent with CurseForge.
This commit is contained in:
comp500
2022-08-25 04:47:18 +01:00
parent 503232a3fa
commit 11671421ac
5 changed files with 63 additions and 36 deletions

View File

@@ -1,10 +1,10 @@
package modrinth
import (
"bufio"
modrinthApi "codeberg.org/jmansfield/go-modrinth/modrinth"
"errors"
"fmt"
"github.com/packwiz/packwiz/cmdshared"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"os"
@@ -116,6 +116,20 @@ func installViaSearch(query string, pack core.Pack, index *core.Index) error {
return err
}
if len(results) == 0 {
return errors.New("no results found")
}
if viper.GetBool("non-interactive") || len(results) == 1 {
//Install the first mod
mod, err := mrDefaultClient.Projects.Get(*results[0].ProjectID)
if err != nil {
return err
}
return installMod(mod, pack, index)
}
//Create menu for the user to choose the correct mod
menu := wmenu.NewMenu("Choose a number:")
menu.Option("Cancel", nil, false, nil)
@@ -289,17 +303,9 @@ func installVersion(mod *modrinthApi.Project, version *modrinthApi.Version, pack
fmt.Println(*v.projectInfo.Title)
}
// TODO: --yes argument
fmt.Print("Would you like to add them? [Y/n]: ")
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return err
}
ansNormal := strings.ToLower(strings.TrimSpace(answer))
if !(len(ansNormal) > 0 && ansNormal[0] == 'n') {
if cmdshared.PromptYesNo("Would you like to add them? [Y/n]: ") {
for _, v := range depMetadata {
err = createFileMeta(v.projectInfo, v.versionInfo, v.fileInfo, index)
err := createFileMeta(v.projectInfo, v.versionInfo, v.fileInfo, index)
if err != nil {
return err
}