Create agents.ReplaceSection(content, profile) as the single canonical function for replacing beads integration sections. Both init_agent.go (upgradeBeadsSection) and setup/agents.go (updateBeadsSectionWithProfile) now delegate to it. Key changes: - Hash-based freshness check skips rewrite when content is current - Returns error on malformed markers (missing/reversed) instead of silent append - Marker format now includes v:1 version field for forward compat - atomicWriteFile accepts optional permissions param (default 0644) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package setup
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/steveyegge/beads/internal/utils"
|
|
)
|
|
|
|
// atomicWriteFile writes data to a file atomically using a unique temporary file.
|
|
// This prevents race conditions when multiple processes write to the same file.
|
|
// If path is a symlink, writes to the resolved target (preserving the symlink).
|
|
// An optional permissions argument sets the file mode (default 0644).
|
|
//
|
|
//nolint:unparam // perm is intentionally variadic for callers that need non-default permissions
|
|
func atomicWriteFile(path string, data []byte, perm ...os.FileMode) error {
|
|
mode := os.FileMode(0644)
|
|
if len(perm) > 0 {
|
|
mode = perm[0]
|
|
}
|
|
|
|
targetPath, err := utils.ResolveForWrite(path)
|
|
if err != nil {
|
|
return fmt.Errorf("resolve path: %w", err)
|
|
}
|
|
|
|
dir := filepath.Dir(targetPath)
|
|
|
|
// Create unique temp file in same directory
|
|
tmpFile, err := os.CreateTemp(dir, ".*.tmp")
|
|
if err != nil {
|
|
return fmt.Errorf("create temp file: %w", err)
|
|
}
|
|
tmpPath := tmpFile.Name()
|
|
|
|
// Write data
|
|
if _, err := tmpFile.Write(data); err != nil {
|
|
_ = tmpFile.Close() // best effort cleanup
|
|
_ = os.Remove(tmpPath) // best effort cleanup
|
|
return fmt.Errorf("write temp file: %w", err)
|
|
}
|
|
|
|
// Close temp file
|
|
if err := tmpFile.Close(); err != nil {
|
|
_ = os.Remove(tmpPath) // Best effort cleanup
|
|
return fmt.Errorf("close temp file: %w", err)
|
|
}
|
|
|
|
if err := os.Chmod(tmpPath, mode); err != nil {
|
|
_ = os.Remove(tmpPath) // Best effort cleanup
|
|
return fmt.Errorf("set permissions: %w", err)
|
|
}
|
|
|
|
// Atomic rename
|
|
if err := os.Rename(tmpPath, targetPath); err != nil {
|
|
_ = os.Remove(tmpPath) // Best effort cleanup
|
|
return fmt.Errorf("rename temp file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DirExists checks if a directory exists
|
|
func DirExists(path string) bool {
|
|
info, err := os.Stat(path)
|
|
return err == nil && info.IsDir()
|
|
}
|
|
|
|
// FileExists checks if a file exists
|
|
func FileExists(path string) bool {
|
|
info, err := os.Stat(path)
|
|
return err == nil && !info.IsDir()
|
|
}
|
|
|
|
// EnsureDir creates a directory if it doesn't exist
|
|
func EnsureDir(path string, perm os.FileMode) error {
|
|
if DirExists(path) {
|
|
return nil
|
|
}
|
|
if err := os.MkdirAll(path, perm); err != nil {
|
|
return fmt.Errorf("create directory: %w", err)
|
|
}
|
|
return nil
|
|
}
|