-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

@@ -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.

View File

@@ -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
}