Skip to content

Commit

Permalink
Merge pull request #147 from MathieuBordere/more-robust-filewriting
Browse files Browse the repository at this point in the history
More robust filewriting
  • Loading branch information
stgraber committed Jun 24, 2021
2 parents b2e31f3 + 31c1135 commit eb26312
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ func (a *App) run(ctx context.Context, frequency time.Duration, join bool) {
cli.Close()
continue
}
if len(servers) == 0 {
a.warn("server list empty")
cli.Close()
continue
}
a.store.Set(ctx, servers)

// If we are starting up, let's see if we should
Expand Down
3 changes: 2 additions & 1 deletion app/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/ghodss/yaml"
"github.com/google/renameio"
)

const (
Expand Down Expand Up @@ -40,7 +41,7 @@ func fileExists(dir, file string) (bool, error) {
func fileWrite(dir, file string, data []byte) error {
path := filepath.Join(dir, file)

if err := ioutil.WriteFile(path, data, 0600); err != nil {
if err := renameio.WriteFile(path, data, 0600); err != nil {
return fmt.Errorf("write %s: %w", file, err)
}

Expand Down
3 changes: 2 additions & 1 deletion client/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"github.com/ghodss/yaml"
"github.com/google/renameio"
"github.com/pkg/errors"

"github.com/canonical/go-dqlite/internal/protocol"
Expand Down Expand Up @@ -228,7 +229,7 @@ func (s *YamlNodeStore) Set(ctx context.Context, servers []NodeInfo) error {
return err
}

if err := ioutil.WriteFile(s.path, data, 0600); err != nil {
if err := renameio.WriteFile(s.path, data, 0600); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/Rican7/retry v0.1.0
github.com/ghodss/yaml v1.0.0
github.com/google/renameio v1.0.1
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.7
github.com/peterh/liner v1.2.1
Expand Down

0 comments on commit eb26312

Please sign in to comment.