readUntil the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. Unlike `bufio`, it only reads until the delimiter and no further bytes. If readUntil encounters an error before finding a delimiter, it returns the data read before the err
(r io.Reader, delim byte)
| 187 | // before the error and the error itself (often io.EOF). readUntil returns err != nil if and only if |
| 188 | // the returned data does not end in delim. |
| 189 | func readUntil(r io.Reader, delim byte) (string, error) { |
| 190 | var ( |
| 191 | have []byte |
| 192 | b = make([]byte, 1) |
| 193 | ) |
| 194 | for { |
| 195 | n, err := r.Read(b) |
| 196 | if n > 0 { |
| 197 | have = append(have, b[0]) |
| 198 | if b[0] == delim { |
| 199 | // match `bufio` in that we only return non-nil if we didn't find the delimiter, |
| 200 | // regardless of whether we also erred. |
| 201 | return string(have), nil |
| 202 | } |
| 203 | } |
| 204 | if err != nil { |
| 205 | return string(have), err |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // readSecretInput reads secret input from the terminal rune-by-rune, |
| 211 | // masking each character with an asterisk. |
no test coverage detected