init: basic tools
This commit is contained in:
37
input_stdin.go
Normal file
37
input_stdin.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StdinReader reads from stdin
|
||||
type StdinReader struct {
|
||||
scanner bufio.Scanner
|
||||
}
|
||||
|
||||
func NewStdinReader() *StdinReader {
|
||||
return &StdinReader{
|
||||
scanner: *bufio.NewScanner(os.Stdin),
|
||||
}
|
||||
}
|
||||
|
||||
// Read reads from stdin until \n character
|
||||
// then splits result at "," separator and returns
|
||||
// the resulting slice.
|
||||
// It returns EOF when nothing left to read.
|
||||
func (s *StdinReader) Read(_ context.Context) ([]string, error) {
|
||||
if !s.scanner.Scan() {
|
||||
err := s.scanner.Err()
|
||||
if err == nil {
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strings.Split(s.scanner.Text(), ","), nil
|
||||
}
|
Reference in New Issue
Block a user