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
|
|
@ -2,6 +2,7 @@ package maps
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/BattlesnakeOfficial/rules"
|
||||
)
|
||||
|
|
@ -21,6 +22,16 @@ func (registry MapRegistry) RegisterMap(id string, m GameMap) {
|
|||
registry[id] = m
|
||||
}
|
||||
|
||||
// List returns all registered map IDs in alphabetical order
|
||||
func (registry MapRegistry) List() []string {
|
||||
var keys []string
|
||||
for k, _ := range registry {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
// GetMap returns the map associated with the given ID.
|
||||
func (registry MapRegistry) GetMap(id string) (GameMap, error) {
|
||||
if m, ok := registry[id]; ok {
|
||||
|
|
@ -34,6 +45,11 @@ func GetMap(id string) (GameMap, error) {
|
|||
return globalRegistry.GetMap(id)
|
||||
}
|
||||
|
||||
// List returns a list of maps registered to the global registry.
|
||||
func List() []string {
|
||||
return globalRegistry.List()
|
||||
}
|
||||
|
||||
// RegisterMap adds a map to the global registry.
|
||||
func RegisterMap(id string, m GameMap) {
|
||||
globalRegistry.RegisterMap(id, m)
|
||||
|
|
|
|||
|
|
@ -112,3 +112,15 @@ func pickSize(meta Metadata) Dimensions {
|
|||
// For fixed, just pick the first supported size
|
||||
return meta.BoardSizes[0]
|
||||
}
|
||||
|
||||
func TestListRegisteredMaps(t *testing.T) {
|
||||
keys := globalRegistry.List()
|
||||
mapCount := 0
|
||||
for k := range globalRegistry {
|
||||
// every registry key should exist in List results
|
||||
require.Contains(t, keys, k)
|
||||
mapCount++
|
||||
}
|
||||
// List should equal number of maps in the global registry
|
||||
require.Equal(t, len(keys), mapCount)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue