fix: prevent file overwrite and improve output messaging in CreateOutputFile

## CHANGES

- Add file existence check before creating output file
- Return error if target file already exists
- Change success message to write to stderr
- Update message format with brackets for clarity
- Prevent accidental file overwrites during output creation
This commit is contained in:
Kayvan Sylvan
2025-07-26 20:16:47 -07:00
parent 737e37f00e
commit 6aa38d2abc

View File

@@ -17,6 +17,10 @@ func CopyToClipboard(message string) (err error) {
}
func CreateOutputFile(message string, fileName string) (err error) {
if _, err = os.Stat(fileName); err == nil {
err = fmt.Errorf("file %s already exists, not overwriting. Rename the existing file or choose a different name", fileName)
return
}
var file *os.File
if file, err = os.Create(fileName); err != nil {
err = fmt.Errorf("error creating file: %v", err)
@@ -26,7 +30,7 @@ func CreateOutputFile(message string, fileName string) (err error) {
if _, err = file.WriteString(message); err != nil {
err = fmt.Errorf("error writing to file: %v", err)
} else {
fmt.Printf("\n\n... written to %s\n", fileName)
fmt.Fprintf(os.Stderr, "\n\n[Output also written to %s]\n", fileName)
}
return
}