Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(commands): add a list func to export registered commands #374

Merged
merged 1 commit into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,18 @@ func CreateCommand(commandType CommandType) (Command, error) {
return nil, ErrCommandNotRegistered
}

// ListCommands returns a list of all registered command types.
func RegisteredCommands() map[CommandType]func() Command {
commandsMu.Lock()
defer commandsMu.Unlock()

mapCopy := make(map[CommandType]func() Command)
for key, val := range commands {
mapCopy[key] = val
}

return mapCopy
}

var commands = make(map[CommandType]func() Command)
var commandsMu sync.RWMutex
12 changes: 12 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestCreateCommand(t *testing.T) {
}

RegisterCommand(func() Command { return &TestCommandRegister{} })
defer UnregisterCommand(TestCommandRegisterType)

cmd, err := CreateCommand(TestCommandRegisterType)
if err != nil {
Expand Down Expand Up @@ -87,6 +88,17 @@ func TestUnregisterCommandTwice(t *testing.T) {
UnregisterCommand(TestCommandUnregisterTwiceType)
}

func TestRegisteredCommands(t *testing.T) {
commands = make(map[CommandType]func() Command)
RegisterCommand(func() Command { return &TestCommandRegister{} })
defer UnregisterCommand(TestCommandRegisterType)

commands := RegisteredCommands()
if len(commands) != 1 {
t.Error("there should be one command:", commands)
}
}

const (
TestCommandRegisterType CommandType = "TestCommandRegister"
TestCommandRegisterEmptyType CommandType = ""
Expand Down