-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
217 lines (180 loc) · 5.05 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"github.com/shibukawa/configdir"
"github.com/skratchdot/open-golang/open"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/admin/directory/v1"
"google.golang.org/api/groupssettings/v1"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
)
var saveDir string
func HasExistingToken() (*oauth2.Token, error) {
b, e := ioutil.ReadFile(path.Join(saveDir, "token.json"))
if e != nil {
return nil, e
}
var token *oauth2.Token
token = new(oauth2.Token)
if e := json.Unmarshal(b, token); e != nil {
return nil, e
}
return token, nil
}
func GetClient() (*http.Client, error) {
ctx := context.Background()
cfgBytes, err := ioutil.ReadFile(path.Join(saveDir, "config.json"))
if err != nil {
return nil, err
}
o2, err := google.ConfigFromJSON(cfgBytes, admin.AdminDirectoryGroupScope, groupssettings.AppsGroupsSettingsScope)
if err != nil {
return nil, err
}
token, err := HasExistingToken()
if err != nil {
url := o2.AuthCodeURL("", oauth2.ApprovalForce)
open.Start(url)
fmt.Printf("If a browser window did not open, please visit: \n%s\n", url)
var code string
fmt.Printf("Enter the given code: ")
if _, err := fmt.Scan(&code); err != nil {
return nil, err
}
token, err := o2.Exchange(ctx, code)
if err != nil {
return nil, err
}
b, _ := json.MarshalIndent(*token, "", " ")
ioutil.WriteFile(path.Join(saveDir, "token.json"), b, 0644)
return o2.Client(ctx, token), nil
} else {
return o2.Client(ctx, token), nil
}
return nil, nil
}
func main() {
args := os.Args
cfgDir := configdir.New("ptdave", "groupcreator")
saveDir = cfgDir.LocalPath
client, err := GetClient()
if err != nil {
println(err.Error())
return
}
adminService, err := admin.New(client)
groupService, err := groupssettings.New(client)
for i, v := range args[1:] {
fmt.Printf("Reading file #%d: %s\n",i,v)
b, e := ioutil.ReadFile(v)
if e!=nil {
fmt.Printf("Error opening file %s : %s\n", v,e.Error())
continue
}
r := csv.NewReader(bytes.NewReader(b))
line:=0
for {
record, err := r.Read()
// Header
if line == 0 {
line++
continue
}
if err == io.EOF {
break
}
if err!=nil {
fmt.Printf("Error reading file: %s\n",err.Error())
break
}
// address, name, member of
address := record[0]
name := record[1]
var memberOf string
if len(record) > 2 {
memberOf = record[2]
}
// Does the group exist?
group, err := adminService.Groups.Get(address).Do()
if err!=nil {
group = new(admin.Group)
group.Name = name
group.Email = address
group, err = adminService.Groups.Insert(group).Do()
if err!= nil {
fmt.Printf("Error creating group %s: %s\n",address, err.Error())
continue
}
}
if group.Name != name {
group.Name = name
group, err = adminService.Groups.Patch(group.Id, group).Do()
if err!= nil {
fmt.Printf("Error updating group %s: %s\n",address, err.Error())
continue
}
}
// We need to check the group settings
settings, err := groupService.Groups.Get(group.Email).Do()
if err != nil {
fmt.Printf("Error pulling settings for group %s\n", group.Name)
} else {
settings.AllowExternalMembers = "false"
settings.AllowGoogleCommunication = "false"
settings.AllowWebPosting = "true"
settings.ArchiveOnly = "false"
settings.IncludeCustomFooter = "false"
settings.IncludeInGlobalAddressList = "true"
settings.IsArchived = "true"
settings.MaxMessageBytes = 26214400
settings.MembersCanPostAsTheGroup = "false"
settings.MessageDisplayFont = "DEFAULT_FONT"
settings.MessageModerationLevel = "MODERATE_NONE"
settings.ReplyTo = "REPLY_TO_SENDER"
settings.SendMessageDenyNotification = "false"
settings.ShowInGroupDirectory = "true"
settings.SpamModerationLevel = "MODERATE"
settings.WhoCanAdd = "ALL_MANAGERS_CAN_ADD"
settings.WhoCanContactOwner = "ALL_IN_DOMAIN_CAN_CONTACT"
settings.WhoCanInvite = "ALL_MANAGERS_CAN_INVITE"
settings.WhoCanJoin = "INVITED_CAN_JOIN"
settings.WhoCanLeaveGroup = "NONE_CAN_LEAVE"
settings.WhoCanPostMessage = "ALL_IN_DOMAIN_CAN_POST"
settings.WhoCanViewGroup = "ALL_IN_DOMAIN_CAN_VIEW"
settings.WhoCanViewMembership = "ALL_MEMBERS_CAN_VIEW"
if _, e := groupService.Groups.Patch(group.Email, settings).Do(); e != nil {
fmt.Printf("Error patching settings: %s", e.Error())
}
}
if len(strings.TrimSpace(memberOf))>0 {
parentGroups := strings.Split(memberOf,",")
for _, parent := range parentGroups {
// Is it a member of the parent group?
member, err := adminService.Members.Get(parent, address).Do()
if err != nil {
// Not a member, add it
member = new(admin.Member)
member.Email = address
member.Role = "MEMBER"
member, err = adminService.Members.Insert(parent, member).Do()
if err!= nil {
fmt.Printf("Error making %s a member of %s: %s\n",address,parent,err.Error())
continue
}
}
}
}
line++
}
}
}