From dcde4562f56922505908f00fcc2a70c974a04c6d Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sat, 9 Dec 2023 16:11:30 +0100 Subject: [PATCH] Avoid code duplication in openDescriptionFile. --- group/group.go | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/group/group.go b/group/group.go index b60fce0..5d97916 100644 --- a/group/group.go +++ b/group/group.go @@ -1007,13 +1007,13 @@ func maxHistoryAge(desc *Description) time.Duration { return DefaultMaxHistoryAge } -func openDescriptionFile(name string) (*os.File, string, bool, error) { +func getDescriptionFile[T any](name string, get func(string) (T, error)) (T, string, bool, error) { isParent := false for name != "" { fileName := filepath.Join( Directory, path.Clean("/"+name)+".json", ) - r, err := os.Open(fileName) + r, err := get(fileName) if !os.IsNotExist(err) { return r, fileName, isParent, err } @@ -1021,24 +1021,8 @@ func openDescriptionFile(name string) (*os.File, string, bool, error) { name, _ = path.Split(name) name = strings.TrimRight(name, "/") } - return nil, "", false, os.ErrNotExist -} - -func statDescriptionFile(name string) (os.FileInfo, string, bool, error) { - isParent := false - for name != "" { - fileName := filepath.Join( - Directory, path.Clean("/"+name)+".json", - ) - fi, err := os.Stat(fileName) - if !os.IsNotExist(err) { - return fi, fileName, isParent, err - } - isParent = true - name, _ = path.Split(name) - name = strings.TrimRight(name, "/") - } - return nil, "", false, os.ErrNotExist + var zero T + return zero, "", false, os.ErrNotExist } // descriptionMatch returns true if the description hasn't changed between @@ -1057,7 +1041,7 @@ func descriptionMatch(d1, d2 *Description) bool { // descriptionUnchanged returns true if a group's description hasn't // changed since it was last read. func descriptionUnchanged(name string, desc *Description) bool { - fi, fileName, _, err := statDescriptionFile(name) + fi, fileName, _, err := getDescriptionFile(name, os.Stat) if err != nil || fileName != desc.FileName { return false } @@ -1082,7 +1066,7 @@ func GetDescription(name string) (*Description, error) { // readDescription reads a group's description from disk func readDescription(name string) (*Description, error) { - r, fileName, isParent, err := openDescriptionFile(name) + r, fileName, isParent, err := getDescriptionFile(name, os.Open) if err != nil { return nil, err }