Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #97 from benbjohnson/cli
Browse files Browse the repository at this point in the history
Refactor bolt commands into individual files.
  • Loading branch information
benbjohnson committed Mar 28, 2014
2 parents b10aa18 + fea388b commit 7dafeaa
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 316 deletions.
33 changes: 33 additions & 0 deletions cmd/bolt/buckets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"os"

"github.com/boltdb/bolt"
)

// Buckets prints a list of all buckets.
func Buckets(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
fatal(err)
return
}

db, err := bolt.Open(path, 0600)
if err != nil {
fatal(err)
return
}
defer db.Close()

err = db.View(func(tx *bolt.Tx) error {
for _, b := range tx.Buckets() {
println(b.Name())
}
return nil
})
if err != nil {
fatal(err)
return
}
}
31 changes: 31 additions & 0 deletions cmd/bolt/buckets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main_test

import (
"testing"

"github.com/boltdb/bolt"
. "github.com/boltdb/bolt/cmd/bolt"
"github.com/stretchr/testify/assert"
)

// Ensure that a list of buckets can be retrieved.
func TestBuckets(t *testing.T) {
SetTestMode(true)
open(func(db *bolt.DB) {
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucket("woojits")
tx.CreateBucket("widgets")
tx.CreateBucket("whatchits")
return nil
})
output := run("buckets", db.Path())
assert.Equal(t, "whatchits\nwidgets\nwoojits", output)
})
}

// Ensure that an error is reported if the database is not found.
func TestBucketsDBNotFound(t *testing.T) {
SetTestMode(true)
output := run("buckets", "no/such/db")
assert.Equal(t, "stat no/such/db: no such file or directory", output)
}
45 changes: 45 additions & 0 deletions cmd/bolt/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"os"

"github.com/boltdb/bolt"
)

// Get retrieves the value for a given bucket/key.
func Get(path, name, key string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
fatal(err)
return
}

db, err := bolt.Open(path, 0600)
if err != nil {
fatal(err)
return
}
defer db.Close()

err = db.View(func(tx *bolt.Tx) error {
// Find bucket.
b := tx.Bucket(name)
if b == nil {
fatalf("bucket not found: %s", name)
return nil
}

// Find value for a given key.
value := b.Get([]byte(key))
if value == nil {
fatalf("key not found: %s", key)
return nil
}

println(string(value))
return nil
})
if err != nil {
fatal(err)
return
}
}
51 changes: 51 additions & 0 deletions cmd/bolt/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main_test

import (
"testing"

"github.com/boltdb/bolt"
. "github.com/boltdb/bolt/cmd/bolt"
"github.com/stretchr/testify/assert"
)

// Ensure that a value can be retrieved from the CLI.
func TestGet(t *testing.T) {
SetTestMode(true)
open(func(db *bolt.DB) {
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucket("widgets")
tx.Bucket("widgets").Put([]byte("foo"), []byte("bar"))
return nil
})
output := run("get", db.Path(), "widgets", "foo")
assert.Equal(t, "bar", output)
})
}

// Ensure that an error is reported if the database is not found.
func TestGetDBNotFound(t *testing.T) {
SetTestMode(true)
output := run("get", "no/such/db", "widgets", "foo")
assert.Equal(t, "stat no/such/db: no such file or directory", output)
}

// Ensure that an error is reported if the bucket is not found.
func TestGetBucketNotFound(t *testing.T) {
SetTestMode(true)
open(func(db *bolt.DB) {
output := run("get", db.Path(), "widgets", "foo")
assert.Equal(t, "bucket not found: widgets", output)
})
}

// Ensure that an error is reported if the key is not found.
func TestGetKeyNotFound(t *testing.T) {
SetTestMode(true)
open(func(db *bolt.DB) {
db.Update(func(tx *bolt.Tx) error {
return tx.CreateBucket("widgets")
})
output := run("get", db.Path(), "widgets", "foo")
assert.Equal(t, "key not found: foo", output)
})
}
41 changes: 41 additions & 0 deletions cmd/bolt/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"os"

"github.com/boltdb/bolt"
)

// Keys retrieves a list of keys for a given bucket.
func Keys(path, name string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
fatal(err)
return
}

db, err := bolt.Open(path, 0600)
if err != nil {
fatal(err)
return
}
defer db.Close()

err = db.View(func(tx *bolt.Tx) error {
// Find bucket.
b := tx.Bucket(name)
if b == nil {
fatalf("bucket not found: %s", name)
return nil
}

// Iterate over each key.
return b.ForEach(func(key, _ []byte) error {
println(string(key))
return nil
})
})
if err != nil {
fatal(err)
return
}
}
41 changes: 41 additions & 0 deletions cmd/bolt/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main_test

import (
"testing"

"github.com/boltdb/bolt"
. "github.com/boltdb/bolt/cmd/bolt"
"github.com/stretchr/testify/assert"
)

// Ensure that a list of keys can be retrieved for a given bucket.
func TestKeys(t *testing.T) {
SetTestMode(true)
open(func(db *bolt.DB) {
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucket("widgets")
tx.Bucket("widgets").Put([]byte("0002"), []byte(""))
tx.Bucket("widgets").Put([]byte("0001"), []byte(""))
tx.Bucket("widgets").Put([]byte("0003"), []byte(""))
return nil
})
output := run("keys", db.Path(), "widgets")
assert.Equal(t, "0001\n0002\n0003", output)
})
}

// Ensure that an error is reported if the database is not found.
func TestKeysDBNotFound(t *testing.T) {
SetTestMode(true)
output := run("keys", "no/such/db", "widgets")
assert.Equal(t, "stat no/such/db: no such file or directory", output)
}

// Ensure that an error is reported if the bucket is not found.
func TestKeysBucketNotFound(t *testing.T) {
SetTestMode(true)
open(func(db *bolt.DB) {
output := run("keys", db.Path(), "widgets")
assert.Equal(t, "bucket not found: widgets", output)
})
}

0 comments on commit 7dafeaa

Please sign in to comment.