package main import ( "fmt" "github.com/BurntSushi/toml" "os" "path" "path/filepath" "runtime" "strings" ) 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, err := os.UserHomeDir() if err != nil { return nil, fmt.Errorf("unable to determine home directory: %v", err) } cfgPath = filepath.Join(home, ".config", configDirName, configName) newConfig := Config{ WowDir: defaultWowPath, LaunchCmd: defaultLaunchCmd, EnableLauncher: false, } _, statErr := os.Stat(cfgPath) if rerun || os.IsNotExist(statErr) { fmt.Println("Enter the path to your Wow directory below:") var s string _, err = fmt.Scanln(&s) if err != nil { return nil, fmt.Errorf("unable to read input: %v", err) } 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) 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 { 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) if s != "" { newConfig.LaunchCmd = s } break } } 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) if err != nil { return nil, fmt.Errorf("unable to create config directory: %v", err) } file, err := os.Create(cfgPath) if err != nil { return nil, fmt.Errorf("unable to create config file: %v", err) } defer file.Close() encoder := toml.NewEncoder(file) if err = encoder.Encode(newConfig); err != nil { return nil, fmt.Errorf("unable to encode config file: %v", err) } fmt.Println("Created new config at ", cfgPath) } _, err = toml.DecodeFile(cfgPath, &newConfig) if err != nil { return nil, err } return &newConfig, nil }