-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.go
50 lines (38 loc) · 837 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package internal
import (
"fmt"
"sync"
"github.com/owenthereal/upterm/host/api"
)
func NewClientRepo() *ClientRepo {
return &ClientRepo{}
}
type ClientRepo struct {
clients sync.Map
}
func (c *ClientRepo) Add(client *api.Client) error {
_, loaded := c.clients.LoadOrStore(client.Id, client)
if loaded {
return fmt.Errorf("client already exists")
}
return nil
}
func (c *ClientRepo) Delete(clientId string) {
c.clients.Delete(clientId)
}
func (c *ClientRepo) Get(clientId string) *api.Client {
val, _ := c.clients.Load(clientId)
if val != nil {
return val.(*api.Client)
}
return nil
}
func (c *ClientRepo) Clients() []*api.Client {
var clients []*api.Client
c.clients.Range(func(key, value interface{}) bool {
cc := value.(*api.Client)
clients = append(clients, cc)
return true
})
return clients
}