small refactor

This commit is contained in:
Stephen McQuay 2016-08-20 21:43:37 -07:00
parent 382c395900
commit e4d9174597
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D

35
main.go
View File

@ -10,6 +10,7 @@ import (
)
const usage = "bdb <(w)rite|(r)ead> <db>"
const queued = "queued"
func main() {
log.SetFlags(log.Lshortfile)
@ -30,22 +31,22 @@ func main() {
panic(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("queue"))
if err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
switch os.Args[1] {
case "write", "w":
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(queued))
if err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
err := db.Update(func(tx *bolt.Tx) error {
s := bufio.NewScanner(os.Stdin)
b := tx.Bucket([]byte("queue"))
b := tx.Bucket([]byte(queued))
for s.Scan() {
err := b.Put(s.Bytes(), s.Bytes())
if err != nil {
@ -58,8 +59,18 @@ func main() {
panic(err)
}
case "read", "r":
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(queued))
if err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("queue"))
b := tx.Bucket([]byte(queued))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {