Skip to content

Commit

Permalink
Merge pull request #2141 from alixander/atomic-writes
Browse files Browse the repository at this point in the history
cli: atomic writes
  • Loading branch information
alixander authored Oct 5, 2024
2 parents fba0a3e + 1ba5ff0 commit 0e08d35
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Improvements 🧹

- Lib: removes a dependency on external slog that was causing troubles with installation [#2137](https://github.com/terrastruct/d2/pull/2137)
- CLI: attempts writing to path atomically, falling back to non-atomic if failed [#2141](https://github.com/terrastruct/d2/pull/2141)

#### Bugfixes ⛑️

Expand Down
15 changes: 12 additions & 3 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
if err != nil {
return nil, false, err
}
err = ms.WritePath(outputPath, out)
err = Write(ms, outputPath, out)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -621,7 +621,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
if err != nil {
return nil, false, err
}
err = ms.WritePath(outputPath, out)
err = Write(ms, outputPath, out)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -906,7 +906,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
if err != nil {
return svg, err
}
err = ms.WritePath(outputPath, out)
err = Write(ms, outputPath, out)
if err != nil {
return svg, err
}
Expand Down Expand Up @@ -1369,6 +1369,15 @@ func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, er
return xgif.AnimatePNGs(pngs, animIntervalMs)
}

func Write(ms *xmain.State, path string, out []byte) error {
err := ms.AtomicWritePath(path, out)
if err == nil {
return nil
}
ms.Log.Debug.Printf("atomic write failed: %s, trying non-atomic write", err.Error())
return ms.WritePath(path, out)
}

func init() {
log.Init()
}
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e08d35

Please sign in to comment.