mirror of https://github.com/etcd-io/bbolt.git
Add 'bolt get'.
parent
e86296ede7
commit
e8d3ae6287
|
@ -0,0 +1,115 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/codegangsta/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0)
|
||||
NewApp().Run(os.Args)
|
||||
}
|
||||
|
||||
// NewApp creates an Application instance.
|
||||
func NewApp() *cli.App {
|
||||
app := cli.NewApp()
|
||||
app.Name = "bolt"
|
||||
app.Usage = "BoltDB toolkit"
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
Name: "get",
|
||||
Usage: "retrieve a value for given key",
|
||||
Action: Get,
|
||||
},
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
||||
// Get retrieves the value for a given bucket/key.
|
||||
func Get(c *cli.Context) {
|
||||
path, name, key := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2)
|
||||
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.With(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
|
||||
}
|
||||
|
||||
logger.Println(string(value))
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var logger = log.New(os.Stderr, "", 0)
|
||||
var logBuffer *bytes.Buffer
|
||||
|
||||
func fatal(v ...interface{}) {
|
||||
logger.Print(v...)
|
||||
if !testMode {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func fatalf(format string, v ...interface{}) {
|
||||
logger.Printf(format, v...)
|
||||
if !testMode {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func fatalln(v ...interface{}) {
|
||||
logger.Println(v...)
|
||||
if !testMode {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// LogBuffer returns the contents of the log.
|
||||
// This only works while the CLI is in test mode.
|
||||
func LogBuffer() string {
|
||||
if logBuffer != nil {
|
||||
return logBuffer.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var testMode bool
|
||||
|
||||
// SetTestMode sets whether the CLI is running in test mode and resets the logger.
|
||||
func SetTestMode(value bool) {
|
||||
testMode = value
|
||||
if testMode {
|
||||
logBuffer = bytes.NewBuffer(nil)
|
||||
logger = log.New(logBuffer, "", 0)
|
||||
} else {
|
||||
logger = log.New(os.Stderr, "", 0)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package main_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"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.Do(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.Do(func(tx *bolt.Tx) error {
|
||||
return tx.CreateBucket("widgets")
|
||||
})
|
||||
output := run("get", db.Path(), "widgets", "foo")
|
||||
assert.Equal(t, "key not found: foo", output)
|
||||
})
|
||||
}
|
||||
|
||||
// open creates and opens a Bolt database in the temp directory.
|
||||
func open(fn func(*bolt.DB)) {
|
||||
f, _ := ioutil.TempFile("", "bolt-")
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
defer os.RemoveAll(f.Name())
|
||||
|
||||
db, err := bolt.Open(f.Name(), 0600)
|
||||
if err != nil {
|
||||
panic("db open error: " + err.Error())
|
||||
}
|
||||
fn(db)
|
||||
}
|
||||
|
||||
// run executes a command against the CLI and returns the output.
|
||||
func run(args ...string) string {
|
||||
args = append([]string{"bolt"}, args...)
|
||||
NewApp().Run(args)
|
||||
return strings.TrimSpace(LogBuffer())
|
||||
}
|
Loading…
Reference in New Issue