From 193268db35776dd6411651a4823043dd1d9c1c24 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sat, 10 Nov 2018 12:00:48 +0100 Subject: [PATCH] client: Added Targets and Channels methods for getting multiple targets. --- client.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/client.go b/client.go index bebd506..d9b742c 100644 --- a/client.go +++ b/client.go @@ -443,6 +443,32 @@ func (client *Client) Target(kind string, name string) Target { return nil } +// Targets gets all targets of the given kinds. +func (client *Client) Targets(kinds ...string) []Target { + if len(kinds) == 0 { + client.mutex.Lock() + targets := make([]Target, len(client.targets)) + copy(targets, client.targets) + client.mutex.Unlock() + + return targets + } + + client.mutex.Lock() + targets := make([]Target, 0, len(client.targets)) + for _, target := range client.targets { + for _, kind := range kinds { + if target.Kind() == kind { + targets = append(targets, target) + break + } + } + } + client.mutex.Unlock() + + return targets +} + // Status gets the client's status target. func (client *Client) Status() *Status { return client.status @@ -458,6 +484,18 @@ func (client *Client) Channel(name string) *Channel { return target.(*Channel) } +// Channels gets all channel targets the client has. +func (client *Client) Channels() []*Channel { + targets := client.Targets("channel") + channels := make([]*Channel, len(targets)) + + for i := range targets { + channels[i] = targets[i].(*Channel) + } + + return channels +} + // Query is a shorthand for getting a query target and type asserting it. func (client *Client) Query(name string) *Query { target := client.Target("query", name)