esp-modman/main.go

309 lines
6.8 KiB
Go
Raw Normal View History

2022-12-02 02:24:13 +01:00
package main
import (
2022-12-03 18:56:36 +01:00
"encoding/json"
2022-12-02 02:24:13 +01:00
"fmt"
"os"
2022-12-21 00:28:30 +01:00
"strings"
"gopkg.in/yaml.v3"
2022-12-02 02:24:13 +01:00
)
type Command struct {
Name string
Description string
2022-12-05 14:13:58 +01:00
Action CommandAction
2022-12-02 02:24:13 +01:00
Arguments []string
IsVarargs bool
}
2022-12-05 14:13:58 +01:00
type CommandAction func([]string, []string) (string, error)
2022-12-02 02:24:13 +01:00
var commands = []Command{
{
Name: "install",
Description: "Install one or more mod archives into modman",
2022-12-03 18:56:36 +01:00
Arguments: []string{"game name", "archive path"},
2022-12-02 02:24:13 +01:00
IsVarargs: true,
2022-12-05 14:13:58 +01:00
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
var argumentCount = len(providedArguments)
2022-12-02 02:24:13 +01:00
2022-12-05 14:13:58 +01:00
if argumentCount < len(requiredArguments) {
2022-12-21 00:28:30 +01:00
return "", fmt.Errorf("expected %s folowed by at least one %ss",
requiredArguments[0], requiredArguments[1])
2022-12-03 18:56:36 +01:00
}
2022-12-02 02:24:13 +01:00
2022-12-22 00:32:30 +01:00
var game, gameOpenError = OpenGame(providedArguments[0])
if gameOpenError != nil {
return "", gameOpenError
}
defer func() {
if closeError := game.Close(); closeError != nil {
panic(closeError)
2022-12-03 19:27:53 +01:00
}
2022-12-22 00:32:30 +01:00
}()
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
for _, archivePath := range providedArguments[1:] {
if installError := game.InstallMod(archivePath); installError != nil {
return "", installError
}
2022-12-02 02:24:13 +01:00
}
2022-12-05 14:13:58 +01:00
return "mods installed", nil
2022-12-02 02:24:13 +01:00
},
},
{
Name: "remove",
Description: "Remove one or more mods from modman",
2022-12-03 18:56:36 +01:00
Arguments: []string{"game name", "mod name"},
2022-12-02 02:24:13 +01:00
IsVarargs: true,
2022-12-05 14:13:58 +01:00
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])
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
var game, gameOpenError = OpenGame(providedArguments[0])
if gameOpenError != nil {
return "", gameOpenError
}
defer func() {
if closeError := game.Close(); closeError != nil {
panic(closeError)
}
}()
if removeModsError := game.RemoveMods(providedArguments[1:]); removeModsError != nil {
return "", removeModsError
2022-12-03 18:56:36 +01:00
}
2022-12-05 14:13:58 +01:00
return "removed mods", nil
2022-12-02 02:24:13 +01:00
},
},
{
Name: "rename",
Description: "Rename a mod within modman",
2022-12-03 18:56:36 +01:00
Arguments: []string{"game name", "mod name", "new name"},
2022-12-02 02:24:13 +01:00
IsVarargs: false,
2022-12-05 14:13:58 +01:00
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
if len(providedArguments) != len(requiredArguments) {
return "", fmt.Errorf("expected %s followed by %s and %s",
requiredArguments[0], requiredArguments[1], requiredArguments[2])
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
var game, gameOpenError = OpenGame(providedArguments[0])
if gameOpenError != nil {
return "", gameOpenError
}
2022-12-05 14:13:58 +01:00
2022-12-22 00:32:30 +01:00
defer func() {
if closeError := game.Close(); closeError != nil {
panic(closeError)
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
}()
if renameError := game.RenameMod(
providedArguments[1], providedArguments[2]); renameError != nil {
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
return "", renameError
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
return "renamed", nil
2022-12-02 02:24:13 +01:00
},
},
{
2022-12-03 18:56:36 +01:00
Name: "manifest",
Description: "Retrieve a manifest of all installed mods",
Arguments: []string{"game name", "format"},
2022-12-02 02:24:13 +01:00
IsVarargs: false,
2022-12-05 14:13:58 +01:00
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
if len(providedArguments) != len(requiredArguments) {
return "", fmt.Errorf("expected %s followed by %s",
requiredArguments[0], requiredArguments[1])
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
var game, gameOpenError = OpenGame(providedArguments[0])
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
if gameOpenError != nil {
return "", gameOpenError
}
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
defer func() {
if closeError := game.Close(); closeError != nil {
panic(closeError)
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
}()
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
var format = providedArguments[1]
var formatter, formatterExists = formatters[format]
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
if !(formatterExists) {
return "", fmt.Errorf("unsupported format: `%s`", format)
}
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
var manifestBuilder = strings.Builder{}
2022-12-03 18:56:36 +01:00
2022-12-22 00:32:30 +01:00
if formatError := formatter(&manifestBuilder, game.Mods); formatError != nil {
return "", formatError
2022-12-03 18:56:36 +01:00
}
2022-12-22 00:32:30 +01:00
return manifestBuilder.String(), nil
2022-12-02 02:24:13 +01:00
},
},
{
Name: "deploy",
2022-12-21 00:28:30 +01:00
Description: "Deploy all specified mods in order of listing",
2022-12-05 14:13:58 +01:00
Arguments: []string{"game name", "mod name"},
IsVarargs: true,
Action: func(requiredArguments []string, arguments []string) (string, error) {
2022-12-21 00:28:30 +01:00
if len(arguments) != len(requiredArguments) {
2022-12-05 14:13:58 +01:00
return "", fmt.Errorf("expected %s followed by one or more %ss",
requiredArguments[0], requiredArguments[1])
}
2022-12-22 00:32:30 +01:00
var game, gameOpenError = OpenGame(arguments[0])
2022-12-21 00:28:30 +01:00
2022-12-22 00:32:30 +01:00
if gameOpenError != nil {
return "", gameOpenError
}
defer func() {
if closeError := game.Close(); closeError != nil {
panic(closeError)
2022-12-05 14:13:58 +01:00
}
2022-12-22 00:32:30 +01:00
}()
2022-12-05 14:13:58 +01:00
2022-12-22 00:32:30 +01:00
if cleanError := game.CleanDeployedMods(); cleanError != nil {
return "", cleanError
}
for _, modName := range arguments[1:] {
if deployError := game.DeployMod(modName); deployError != nil {
return "", deployError
}
2022-12-05 14:13:58 +01:00
}
2022-12-21 00:28:30 +01:00
return "deployed", nil
2022-12-05 14:13:58 +01:00
},
},
{
2022-12-21 00:28:30 +01:00
Name: "clean",
Description: "Clean all deployed mods",
Arguments: []string{"game name"},
IsVarargs: false,
2022-12-05 14:13:58 +01:00
Action: func(requiredArguments []string, arguments []string) (string, error) {
2022-12-21 00:33:20 +01:00
if len(arguments) != len(requiredArguments) {
return "", fmt.Errorf("expected %s", requiredArguments[0])
}
2022-12-22 00:32:30 +01:00
var game, gameOpenError = OpenGame(arguments[0])
if gameOpenError != nil {
return "", gameOpenError
}
defer func() {
if closeError := game.Close(); closeError != nil {
panic(closeError)
}
}()
if cleanDeployedModsError := game.CleanDeployedMods(); cleanDeployedModsError != nil {
return "", cleanDeployedModsError
2022-12-21 00:33:20 +01:00
}
2022-12-21 00:28:30 +01:00
return "cleaned", nil
2022-12-02 02:24:13 +01:00
},
},
}
2022-12-21 00:28:30 +01:00
var formatters = map[string]func(*strings.Builder, any) error{
"json": func(builder *strings.Builder, data any) error {
var encoder = json.NewEncoder(builder)
2022-12-03 19:27:53 +01:00
2022-12-21 00:28:30 +01:00
if encodeError := encoder.Encode(data); encodeError != nil {
return encodeError
2022-12-03 19:27:53 +01:00
}
return nil
},
2022-12-21 00:28:30 +01:00
"yaml": func(builder *strings.Builder, data any) error {
var encoder = yaml.NewEncoder(builder)
2022-12-03 18:56:36 +01:00
2022-12-21 00:28:30 +01:00
encoder.SetIndent(2)
if encodeError := encoder.Encode(data); encodeError != nil {
return encodeError
2022-12-03 18:56:36 +01:00
}
2022-12-21 00:28:30 +01:00
return nil
2022-12-03 18:56:36 +01:00
},
}
2022-12-02 02:24:13 +01:00
func main() {
var argCount = len(os.Args)
if argCount == 1 {
fmt.Println("Modman v0.1")
fmt.Println("Enter one of the following commands following modman:")
for i := range commands {
var command = commands[i]
fmt.Print("\t", command.Name)
for j := range command.Arguments {
fmt.Print(" [", command.Arguments[j], "]")
}
if command.IsVarargs {
fmt.Println("...")
} else {
fmt.Println()
}
}
return
}
var commandName = os.Args[1]
for i := range commands {
var command = commands[i]
if command.Name == commandName {
2022-12-05 14:13:58 +01:00
var response, actionError = command.Action(command.Arguments, os.Args[2:])
2022-12-02 02:24:13 +01:00
2022-12-03 18:56:36 +01:00
if actionError != nil {
fmt.Fprintln(os.Stderr, actionError.Error())
2022-12-02 02:24:13 +01:00
os.Exit(1)
}
2022-12-03 18:56:36 +01:00
if len(response) != 0 {
fmt.Println(response)
}
2022-12-02 02:24:13 +01:00
return
}
}
2022-12-03 18:56:36 +01:00
fmt.Fprintf(os.Stderr, "unknown command: `%s`\n", commandName)
2022-12-02 02:24:13 +01:00
}