diff --git a/config.go b/config.go index fbdce3a..8c900a9 100644 --- a/config.go +++ b/config.go @@ -46,52 +46,37 @@ func setupConfig(rerun bool) (*Config, error) { } newConfig.WowDir = strings.TrimSpace(s) - for { - fmt.Printf("Do you want to use epochcli to launch Wow? Select No if you plan on using a launcher tool like Lutris (y/n): ") - _, err = fmt.Scanf("%s", &s) - if err != nil { - return nil, fmt.Errorf("unable to read input: %v", err) - } - s = strings.TrimSpace(s) + p, err := promptYesNo(fmt.Sprintf("Do you want to use epochcli to launch Wow? Select No if you plan on using a launcher tool like Lutris (y/n): ")) + if err != nil { + return nil, err + } + if p { + newConfig.EnableLauncher = true - if s == "y" || s == "Y" { - newConfig.EnableLauncher = true + if runtime.GOOS == "windows" { + newConfig.LaunchCmd = path.Join(newConfig.WowDir, "Project-Epoch.exe") + exePath, err := os.Executable() + if err != nil { + return nil, fmt.Errorf("unable to create desktop shortcut: %v", err) + } + err = makeLink(exePath, path.Join(home, "Desktop", "Project-Epoch.lnk")) + if err != nil { + return nil, fmt.Errorf("unable to create desktop shortcut: %v", err) + } + } else { + fmt.Println("Enter your launch command to start Wow below. If you would rather configure this later in the configuration file, just press Enter") + fmt.Printf("> ") - if runtime.GOOS == "windows" { - newConfig.LaunchCmd = path.Join(newConfig.WowDir, "Project-Epoch.exe") - exePath, err := os.Executable() - if err != nil { - fmt.Println("unable to create desktop shortcut: ", err) - break - } - err = makeLink(exePath, path.Join(home, "Desktop", "Project-Epoch.lnk")) - if err != nil { - fmt.Println("unable to create desktop shortcut: ", err) - } - break - } else { - fmt.Println("Enter your launch command to start Wow below. If you would rather configure this later in the configuration file, just press Enter") - fmt.Printf("> ") + _, err = fmt.Scanf("%s", &s) + if err != nil { + return nil, fmt.Errorf("unable to read input: %v", err) + } + s = strings.TrimSpace(s) - _, err = fmt.Scanf("%s", &s) - if err != nil { - return nil, fmt.Errorf("unable to read input: %v", err) - } - s = strings.TrimSpace(s) - - if s != "" { - newConfig.LaunchCmd = s - } - - break + if s != "" { + newConfig.LaunchCmd = s } } - - if s == "n" || s == "N" { - break - } - - fmt.Println("Please enter a valid value of either 'y' or 'n'") } err = os.MkdirAll(filepath.Join(home, ".config", configDirName), 0755) @@ -120,3 +105,24 @@ func setupConfig(rerun bool) (*Config, error) { return &newConfig, nil } + +func promptYesNo(prompt string) (bool, error) { + var s string + for { + fmt.Print(prompt) + _, err := fmt.Scanf("%s", &s) + if err != nil { + return false, fmt.Errorf("unable to read input: %v", err) + } + s := strings.TrimSpace(s) + + if s == "y" || s == "Y" { + return true, nil + } + + if s == "n" || s == "N" { + return false, nil + } + fmt.Println("Please enter a valid value of either 'y' or 'n'") + } +}