diff --git a/cmd/root.go b/cmd/root.go index 416d6bc..9f92158 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,7 +58,7 @@ func init() { defaultCacheDir, err := core.GetPackwizCache() cacheUsage := "The directory where packwiz will cache downloaded mods" if err == nil { - cacheUsage += "(default \""+defaultCacheDir+"\")" + cacheUsage += "(default \"" + defaultCacheDir + "\")" } rootCmd.PersistentFlags().String("cache", defaultCacheDir, cacheUsage) _ = viper.BindPFlag("cache.directory", rootCmd.PersistentFlags().Lookup("cache")) @@ -70,6 +70,10 @@ func init() { } file = filepath.Join(file, ".packwiz.toml") 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. diff --git a/cmd/update.go b/cmd/update.go index b9ecf52..08f7f84 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -1,14 +1,12 @@ package cmd import ( - "bufio" "fmt" - "github.com/spf13/viper" - "os" - "strings" - + "github.com/packwiz/packwiz/cmdshared" "github.com/packwiz/packwiz/core" "github.com/spf13/cobra" + "github.com/spf13/viper" + "os" ) // updateCmd represents the update command @@ -101,15 +99,7 @@ var updateCmd = &cobra.Command{ return } - fmt.Print("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' { + if !cmdshared.PromptYesNo("Do you want to update? [Y/n]: ") { fmt.Println("Cancelled!") return } diff --git a/cmdshared/prompt.go b/cmdshared/prompt.go new file mode 100644 index 0000000..d7a32e8 --- /dev/null +++ b/cmdshared/prompt.go @@ -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 +} diff --git a/curseforge/install.go b/curseforge/install.go index 9c4bc95..eb89cc0 100644 --- a/curseforge/install.go +++ b/curseforge/install.go @@ -1,9 +1,9 @@ package curseforge import ( - "bufio" "errors" "fmt" + "github.com/packwiz/packwiz/cmdshared" "github.com/sahilm/fuzzy" "github.com/spf13/viper" "golang.org/x/exp/slices" @@ -212,15 +212,7 @@ var installCmd = &cobra.Command{ fmt.Println(v.Name) } - fmt.Print("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') { + if cmdshared.PromptYesNo("Would you like to add them? [Y/n]: ") { for _, v := range depsInstallable { err = createModFile(v.modInfo, v.fileInfo, &index, false) if err != nil { @@ -361,6 +353,13 @@ func searchCurseforgeInternal(searchTerm string, isSlug bool, game string, categ // Fuzzy search on results list 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.Option("Cancel", nil, false, nil) diff --git a/modrinth/install.go b/modrinth/install.go index 1578f41..06a386a 100644 --- a/modrinth/install.go +++ b/modrinth/install.go @@ -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 }