Codebase list golang-github-boltdb-bolt / c22d2e0
Remove 'bolt set'. This commit removes the 'set' command in the Bolt CLI. It proved to not be very useful so there's no point in keeping the extra code around. Ben Johnson 10 years ago
3 changed file(s) with 0 addition(s) and 87 deletion(s). Raw diff Collapse all Expand all
3030 Action: func(c *cli.Context) {
3131 path, name, key := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2)
3232 Get(path, name, key)
33 },
34 },
35 {
36 Name: "set",
37 Usage: "Sets a value for given key in a bucket",
38 Action: func(c *cli.Context) {
39 path, name, key, value := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2), c.Args().Get(3)
40 Set(path, name, key, value)
4133 },
4234 },
4335 {
+0
-39
cmd/bolt/set.go less more
0 package main
1
2 import (
3 "os"
4
5 "github.com/boltdb/bolt"
6 )
7
8 // Set sets the value for a given key in a bucket.
9 func Set(path, name, key, value string) {
10 if _, err := os.Stat(path); os.IsNotExist(err) {
11 fatal(err)
12 return
13 }
14
15 db, err := bolt.Open(path, 0600)
16 if err != nil {
17 fatal(err)
18 return
19 }
20 defer db.Close()
21
22 err = db.Update(func(tx *bolt.Tx) error {
23
24 // Find bucket.
25 b := tx.Bucket([]byte(name))
26 if b == nil {
27 fatalf("bucket not found: %s", name)
28 return nil
29 }
30
31 // Set value for a given key.
32 return b.Put([]byte(key), []byte(value))
33 })
34 if err != nil {
35 fatal(err)
36 return
37 }
38 }
+0
-40
cmd/bolt/set_test.go less more
0 package main_test
1
2 import (
3 "testing"
4
5 "github.com/boltdb/bolt"
6 . "github.com/boltdb/bolt/cmd/bolt"
7 "github.com/stretchr/testify/assert"
8 )
9
10 // Ensure that a value can be set from the CLI.
11 func TestSet(t *testing.T) {
12 SetTestMode(true)
13 open(func(db *bolt.DB, path string) {
14 db.Update(func(tx *bolt.Tx) error {
15 tx.CreateBucket([]byte("widgets"))
16 return nil
17 })
18 db.Close()
19 assert.Equal(t, "", run("set", path, "widgets", "foo", "bar"))
20 assert.Equal(t, "bar", run("get", path, "widgets", "foo"))
21 })
22 }
23
24 // Ensure that an error is reported if the database is not found.
25 func TestSetDBNotFound(t *testing.T) {
26 SetTestMode(true)
27 output := run("set", "no/such/db", "widgets", "foo", "bar")
28 assert.Equal(t, "stat no/such/db: no such file or directory", output)
29 }
30
31 // Ensure that an error is reported if the bucket is not found.
32 func TestSetBucketNotFound(t *testing.T) {
33 SetTestMode(true)
34 open(func(db *bolt.DB, path string) {
35 db.Close()
36 output := run("set", path, "widgets", "foo", "bar")
37 assert.Equal(t, "bucket not found: widgets", output)
38 })
39 }