2021-07-26 15:28:11 +03:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/pkgsite/internal/derrors"
|
|
|
|
)
|
|
|
|
|
2023-02-07 17:22:14 +03:00
|
|
|
// ReadFileLines reads and returns the lines from a file.
|
2021-07-26 15:28:11 +03:00
|
|
|
// Whitespace on each line is trimmed.
|
|
|
|
// Blank lines and lines beginning with '#' are ignored.
|
|
|
|
func ReadFileLines(filename string) (lines []string, err error) {
|
2021-08-08 14:13:22 +03:00
|
|
|
defer derrors.Wrap(&err, "ReadFileLines(%q)", filename)
|
2021-07-26 15:28:11 +03:00
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
s := bufio.NewScanner(f)
|
|
|
|
for s.Scan() {
|
|
|
|
line := strings.TrimSpace(s.Text())
|
|
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
if s.Err() != nil {
|
|
|
|
return nil, s.Err()
|
|
|
|
}
|
|
|
|
return lines, nil
|
|
|
|
}
|