elvish/pkg/store/shared_var.go

49 lines
1.1 KiB
Go
Raw Normal View History

package store
import (
"errors"
bolt "go.etcd.io/bbolt"
)
2019-12-27 01:53:50 +08:00
// ErrNoSharedVar is returned by Store.SharedVar when there is no such variable.
var ErrNoSharedVar = errors.New("no such shared variable")
func init() {
initDB["initialize shared variable table"] = func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucketSharedVar))
return err
}
}
// SharedVar gets the value of a shared variable.
func (s *dbStore) SharedVar(n string) (string, error) {
var value string
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketSharedVar))
v := b.Get([]byte(n))
if v == nil {
2019-12-27 01:53:50 +08:00
return ErrNoSharedVar
}
value = string(v)
return nil
})
return value, err
}
2017-05-22 06:54:04 +08:00
// SetSharedVar sets the value of a shared variable.
func (s *dbStore) SetSharedVar(n, v string) error {
return s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketSharedVar))
return b.Put([]byte(n), []byte(v))
})
}
2017-05-22 06:54:04 +08:00
// DelSharedVar deletes a shared variable.
func (s *dbStore) DelSharedVar(n string) error {
return s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketSharedVar))
return b.Delete([]byte(n))
})
}