package main import ( "fmt" "os" ) type Command struct { Name string Description string Action CommandAction Arguments []string IsVarargs bool } type CommandAction func([]string, []string) (string, error) var commands = []Command{ { 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) { 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] if registerGameError := CreateGame(gameName, dataPath); registerGameError != nil { return "", registerGameError } return fmt.Sprintf("Created %s at %s", gameName, dataPath), nil }, }, { Name: "remove", Description: "Cleans and unregisters the given game name from management", Arguments: []string{"game name"}, IsVarargs: false, Action: func(requiredArguments []string, providedArguments []string) (string, error) { if len(providedArguments) != len(requiredArguments) { return "", fmt.Errorf("expected %s", requiredArguments[0]) } var dataPath = providedArguments[0] var gameName = providedArguments[1] if registerGameError := RemoveGame(gameName); registerGameError != nil { return "", registerGameError } return fmt.Sprintf("Removed %s at %s", gameName, dataPath), nil }, }, { Name: "deploy", Description: "Deploys all specified mod archives in order of listing", Arguments: []string{"game name", "archive paths"}, IsVarargs: true, 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 deployError = DeployGameMods(providedArguments[0], providedArguments[1:]) if deployError != nil { return "", deployError } return "deployed", nil }, }, { Name: "clean", Description: "Clean all deployed mods", Arguments: []string{"game name"}, IsVarargs: false, Action: func(requiredArguments []string, arguments []string) (string, error) { if len(arguments) != len(requiredArguments) { return "", fmt.Errorf("expected %s", requiredArguments[0]) } var cleanError = CleanGameMods(arguments[0]) if cleanError != nil { return "", cleanError } return "cleaned", nil }, }, } 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 { var response, actionError = command.Action(command.Arguments, os.Args[2:]) if actionError != nil { fmt.Fprintln(os.Stderr, actionError.Error()) os.Exit(1) } if len(response) != 0 { fmt.Println(response) } return } } fmt.Fprintf(os.Stderr, "unknown command: `%s`\n", commandName) }