Skip to content

Commit

Permalink
Correctly handle errors when saving to yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Dementyev authored and ekini committed Nov 16, 2021
1 parent 1dcb606 commit f2768cb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/cache/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"gopkg.in/yaml.v2"

"github.com/hashicorp/go-multierror"
fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
)

Expand Down Expand Up @@ -81,10 +82,11 @@ func (y *YAMLCache) Save(profileSummaries []lib.ProcessedProfileSummary) error {
if err != nil {
return err
}
var errors error
// every ssh entry is self-contained
for _, summary := range profileSummaries {
for _, sshEntry := range summary.SSHEntries {
func() error {
if err := 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 {
Expand Down Expand Up @@ -114,9 +116,14 @@ func (y *YAMLCache) Save(profileSummaries []lib.ProcessedProfileSummary) error {
}
}
return nil
}()
}(); err != nil {
errors = multierror.Append(errors, err)
}
}
}
if errors != nil {
return errors
}
sort.Strings(y.index.CanonicalNames)
y.index.InstancesIndex = index

Expand Down

0 comments on commit f2768cb

Please sign in to comment.