mirror of
https://github.com/packwiz/packwiz.git
synced 2025-04-19 13:06:30 +02:00
-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:
parent
503232a3fa
commit
11671421ac
@ -58,7 +58,7 @@ func init() {
|
|||||||
defaultCacheDir, err := core.GetPackwizCache()
|
defaultCacheDir, err := core.GetPackwizCache()
|
||||||
cacheUsage := "The directory where packwiz will cache downloaded mods"
|
cacheUsage := "The directory where packwiz will cache downloaded mods"
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cacheUsage += "(default \""+defaultCacheDir+"\")"
|
cacheUsage += "(default \"" + defaultCacheDir + "\")"
|
||||||
}
|
}
|
||||||
rootCmd.PersistentFlags().String("cache", defaultCacheDir, cacheUsage)
|
rootCmd.PersistentFlags().String("cache", defaultCacheDir, cacheUsage)
|
||||||
_ = viper.BindPFlag("cache.directory", rootCmd.PersistentFlags().Lookup("cache"))
|
_ = viper.BindPFlag("cache.directory", rootCmd.PersistentFlags().Lookup("cache"))
|
||||||
@ -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.
|
||||||
|
@ -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
28
cmdshared/prompt.go
Normal 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
|
||||||
|
}
|
@ -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)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user