Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: atomic writes #2141

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading