This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-core/pkg/tools/file/write.go

40 lines
1.2 KiB
Go

package file
import (
"fmt"
"os"
"strings"
)
// Write writes content to path. If startLine and endLine are both zero the
// file is written in full; otherwise the given line range is replaced.
func Write(path, content string, startLine, endLine int) (string, error) {
if startLine == 0 && endLine == 0 {
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
return "", fmt.Errorf("file_write: %w", err)
}
return fmt.Sprintf("wrote %d bytes to %s", len(content), path), nil
}
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("file_write: %w", err)
}
lines := strings.Split(string(data), "\n")
start, end := startLine, endLine
if start < 1 {
start = 1
}
if end > len(lines) {
end = len(lines)
}
if start > end {
return "", fmt.Errorf("file_write: start_line %d > end_line %d", start, end)
}
newLines := strings.Split(content, "\n")
result := append(lines[:start-1], append(newLines, lines[end:]...)...)
if err := os.WriteFile(path, []byte(strings.Join(result, "\n")), 0644); err != nil {
return "", fmt.Errorf("file_write: %w", err)
}
return fmt.Sprintf("replaced lines %d-%d in %s", start, end, path), nil
}