game server register and status added
This commit is contained in:
parent
9c1460fa1f
commit
6428dc557d
28
commands/games/games.go
Normal file
28
commands/games/games.go
Normal file
@ -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
|
||||||
|
}
|
||||||
127
commands/games/subcommands.go
Normal file
127
commands/games/subcommands.go
Normal file
@ -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
|
||||||
|
}
|
||||||
@ -14,6 +14,6 @@ int1=1
|
|||||||
# Generate commands with kleinCommand or configure here
|
# Generate commands with kleinCommand or configure here
|
||||||
[[custom_commands.games]]
|
[[custom_commands.games]]
|
||||||
name = "example"
|
name = "example"
|
||||||
status_command="example"
|
status_command="docker ps --format '{{.Names}}'"
|
||||||
start_command="example"
|
start_command="echo start"
|
||||||
stop_command="example"
|
stop_command="echo stop"
|
||||||
2
main.go
2
main.go
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/DariusKlein/kleinCommand/commands/boom"
|
"github.com/DariusKlein/kleinCommand/commands/boom"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/bubbleTeaTest"
|
"github.com/DariusKlein/kleinCommand/commands/bubbleTeaTest"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/config"
|
"github.com/DariusKlein/kleinCommand/commands/config"
|
||||||
|
"github.com/DariusKlein/kleinCommand/commands/games"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/template"
|
"github.com/DariusKlein/kleinCommand/commands/template"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/welcome"
|
"github.com/DariusKlein/kleinCommand/commands/welcome"
|
||||||
"github.com/DariusKlein/kleinCommand/services"
|
"github.com/DariusKlein/kleinCommand/services"
|
||||||
@ -34,6 +35,7 @@ func main() {
|
|||||||
boom.Command(Config),
|
boom.Command(Config),
|
||||||
bubbleTeaTest.Command(Config),
|
bubbleTeaTest.Command(Config),
|
||||||
config.Command(Config),
|
config.Command(Config),
|
||||||
|
games.Command(Config),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,9 @@ func ReadConfig() (types.Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err = toml.Decode(string(file), &config)
|
_, err = toml.Decode(string(file), &config)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user