aboutsummaryrefslogtreecommitdiff
blob: a0d0065ec26489e1dc5ae70b86aec85033735393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Contains utility functions to read the content of files

package utils

import (
	"bufio"
	"os"
)

// readLines reads a whole file into memory
// and returns a slice of its lines.
func ReadLines(path string) ([]string, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	return lines, scanner.Err()
}

// FileExists checks whether the file
// at the given path does exist
func FileExists(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}