From a84c9c60007c42cbbf236a6c5aa0c540194b0591 Mon Sep 17 00:00:00 2001 From: Eugene Dementyev Date: Tue, 26 Oct 2021 21:30:24 +1300 Subject: [PATCH] Properly closes files in the cycle Fixes #26 --- lib/cache/yaml.go | 55 +++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/cache/yaml.go b/lib/cache/yaml.go index 3cf5cfc..46a8a2c 100644 --- a/lib/cache/yaml.go +++ b/lib/cache/yaml.go @@ -84,34 +84,37 @@ func (y *YAMLCache) Save(profileSummaries []lib.ProcessedProfileSummary) error { // every ssh entry is self-contained for _, summary := range profileSummaries { for _, sshEntry := range summary.SSHEntries { - var fileName = path.Join(instancesPath, fmt.Sprintf("%s.yaml", sshEntry.InstanceID)) - file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0644) - if err != nil { - return fmt.Errorf("can't open %s: %s", fileName, err) - } - - defer file.Close() - encoder := yaml.NewEncoder(file) - err = encoder.Encode(&sshEntry) - if err != nil { - return fmt.Errorf("can't encode %s: %s", fileName, err) - } - if err := encoder.Close(); err != nil { - return fmt.Errorf("can't close %s: %s", fileName, err) - } - - // add every instance name to the index and resolve to instance id - for n, name := range sshEntry.Names { - if name != sshEntry.InstanceID { - index[name] = sshEntry.InstanceID - // add the first name to canonical names - if n == 0 { - y.index.CanonicalNames = append(y.index.CanonicalNames, name) + return func() error { + var fileName = path.Join(instancesPath, fmt.Sprintf("%s.yaml", sshEntry.InstanceID)) + file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + return fmt.Errorf("can't open %s: %s", fileName, err) + } + + defer file.Close() + encoder := yaml.NewEncoder(file) + err = encoder.Encode(&sshEntry) + if err != nil { + return fmt.Errorf("can't encode %s: %s", fileName, err) + } + if err := encoder.Close(); err != nil { + return fmt.Errorf("can't close %s: %s", fileName, err) + } + + // add every instance name to the index and resolve to instance id + for n, name := range sshEntry.Names { + if name != sshEntry.InstanceID { + index[name] = sshEntry.InstanceID + // add the first name to canonical names + if n == 0 { + y.index.CanonicalNames = append(y.index.CanonicalNames, name) + } + } else { + index[name] = "" } - } else { - index[name] = "" } - } + return nil + }() } } sort.Strings(y.index.CanonicalNames)