Add command for registering data paths of installed games

This commit is contained in:
kayomn 2022-12-22 00:34:41 +00:00
parent 85a941de81
commit 11b66395b5
2 changed files with 82 additions and 7 deletions

35
main.go
View File

@ -27,9 +27,7 @@ var commands = []Command{
IsVarargs: true,
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
var argumentCount = len(providedArguments)
if argumentCount < len(requiredArguments) {
if len(providedArguments) < len(requiredArguments) {
return "", fmt.Errorf("expected %s folowed by at least one %ss",
requiredArguments[0], requiredArguments[1])
}
@ -56,6 +54,29 @@ var commands = []Command{
},
},
{
Name: "register",
Description: "Registers the data directory path of game for management under the given name",
Arguments: []string{"data path", "game name"},
IsVarargs: false,
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
if len(providedArguments) < len(requiredArguments) {
return "", fmt.Errorf("expected %s folowed by at least one %ss",
requiredArguments[0], requiredArguments[1])
}
var dataPath = providedArguments[0]
var gameName = providedArguments[1]
if registerGameError := RegisterGame(gameName, dataPath); registerGameError != nil {
return "", registerGameError
}
return fmt.Sprintf("Registered %s at %s", gameName, dataPath), nil
},
},
{
Name: "remove",
Description: "Remove one or more mods from modman",
@ -169,13 +190,13 @@ var commands = []Command{
Arguments: []string{"game name", "mod name"},
IsVarargs: true,
Action: func(requiredArguments []string, arguments []string) (string, error) {
if len(arguments) != len(requiredArguments) {
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
if len(providedArguments) != len(requiredArguments) {
return "", fmt.Errorf("expected %s followed by one or more %ss",
requiredArguments[0], requiredArguments[1])
}
var game, gameOpenError = OpenGame(arguments[0])
var game, gameOpenError = OpenGame(providedArguments[0])
if gameOpenError != nil {
return "", gameOpenError
@ -191,7 +212,7 @@ var commands = []Command{
return "", cleanError
}
for _, modName := range arguments[1:] {
for _, modName := range providedArguments[1:] {
if deployError := game.DeployMod(modName); deployError != nil {
return "", deployError
}

View File

@ -461,6 +461,60 @@ func (game *Game) RemoveMods(names []string) error {
return nil
}
func RegisterGame(name string, dataPath string) error {
var configPath, configPathError = os.UserConfigDir()
if configPathError != nil {
return configPathError
}
var gamesPath = filepath.Join(configPath, "modman", "games.ini")
var gamesEntries = []ini.Entry{{
Section: name,
Key: "path",
Value: dataPath,
}}
if gamesFile, gamesFileError := os.Open(gamesPath); gamesFileError == nil {
var gamesParser = ini.NewParser(gamesFile)
for entry := gamesParser.Parse(); !(gamesParser.IsEnd()); entry = gamesParser.Parse() {
gamesEntries = append(gamesEntries, entry)
}
if err := gamesFile.Close(); err != nil {
return err
}
if err := gamesParser.Err(); err != nil {
return err
}
} else if !(os.IsNotExist(gamesFileError)) {
return gamesFileError
}
if gamesFile, gamesFileError := os.Create(gamesPath); gamesFileError == nil {
var writeError = ini.Write(gamesFile, gamesEntries)
if err := gamesFile.Sync(); err != nil {
return err
}
if err := gamesFile.Close(); err != nil {
return err
}
if writeError != nil {
return writeError
}
} else {
return gamesFileError
}
return nil
}
func (game *Game) RenameMod(modName string, newName string) error {
if _, exists := game.Mods[modName]; !(exists) {
return fmt.Errorf("no mod with that name exists")