-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

@ -70,6 +70,10 @@ func init() {
} }
file = filepath.Join(file, ".packwiz.toml") file = filepath.Join(file, ".packwiz.toml")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "The config file to use (default \""+file+"\")") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "The config file to use (default \""+file+"\")")
var nonInteractive bool
rootCmd.PersistentFlags().BoolVarP(&nonInteractive, "yes", "y", false, "Accept all prompts with the default or \"yes\" option (non-interactive mode) - may pick unwanted options in search results")
_ = viper.BindPFlag("non-interactive", rootCmd.PersistentFlags().Lookup("yes"))
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.

View File

@ -1,14 +1,12 @@
package cmd package cmd
import ( import (
"bufio"
"fmt" "fmt"
"github.com/spf13/viper" "github.com/packwiz/packwiz/cmdshared"
"os"
"strings"
"github.com/packwiz/packwiz/core" "github.com/packwiz/packwiz/core"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
) )
// updateCmd represents the update command // updateCmd represents the update command
@ -101,15 +99,7 @@ var updateCmd = &cobra.Command{
return return
} }
fmt.Print("Do you want to update? [Y/n]: ") if !cmdshared.PromptYesNo("Do you want to update? [Y/n]: ") {
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ansNormal := strings.ToLower(strings.TrimSpace(answer))
if len(ansNormal) > 0 && ansNormal[0] == 'n' {
fmt.Println("Cancelled!") fmt.Println("Cancelled!")
return return
} }

28
cmdshared/prompt.go Normal file
View File

@ -0,0 +1,28 @@
package cmdshared
import (
"bufio"
"fmt"
"github.com/spf13/viper"
"os"
"strings"
)
func PromptYesNo(prompt string) bool {
fmt.Print(prompt)
if viper.GetBool("non-interactive") {
fmt.Println("Y (non-interactive mode)")
return true
}
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
fmt.Printf("Failed to prompt user: %v\n", err)
os.Exit(1)
}
ansNormal := strings.ToLower(strings.TrimSpace(answer))
if len(ansNormal) > 0 && ansNormal[0] == 'n' {
return false
}
return true
}

View File

@ -1,9 +1,9 @@
package curseforge package curseforge
import ( import (
"bufio"
"errors" "errors"
"fmt" "fmt"
"github.com/packwiz/packwiz/cmdshared"
"github.com/sahilm/fuzzy" "github.com/sahilm/fuzzy"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
@ -212,15 +212,7 @@ var installCmd = &cobra.Command{
fmt.Println(v.Name) fmt.Println(v.Name)
} }
fmt.Print("Would you like to add them? [Y/n]: ") if cmdshared.PromptYesNo("Would you like to add them? [Y/n]: ") {
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ansNormal := strings.ToLower(strings.TrimSpace(answer))
if !(len(ansNormal) > 0 && ansNormal[0] == 'n') {
for _, v := range depsInstallable { for _, v := range depsInstallable {
err = createModFile(v.modInfo, v.fileInfo, &index, false) err = createModFile(v.modInfo, v.fileInfo, &index, false)
if err != nil { if err != nil {
@ -361,6 +353,13 @@ func searchCurseforgeInternal(searchTerm string, isSlug bool, game string, categ
// Fuzzy search on results list // Fuzzy search on results list
fuzzySearchResults := fuzzy.FindFrom(searchTerm, modResultsList(results)) fuzzySearchResults := fuzzy.FindFrom(searchTerm, modResultsList(results))
if viper.GetBool("non-interactive") {
if len(fuzzySearchResults) > 0 {
return false, results[fuzzySearchResults[0].Index]
}
return false, results[0]
}
menu := wmenu.NewMenu("Choose a number:") menu := wmenu.NewMenu("Choose a number:")
menu.Option("Cancel", nil, false, nil) menu.Option("Cancel", nil, false, nil)

View File

@ -1,10 +1,10 @@
package modrinth package modrinth
import ( import (
"bufio"
modrinthApi "codeberg.org/jmansfield/go-modrinth/modrinth" modrinthApi "codeberg.org/jmansfield/go-modrinth/modrinth"
"errors" "errors"
"fmt" "fmt"
"github.com/packwiz/packwiz/cmdshared"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"os" "os"
@ -116,6 +116,20 @@ func installViaSearch(query string, pack core.Pack, index *core.Index) error {
return err 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 //Create menu for the user to choose the correct mod
menu := wmenu.NewMenu("Choose a number:") menu := wmenu.NewMenu("Choose a number:")
menu.Option("Cancel", nil, false, nil) menu.Option("Cancel", nil, false, nil)
@ -289,17 +303,9 @@ func installVersion(mod *modrinthApi.Project, version *modrinthApi.Version, pack
fmt.Println(*v.projectInfo.Title) fmt.Println(*v.projectInfo.Title)
} }
// TODO: --yes argument if cmdshared.PromptYesNo("Would you like to add them? [Y/n]: ") {
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') {
for _, v := range depMetadata { 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 { if err != nil {
return err return err
} }