-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
users.go
71 lines (61 loc) · 1.53 KB
/
users.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package types
import (
"fmt"
"io"
"sort"
"text/tabwriter"
"github.com/slack-go/slack"
"github.com/rusq/slackdump/v2/internal/structures"
)
// Users is a slice of users.
type Users []slack.User
// ToText outputs Users us to io.Writer w in Text format
func (us Users) ToText(w io.Writer, _ structures.UserIndex) error {
const strFormat = "%s\t%s\t%s\t%s\t%s\n"
writer := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
defer writer.Flush()
// header
if _, err := fmt.Fprintf(writer, strFormat, "Name", "ID", "Bot?", "Deleted?", "Restricted?"); err != nil {
return fmt.Errorf("writer error: %w", err)
}
if _, err := fmt.Fprintf(writer, strFormat, "", "", "", "", ""); err != nil {
return fmt.Errorf("writer error: %w", err)
}
var (
names = make([]string, 0, len(us))
usermap = make(structures.UserIndex, len(us))
)
for i := range us {
names = append(names, us[i].Name)
usermap[us[i].Name] = &us[i]
}
sort.Strings(names)
// data
for _, name := range names {
var (
deleted string
bot string
restricted string
)
if usermap[name].Deleted {
deleted = "deleted"
}
if usermap[name].IsBot {
bot = "bot"
}
if usermap[name].IsRestricted {
restricted = "restricted"
}
_, err := fmt.Fprintf(writer, strFormat,
name, usermap[name].ID, bot, deleted, restricted,
)
if err != nil {
return fmt.Errorf("writer error: %w", err)
}
}
return nil
}
// IndexByID returns the userID map to relevant *slack.User
func (us Users) IndexByID() structures.UserIndex {
return structures.NewUserIndex(us)
}