Add functionality for listing game associations

This commit is contained in:
kayomn 2023-01-02 14:41:32 +00:00
parent 4d0b24c46a
commit d44276a56e
2 changed files with 61 additions and 0 deletions

31
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"
)
type Command struct {
@ -103,6 +104,36 @@ var commands = []Command{
return "cleaned", nil
},
},
{
Name: "list",
Description: "List all known game associations by name followed by data path in a newline-separated list",
Arguments: []string{},
IsVarargs: false,
Action: func(requiredArguments []string, arguments []string) (string, error) {
if len(arguments) != 0 {
return "", fmt.Errorf("expected zero arguments")
}
var gameDataPaths, loadError = LoadGames()
if loadError != nil {
return "", loadError
}
var stringBuilder = strings.Builder{}
for game, dataPath := range gameDataPaths {
stringBuilder.WriteString(game)
stringBuilder.WriteString(" \"")
stringBuilder.WriteString(dataPath)
stringBuilder.WriteByte('"')
}
return stringBuilder.String(), nil
},
},
}
func main() {

View File

@ -317,6 +317,36 @@ func DeployGameMods(gameName string, modArchivePaths []string) error {
return nil
}
func LoadGames() (map[string]string, error) {
var gameDataPaths = make(map[string]string)
if iniFile, openError := os.Open(gamesIniPath); openError == nil {
defer func() {
if closeError := iniFile.Close(); closeError != nil {
panic(closeError)
}
}()
var parser = ini.NewParser(iniFile)
for entry := parser.Parse(); !(parser.IsEnd()); entry = parser.Parse() {
var section = parser.Section()
if entry.Key == "path" {
gameDataPaths[section] = entry.Value
}
}
if parseError := parser.Err(); parseError != nil {
return gameDataPaths, parseError
}
} else if !(os.IsNotExist(openError)) {
return gameDataPaths, openError
}
return gameDataPaths, nil
}
func RemoveGame(gameName string) error {
if cleanError := CleanGameMods(gameName); cleanError != nil {
return cleanError