-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.go
57 lines (45 loc) · 944 Bytes
/
team.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
51
52
53
54
55
56
57
package main
import (
"encoding/json"
"github.com/charmbracelet/log"
"go.etcd.io/bbolt"
)
type Team struct {
ID string
Name string
Token string
}
func ListTeams() ([]Team, error) {
teams := []Team{}
err := App.db.View(func(tx *bbolt.Tx) error {
teamsBucket := tx.Bucket([]byte("teams"))
return teamsBucket.ForEach(func(k, v []byte) error {
var team Team
err := json.Unmarshal(v, &team)
if err != nil {
return err
}
teams = append(teams, team)
return nil
})
})
return teams, err
}
func (t *Team) Save() error {
return App.db.Update(func(tx *bbolt.Tx) error {
teamsBucket := tx.Bucket([]byte("teams"))
data, err := json.Marshal(t)
if err != nil {
return err
}
return teamsBucket.Put([]byte(t.ID), data)
})
}
func (t *Team) Connect() {
App.wg.Add(1)
defer App.wg.Done()
err := ConnectTeam(*t)
if err != nil {
log.Error("Team disconnected.", "team", t.ID, "err", err)
}
}