forked from bufbuild/buf
/
io.go
69 lines (62 loc) · 1.91 KB
/
io.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufcurl
import (
"bufio"
"fmt"
"io"
"strings"
)
// ErrorHasFilename makes sure that the given error includes a reference to the
// given filename. If not, it wraps the given error and adds the filename. This
// is to make sure errors are useful -- an error related to file I/O is not very
// helpful if it doesn't indicate the name of the file.
func ErrorHasFilename(err error, filename string) error {
if strings.Contains(err.Error(), filename) {
return err
}
return fmt.Errorf("%s: %w", filename, err)
}
type readerWithClose struct {
io.Reader
io.Closer
}
// lineReader wraps a *bufio.Reader, making it easier to read a file one line
// at a time.
type lineReader struct {
r *bufio.Reader
err error
}
func (r *lineReader) Read(p []byte) (n int, err error) {
return r.r.Read(p)
}
func (r *lineReader) ReadLine() (string, error) {
if r.err != nil {
return "", r.err
}
str, err := r.r.ReadString('\n')
// Instead of returning data AND error, like bufio.Reader.ReadString,
// only return one or the other since that is easier for the caller.
if err != nil {
if str != "" {
r.err = err // save for next call
return str, nil
}
return "", err
}
// If bufio.Reader.ReadString returns nil err, then the string ends
// with the delimiter. Remove it.
str = strings.TrimSuffix(str, "\n")
return str, nil
}