github.com/docker-library/go-dockerlibrary@v0.0.0-20200821205225-669fbe5c1d52/pkg/stripper/comments.go (about) 1 package stripper 2 3 import ( 4 "bufio" 5 "bytes" 6 "io" 7 "strings" 8 "unicode" 9 ) 10 11 type CommentStripper struct { 12 Comment string 13 Delimiter byte 14 Whitespace bool 15 16 r *bufio.Reader 17 buf bytes.Buffer 18 } 19 20 func NewCommentStripper(r io.Reader) *CommentStripper { 21 return &CommentStripper{ 22 Comment: "#", 23 Delimiter: '\n', 24 Whitespace: true, 25 26 r: bufio.NewReader(r), 27 } 28 } 29 30 func (r *CommentStripper) Read(p []byte) (int, error) { 31 for { 32 if r.buf.Len() >= len(p) { 33 return r.buf.Read(p) 34 } 35 line, err := r.r.ReadString(r.Delimiter) 36 if len(line) > 0 { 37 checkLine := line 38 if r.Whitespace { 39 checkLine = strings.TrimLeftFunc(checkLine, unicode.IsSpace) 40 } 41 if strings.HasPrefix(checkLine, r.Comment) { 42 // yay, skip this line 43 continue 44 } 45 r.buf.WriteString(line) 46 } 47 if err != nil { 48 if r.buf.Len() > 0 { 49 return r.buf.Read(p) 50 } 51 return 0, err 52 } 53 } 54 }