diff --git a/internal/utilities/file.go b/internal/utilities/file.go index eb80ccd..d6e29a7 100644 --- a/internal/utilities/file.go +++ b/internal/utilities/file.go @@ -1,18 +1,36 @@ package utilities import ( + "bufio" "errors" "fmt" "os" ) func ReadFile(path string) (string, error) { - data, err := os.ReadFile(path) + file, err := os.Open(path) if err != nil { - return "", fmt.Errorf("unable to read the data from the file: %w", err) + 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()) } - return string(data), nil + 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) {