This repository has been archived by the owner on Mar 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from benbjohnson/cli
Refactor bolt commands into individual files.
- Loading branch information
Showing
11 changed files
with
395 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
Oops, something went wrong.