Skip to content

Commit 1666abf

Browse files
committed
Add allowSubgroups argument to readDescription.
1 parent b7094fc commit 1666abf

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

group/description.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,20 @@ func maxHistoryAge(desc *Description) time.Duration {
205205
return DefaultMaxHistoryAge
206206
}
207207

208-
func getDescriptionFile[T any](name string, get func(string) (T, error)) (T, string, bool, error) {
209-
isParent := false
208+
func getDescriptionFile[T any](name string, allowSubgroups bool, get func(string) (T, error)) (T, string, bool, error) {
209+
isSubgroup := false
210210
for name != "" {
211211
fileName := filepath.Join(
212212
Directory, path.Clean("/"+name)+".json",
213213
)
214214
r, err := get(fileName)
215215
if !os.IsNotExist(err) {
216-
return r, fileName, isParent, err
216+
return r, fileName, isSubgroup, err
217217
}
218-
isParent = true
218+
if !allowSubgroups {
219+
break
220+
}
221+
isSubgroup = true
219222
name, _ = path.Split(name)
220223
name = strings.TrimRight(name, "/")
221224
}
@@ -239,7 +242,7 @@ func descriptionMatch(d1, d2 *Description) bool {
239242
// descriptionUnchanged returns true if a group's description hasn't
240243
// changed since it was last read.
241244
func descriptionUnchanged(name string, desc *Description) bool {
242-
fi, fileName, _, err := getDescriptionFile(name, os.Stat)
245+
fi, fileName, _, err := getDescriptionFile(name, true, os.Stat)
243246
if err != nil || fileName != desc.FileName {
244247
return false
245248
}
@@ -259,12 +262,13 @@ func GetDescription(name string) (*Description, error) {
259262
}
260263
}
261264

262-
return readDescription(name)
265+
return readDescription(name, true)
263266
}
264267

265268
// readDescription reads a group's description from disk
266-
func readDescription(name string) (*Description, error) {
267-
r, fileName, isParent, err := getDescriptionFile(name, os.Open)
269+
func readDescription(name string, allowSubgroups bool) (*Description, error) {
270+
r, fileName, isSubgroup, err :=
271+
getDescriptionFile(name, allowSubgroups, os.Open)
268272
if err != nil {
269273
return nil, err
270274
}
@@ -283,7 +287,7 @@ func readDescription(name string) (*Description, error) {
283287
if err != nil {
284288
return nil, err
285289
}
286-
if isParent {
290+
if isSubgroup {
287291
if !desc.AutoSubgroups {
288292
return nil, os.ErrNotExist
289293
}

group/group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func add(name string, desc *Description) (*Group, []Client, error) {
446446
g := groups.groups[name]
447447
if g == nil {
448448
if desc == nil {
449-
desc, err = readDescription(name)
449+
desc, err = readDescription(name, true)
450450
if err != nil {
451451
return nil, nil, err
452452
}
@@ -471,7 +471,7 @@ func add(name string, desc *Description) (*Group, []Client, error) {
471471
notify = true
472472
}
473473
} else if !descriptionUnchanged(name, g.description) {
474-
desc, err = readDescription(name)
474+
desc, err = readDescription(name, true)
475475
if err != nil {
476476
if !os.IsNotExist(err) {
477477
log.Printf("Reading group %v: %v", name, err)

0 commit comments

Comments
 (0)