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

support: all or nothing #28

Merged
merged 3 commits into from
Oct 30, 2024
Merged
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
60 changes: 58 additions & 2 deletions tparagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/saracen/walker"
)
Expand Down Expand Up @@ -48,7 +49,27 @@ type tparagen struct {
}

func (t *tparagen) run() error {
return walker.Walk(t.in, func(path string, info fs.FileInfo) error {
// Information of files to be modified
// key: original file path, value: temporary file path
// walker.Walk() may execute concurrently, so sync.Map is used.
var tempFiles sync.Map

// remove all temporary files
defer func() {
tempFiles.Range(func(_, p any) bool {
path, ok := p.(string)
if !ok {
return false
}

// Remove temporary files
os.Remove(path)

return true
})
}()

if err := walker.Walk(t.in, func(path string, info fs.FileInfo) error {
if info.IsDir() && t.skipDir(path) {
return filepath.SkipDir
}
Expand All @@ -70,6 +91,14 @@ func (t *tparagen) run() error {
return fmt.Errorf("cannot open %s. %w", path, err)
}
defer f.Close()

tmpf, err := os.CreateTemp("", "temp_")
if err != nil {
return fmt.Errorf("failed to create temp file for %s. %w", path, err)
}

defer tmpf.Close()

b, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("cannot read %s. %w", path, err)
Expand All @@ -81,13 +110,40 @@ func (t *tparagen) run() error {
}

if !bytes.Equal(b, got) {
if _, err := f.WriteAt(got, 0); err != nil {
if _, err := tmpf.WriteAt(got, 0); err != nil {
return fmt.Errorf("error occurred in writeAt(). %w", err)
}
tempFiles.Store(path, tmpf.Name())
}

return nil
}); err != nil {
return fmt.Errorf("error occurred in walker.Walk(). %w", err)
}

// Replace the original file with the temporary file if all writes are successful
tempFiles.Range(func(key, value any) bool {
origPath, ok := key.(string)
if !ok {
return false
}

tmpPath, ok := value.(string)
if !ok {
return false
}

if err := os.Rename(tmpPath, origPath); err != nil {
// TODO: logging
if _, err := t.errStream.Write([]byte(fmt.Sprintf("failed to rename %s to %s. %v\n", tmpPath, origPath, err))); err != nil {
return false
}
}

return true
})

return nil
}

func (t *tparagen) skipDir(p string) bool {
Expand Down
Loading