-
Notifications
You must be signed in to change notification settings - Fork 0
/
refreshUsers.go
68 lines (56 loc) · 1.34 KB
/
refreshUsers.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
package htb
import (
"../config"
"io/ioutil"
"encoding/json"
"fmt"
"sync"
"time"
)
func StartRefreshUsers(ticker *time.Ticker){
RefreshUsers()
for {
select {
case <- ticker.C:
RefreshUsers()
}
}
}
func RefreshUsers() {
/*
TODO:
- move embed to own function
- Add mutex to write !
*/
var users []config.User
byteValue, err := ioutil.ReadFile("users.json")
if err != nil{
fmt.Println("[!] Error RefreshUsers, cannot read users.json")
return
}
json.Unmarshal(byteValue, &users)
// Create progress slice
progress := make([]config.Progress, len(users))
// Parse users profil asynchronously
var wg sync.WaitGroup
for i, _ := range users {
wg.Add(1)
go ParseUserProfil(&wg, &users[i], &progress[i])
}
wg.Wait()
// Create file with new data
data, _ := json.Marshal(users)
err = ioutil.WriteFile("users.json", data, 0644)
if err != nil{
fmt.Println("[!] error RefreshUsers : cannot create users file")
return
}
// Create file with new data
data, _ = json.Marshal(progress)
err = ioutil.WriteFile("progress.json", data, 0644)
if err != nil{
fmt.Println("[!] error in RefreshUsers : cannot create progress file")
return
}
return
}