add 'map' cli command to provide map information (#100)
* add 'map' cli command - provides the following map information functions: - list all available maps in the global registry - display map metadata - update docs with map command examples * add list and info subcommands to map cli command * rename map command list and info factory functions * add --all flag to map info subcommand * handle cmd.Help error
This commit is contained in:
parent
91106aec09
commit
ffeb401377
7 changed files with 177 additions and 0 deletions
78
cli/commands/info.go
Normal file
78
cli/commands/info.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/BattlesnakeOfficial/rules/maps"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type mapInfo struct {
|
||||
All bool
|
||||
}
|
||||
|
||||
func NewMapInfoCommand() *cobra.Command {
|
||||
info := mapInfo{}
|
||||
var infoCmd = &cobra.Command{
|
||||
Use: "info [flags] map_name [...map_name]",
|
||||
Short: "Display metadata for given map(s)",
|
||||
Long: "Display metadata for given map(s)",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// handle --all flag first as there would be no args
|
||||
if info.All {
|
||||
mapList := maps.List()
|
||||
for i, m := range mapList {
|
||||
info.display(m)
|
||||
if i < (len(mapList) - 1) {
|
||||
fmt.Print("\n")
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// display help when no map(s) provided via args
|
||||
if len(args) < 1 {
|
||||
err := cmd.Help()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// display all maps via command args
|
||||
for i, m := range args {
|
||||
info.display(m)
|
||||
if i < (len(args) - 1) {
|
||||
fmt.Print("\n")
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
infoCmd.Flags().BoolVarP(&info.All, "all", "a", false, "Display information for all maps")
|
||||
|
||||
return infoCmd
|
||||
}
|
||||
|
||||
func (m *mapInfo) display(id string) {
|
||||
gameMap, err := maps.GetMap(id)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load game map %#v: %v", id, err)
|
||||
}
|
||||
meta := gameMap.Meta()
|
||||
fmt.Println("Name:", meta.Name)
|
||||
fmt.Println("Author:", meta.Author)
|
||||
fmt.Println("Description:", meta.Description)
|
||||
fmt.Println("Version:", meta.Version)
|
||||
fmt.Println("Min Players:", meta.MinPlayers)
|
||||
fmt.Println("Max Players:", meta.MaxPlayers)
|
||||
fmt.Print("Board Sizes (WxH):")
|
||||
for i, s := range meta.BoardSizes {
|
||||
fmt.Printf(" %dx%d", s.Width, s.Height)
|
||||
if i == (len(meta.BoardSizes) - 1) {
|
||||
fmt.Print("\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
22
cli/commands/list.go
Normal file
22
cli/commands/list.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/BattlesnakeOfficial/rules/maps"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewMapListCommand() *cobra.Command {
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List available game maps",
|
||||
Long: "List available game maps",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
for _, m := range maps.List() {
|
||||
fmt.Println(m)
|
||||
}
|
||||
},
|
||||
}
|
||||
return listCmd
|
||||
}
|
||||
24
cli/commands/map.go
Normal file
24
cli/commands/map.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewMapCommand() *cobra.Command {
|
||||
|
||||
var mapCmd = &cobra.Command{
|
||||
Use: "map",
|
||||
Short: "Display map information",
|
||||
Long: "Display map information",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := cmd.Help()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
return mapCmd
|
||||
}
|
||||
|
|
@ -21,6 +21,12 @@ var rootCmd = &cobra.Command{
|
|||
func Execute() {
|
||||
rootCmd.AddCommand(NewPlayCommand())
|
||||
|
||||
mapCommand := NewMapCommand()
|
||||
mapCommand.AddCommand(NewMapListCommand())
|
||||
mapCommand.AddCommand(NewMapInfoCommand())
|
||||
|
||||
rootCmd.AddCommand(mapCommand)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue