elvish/pkg/store/shared_var.go
Kurtis Rader eb2a792301 Fix some lint warnings
In addition to an uncontroversial spelling fix this addresses several,
related, warnings produced by the `golint` tool. In general I agree with
golint that unnecessary "else" blocks should be avoided. So this change
refactors those cases.

Note: I recognize that `golint` is deprecated (see
https://github.com/golang/go/issues/38968) since it is no longer being
maintained and there is controversy about its set of warnings. Nonetheless,
it appears that the warnings it emits for this project are all reasonable
and actionable with one potential exception: the naming of the `map_`
method in pkg/eval/compile_value.go.
2020-08-01 23:07:50 +01:00

49 lines
1.1 KiB
Go

package store
import (
"errors"
bolt "go.etcd.io/bbolt"
)
// 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 {
return ErrNoSharedVar
}
value = string(v)
return nil
})
return value, err
}
// 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))
})
}
// 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))
})
}