-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters