From 50e04a29aeebe461fe9d00ec780246ac3a222c09 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 8 May 2014 08:43:18 -0600 Subject: [PATCH] Add 'bolt info'. --- cmd/bolt/info.go | 26 ++++++++++++++++++++++++++ cmd/bolt/info_test.go | 32 ++++++++++++++++++++++++++++++++ cmd/bolt/main.go | 8 ++++++++ 3 files changed, 66 insertions(+) create mode 100644 cmd/bolt/info.go create mode 100644 cmd/bolt/info_test.go diff --git a/cmd/bolt/info.go b/cmd/bolt/info.go new file mode 100644 index 0000000..1e9e0d8 --- /dev/null +++ b/cmd/bolt/info.go @@ -0,0 +1,26 @@ +package main + +import ( + "os" + + "github.com/boltdb/bolt" +) + +// Info prints basic information about a database. +func Info(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() + + // Print basic database info. + var info = db.Info() + printf("Page Size: %d", info.PageSize) +} diff --git a/cmd/bolt/info_test.go b/cmd/bolt/info_test.go new file mode 100644 index 0000000..668cc61 --- /dev/null +++ b/cmd/bolt/info_test.go @@ -0,0 +1,32 @@ +package main_test + +import ( + "testing" + + "github.com/boltdb/bolt" + . "github.com/boltdb/bolt/cmd/bolt" + "github.com/stretchr/testify/assert" +) + +// Ensure that a database info can be printed. +func TestInfo(t *testing.T) { + SetTestMode(true) + open(func(db *bolt.DB, path string) { + db.Update(func(tx *bolt.Tx) error { + tx.CreateBucket([]byte("widgets")) + b := tx.Bucket([]byte("widgets")) + b.Put([]byte("foo"), []byte("0000")) + return nil + }) + db.Close() + output := run("info", path) + assert.Equal(t, `Page Size: 4096`, output) + }) +} + +// Ensure that an error is reported if the database is not found. +func TestInfo_NotFound(t *testing.T) { + SetTestMode(true) + output := run("info", "no/such/db") + assert.Equal(t, "stat no/such/db: no such file or directory", output) +} diff --git a/cmd/bolt/main.go b/cmd/bolt/main.go index 1af2636..3397042 100644 --- a/cmd/bolt/main.go +++ b/cmd/bolt/main.go @@ -25,6 +25,14 @@ func NewApp() *cli.App { app.Usage = "BoltDB toolkit" app.Version = fmt.Sprintf("0.1.0 (%s %s)", branch, commit) app.Commands = []cli.Command{ + { + Name: "info", + Usage: "Print basic information about a database", + Action: func(c *cli.Context) { + path := c.Args().Get(0) + Info(path) + }, + }, { Name: "get", Usage: "Retrieve a value for given key in a bucket",