Skip to content
Snippets Groups Projects
Commit bff49bef authored by ale's avatar ale
Browse files

Add function to read book IDs from stdin

parent 9e122e3d
No related branches found
No related tags found
No related merge requests found
package liber
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
)
func (db *Database) Refine(dir string) error {
......@@ -29,3 +32,22 @@ func (db *Database) ListBooks(w io.Writer, matchFuncs ...func(book *Book) bool)
return nil
})
}
// WithBookIDs calls a function on books whose IDs are read from the
// specified io.Reader.
func (db *Database) WithBookIDs(r io.Reader, f func(book *Book) error) error {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
id := ParseID(string(bytes.TrimSpace(scanner.Bytes())))
book, err := db.GetBook(id)
if err != nil {
log.Printf("error: no such book %s", id)
continue
}
if err := f(book); err != nil {
log.Printf("error: %s: %v", id, err)
// Stop?
}
}
return scanner.Err()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment