-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (96 loc) · 1.91 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"math/rand"
"os"
"time"
)
// User participating in Secret Santa
type User struct {
Name string
Email string
}
// Message to participants in Secret Santa
type Message struct {
SecretSanta User
Recipient User
}
func main() {
users, err := fetchUsers(os.Args[1])
if err != nil {
log.Fatalf("Unable to fetch Users: %v", err)
}
messages := buildMessages(users)
debugMessages(messages)
}
//fetchUsers get the users from input csv
func fetchUsers(fileName string) ([]User, error) {
csvFile, err := os.Open(fileName)
if err != nil {
return nil, err
}
reader := csv.NewReader(bufio.NewReader(csvFile))
var users []User
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if record[0] == "name" {
continue
}
users = append(users, User{
Name: record[0],
Email: record[1],
})
}
return users, nil
}
// buildMessages pairs up users and gets messages ready
func buildMessages(users []User) []Message {
rand.Seed(time.Now().UnixNano())
var messages []Message
santa := users[0]
for len(users) > 1 {
users = users[1:]
rand.Shuffle(
len(users),
func(i, j int) { users[i], users[j] = users[j], users[i] },
)
recipient := users[0]
messages = append(messages, Message{SecretSanta: santa, Recipient: recipient})
santa = recipient
}
// Close messages into a loop
messages = append(
messages,
Message{
SecretSanta: messages[len(messages)-1].Recipient,
Recipient: messages[0].SecretSanta,
},
)
return messages
}
func sendMessages(messages []Message) error {
return nil
}
func persisteMessages(messages []Message) error {
// persist by CSV
return nil
}
func debugMessages(messages []Message) {
for _, message := range messages {
fmt.Printf(
"Secret Santa: %s, Recipient: %s\n",
message.SecretSanta,
message.Recipient,
)
}
}