package main import ( "fmt" "github.com/BurntSushi/toml" "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("Enter the path to your Wow directory below:") _, err := fmt.Scanln(&newConfig.WowDir) if err != nil { 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 }