From 6428dc557da05ce89734a88b68e8be51613551bb Mon Sep 17 00:00:00 2001 From: darius Date: Fri, 24 May 2024 23:10:45 +0200 Subject: [PATCH] game server register and status added --- commands/games/games.go | 28 ++++++++ commands/games/subcommands.go | 127 ++++++++++++++++++++++++++++++++++ config.toml | 6 +- main.go | 2 + services/readConfig.go | 3 + 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 commands/games/games.go create mode 100644 commands/games/subcommands.go diff --git a/commands/games/games.go b/commands/games/games.go new file mode 100644 index 0000000..1422746 --- /dev/null +++ b/commands/games/games.go @@ -0,0 +1,28 @@ +package games + +import ( + "github.com/DariusKlein/kleinCommand/types" + "github.com/urfave/cli/v2" +) + +var config types.Config + +func Command(conf types.Config) *cli.Command { + config = conf + return &cli.Command{ + Name: "games", + Usage: "manage game servers", + Aliases: []string{"gs"}, + Action: action, + Subcommands: subcommands(), + HideHelpCommand: true, + } +} + +func action(c *cli.Context) error { + err := cli.ShowSubcommandHelp(c) + if err != nil { + return err + } + return nil +} diff --git a/commands/games/subcommands.go b/commands/games/subcommands.go new file mode 100644 index 0000000..0e963b1 --- /dev/null +++ b/commands/games/subcommands.go @@ -0,0 +1,127 @@ +package games + +import ( + "fmt" + "github.com/BurntSushi/toml" + "github.com/DariusKlein/kleinCommand/services" + "github.com/DariusKlein/kleinCommand/types" + "github.com/urfave/cli/v2" + "os" + "os/exec" + "strconv" + "strings" +) + +func subcommands() []*cli.Command { + return []*cli.Command{ + { + Name: "register", + Usage: "Register game server", + Action: registerAction, + Flags: registerFlags(), + }, + { + Name: "list", + Usage: "List of game servers", + Aliases: []string{"l"}, + Action: listAction, + Flags: []cli.Flag{}, + }, + { + Name: "status", + Usage: "Get status of game servers", + Aliases: []string{"s"}, + Action: statusAction, + Flags: []cli.Flag{}, + }, + } +} + +func registerAction(c *cli.Context) error { + + for _, games := range config.CustomCommands.Games { + if games.Name == c.String("name") { + return fmt.Errorf("game '%s' already exists", games.Name) + } + } + + config.CustomCommands.Games = append(config.CustomCommands.Games, types.Games{ + Name: c.String("name"), + StatusCommand: c.String("status"), + StartCommand: c.String("start_command"), + StopCommand: c.String("stop_command"), + }) + + configString, err := toml.Marshal(config) + if err != nil { + return err + } + + _, configPath, err := services.GetConfigPath() + if err != nil { + fmt.Println(err) + } + + err = os.WriteFile(configPath, configString, 0644) + if err != nil { + return err + } + return nil +} + +func registerFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + Usage: "Game server name", + Required: true, + }, + &cli.StringFlag{ + Name: "status_command", + Aliases: []string{"status"}, + Usage: "Game server status command", + Required: true, + }, + &cli.StringFlag{ + Name: "start_command", + Aliases: []string{"start"}, + Usage: "Game server start command", + Required: true, + }, + &cli.StringFlag{ + Name: "stop_command", + Aliases: []string{"stop"}, + Usage: "Game server stop command", + Required: true, + }, + } +} + +func listAction(c *cli.Context) error { + fmt.Println("List of game servers") + for i, game := range config.CustomCommands.Games { + fmt.Println( + strconv.Itoa(i+1) + ") \n" + + "\tName: " + game.Name + + "\n\tStatus command: " + game.StatusCommand + + "\n\tStart command: " + game.StartCommand + + "\n\tStop command: " + game.StopCommand) + } + return nil +} + +func statusAction(c *cli.Context) error { + fmt.Println("List of game servers") + for _, game := range config.CustomCommands.Games { + command := strings.Fields(game.StatusCommand) + cmd := exec.Command(command[0], command[1:]...) + stdout, err := cmd.Output() + if err != nil { + return err + } + fmt.Println(game.Name + ": " + string(stdout)) + } + + return nil +} diff --git a/config.toml b/config.toml index 05dd5ad..d9dc0e0 100644 --- a/config.toml +++ b/config.toml @@ -14,6 +14,6 @@ int1=1 # Generate commands with kleinCommand or configure here [[custom_commands.games]] name = "example" -status_command="example" -start_command="example" -stop_command="example" \ No newline at end of file +status_command="docker ps --format '{{.Names}}'" +start_command="echo start" +stop_command="echo stop" \ No newline at end of file diff --git a/main.go b/main.go index 85b61a8..9ebbc89 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "github.com/DariusKlein/kleinCommand/commands/boom" "github.com/DariusKlein/kleinCommand/commands/bubbleTeaTest" "github.com/DariusKlein/kleinCommand/commands/config" + "github.com/DariusKlein/kleinCommand/commands/games" "github.com/DariusKlein/kleinCommand/commands/template" "github.com/DariusKlein/kleinCommand/commands/welcome" "github.com/DariusKlein/kleinCommand/services" @@ -34,6 +35,7 @@ func main() { boom.Command(Config), bubbleTeaTest.Command(Config), config.Command(Config), + games.Command(Config), }, } diff --git a/services/readConfig.go b/services/readConfig.go index ddd5778..3f6a915 100644 --- a/services/readConfig.go +++ b/services/readConfig.go @@ -21,6 +21,9 @@ func ReadConfig() (types.Config, error) { } _, err = toml.Decode(string(file), &config) + if err != nil { + return config, err + } return config, nil }