enbas/internal/utilities/file.go
Dan Anglin 4e76d20a7a
fix: strip trailing empty line when reading files
Updated the ReadFile function in 'utilities' by using the Scanner from
the bufio package to read lines from a text file. The trailing empty
line is stripped after scanning and before returning back to the caller.
2024-08-15 15:24:25 +01:00

46 lines
857 B
Go

package utilities
import (
"bufio"
"errors"
"fmt"
"os"
)
func ReadFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("unable to open %q: %w", path, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("received an error after scanning the contents from %q: %w", path, err)
}
if lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
return strings.Join(lines, "\n"), nil
}
func FileExists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, fmt.Errorf("unable to check if the file exists: %w", err)
}
return true, nil
}