esp-modman/main.go

157 lines
3.5 KiB
Go
Raw Normal View History

2022-12-02 02:24:13 +01:00
package main
import (
"fmt"
"os"
)
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{
{
2022-12-25 18:31:35 +01:00
Name: "create",
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) {
2022-12-25 18:31:35 +01:00
if len(providedArguments) != len(requiredArguments) {
return "", fmt.Errorf("expected %s folowed by %s",
requiredArguments[0], requiredArguments[1])
}
var dataPath = providedArguments[0]
var gameName = providedArguments[1]
2022-12-25 18:31:35 +01:00
if registerGameError := CreateGame(gameName, dataPath); registerGameError != nil {
return "", registerGameError
}
2022-12-25 18:31:35 +01:00
return fmt.Sprintf("Created %s at %s", gameName, dataPath), nil
},
},
2022-12-02 02:24:13 +01:00
{
Name: "remove",
2022-12-25 18:31:35 +01:00
Description: "Cleans and unregisters the given game name from management",
Arguments: []string{"game 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) {
2022-12-25 18:31:35 +01:00
return "", fmt.Errorf("expected %s", requiredArguments[0])
2022-12-03 18:56:36 +01:00
}
2022-12-25 18:31:35 +01:00
var dataPath = providedArguments[0]
var gameName = providedArguments[1]
2022-12-03 18:56:36 +01:00
2022-12-25 18:31:35 +01:00
if registerGameError := RemoveGame(gameName); registerGameError != nil {
return "", registerGameError
2022-12-03 18:56:36 +01:00
}
2022-12-25 18:31:35 +01:00
return fmt.Sprintf("Removed %s at %s", gameName, dataPath), nil
2022-12-02 02:24:13 +01:00
},
},
{
Name: "deploy",
2022-12-25 18:31:35 +01:00
Description: "Deploys all specified mod archives in order of listing",
Arguments: []string{"game name", "archive paths"},
2022-12-05 14:13:58 +01:00
IsVarargs: true,
Action: func(requiredArguments []string, providedArguments []string) (string, error) {
if len(providedArguments) < 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-25 18:31:35 +01:00
var deployError = DeployGameMods(providedArguments[0], providedArguments[1:])
if 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-25 18:31:35 +01:00
var cleanError = CleanGameMods(arguments[0])
if cleanError != nil {
return "", cleanError
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
},
},
}
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
}