upgraded config creation

This commit is contained in:
2025-06-09 21:32:20 -07:00
parent bdf8067bdc
commit 72f7a1e163
6 changed files with 118 additions and 56 deletions

View File

@ -1,15 +1,18 @@
package main
import (
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/sqweek/dialog"
"os"
"path/filepath"
)
type Config struct {
WowDir string
LaunchCmd string
WowDir string
LaunchCmd string
EnableLauncher bool
}
const (
@ -19,20 +22,64 @@ const (
var cfgPath string
func setupConfig() (*Config, error) {
func setupConfig(rerun bool) (*Config, error) {
home := os.Getenv("HOME")
if home == "" {
return nil, fmt.Errorf("$HOME environment variable not set")
}
newConfig := Config{
WowDir: defaultWowPath,
LaunchCmd: defaultLaunchCmd,
WowDir: defaultWowPath,
LaunchCmd: defaultLaunchCmd,
EnableLauncher: false,
}
cfgPath = filepath.Join(home, ".config", configDirName, configName)
if _, statErr := os.Stat(cfgPath); os.IsNotExist(statErr) {
_, statErr := os.Stat(cfgPath)
if rerun || os.IsNotExist(statErr) {
fmt.Println("Press any key to open a file window and select your wow directory")
var r rune
_, _ = fmt.Scanf("%c", &r)
var err error
newConfig.WowDir, err = dialog.Directory().Title("Select your wow directory").Browse()
if err != nil {
if errors.Is(err, dialog.ErrCancelled) {
return nil, fmt.Errorf("cancelled dialog box, exiting")
}
return nil, err
}
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): ")
var s string
_, err = fmt.Scanf("%s", &s)
if err != nil {
return nil, err
}
if s == "y" || s == "Y" {
newConfig.EnableLauncher = true
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, err
}
if s != "" {
newConfig.LaunchCmd = s
}
break
}
if s == "n" || s == "N" {
break
}
fmt.Println("Please enter a valid value of either 'y' or 'n'")
}
os.MkdirAll(filepath.Join(home, ".config", configDirName), 0755)
file, err := os.Create(cfgPath)
@ -46,8 +93,7 @@ func setupConfig() (*Config, error) {
return nil, err
}
fmt.Printf("Created new config at %s, edit it before running the launcher again\n", cfgPath)
os.Exit(0)
fmt.Println("Created new config at ", cfgPath)
}
_, err := toml.DecodeFile(cfgPath, &newConfig)