Files
epochcli/config.go
2025-06-09 21:38:39 -07:00

106 lines
2.2 KiB
Go

package main
import (
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/sqweek/dialog"
"os"
"path/filepath"
)
type Config struct {
WowDir string
LaunchCmd string
EnableLauncher bool
}
const (
configDirName = "epochcli"
configName = "config.toml"
)
var cfgPath string
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,
EnableLauncher: false,
}
cfgPath = filepath.Join(home, ".config", configDirName, configName)
_, 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)
if err != nil {
return nil, err
}
defer file.Close()
encoder := toml.NewEncoder(file)
if err = encoder.Encode(newConfig); err != nil {
return nil, err
}
fmt.Println("Created new config at ", cfgPath)
}
_, err := toml.DecodeFile(cfgPath, &newConfig)
if err != nil {
return nil, err
}
return &newConfig, nil
}