You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
2.4 KiB
138 lines
2.4 KiB
5 years ago
|
package keybase
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
)
|
||
|
|
||
|
// kvAPIOut sends a JSON request to the kvstore API and returns its response.
|
||
|
func kvAPIOut(k *Keybase, kv KVAPI) (KVAPI, error) {
|
||
|
jsonBytes, _ := json.Marshal(kv)
|
||
|
|
||
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes))
|
||
|
if err != nil {
|
||
|
return KVAPI{}, err
|
||
|
}
|
||
|
|
||
|
var r KVAPI
|
||
|
if err := json.Unmarshal(cmdOut, &r); err != nil {
|
||
|
return KVAPI{}, err
|
||
|
}
|
||
|
|
||
|
if r.Error != nil {
|
||
|
return KVAPI{}, errors.New(r.Error.Message)
|
||
|
}
|
||
|
|
||
|
return r, nil
|
||
|
}
|
||
|
|
||
|
// Namespaces returns all namespaces for a team
|
||
|
func (kv KV) Namespaces() (KVAPI, error) {
|
||
|
m := KVAPI{
|
||
|
Params: &kvParams{},
|
||
|
}
|
||
|
m.Params.Options = kvOptions{
|
||
|
Team: kv.Team,
|
||
|
}
|
||
|
|
||
|
m.Method = "list"
|
||
|
|
||
|
r, err := kvAPIOut(kv.keybase, m)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
return r, nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// Keys returns all non-deleted keys for a namespace
|
||
|
func (kv KV) Keys(namespace string) (KVAPI, error) {
|
||
|
m := KVAPI{
|
||
|
Params: &kvParams{},
|
||
|
}
|
||
|
m.Params.Options = kvOptions{
|
||
|
Team: kv.Team,
|
||
|
Namespace: namespace,
|
||
|
}
|
||
|
|
||
|
m.Method = "list"
|
||
|
|
||
|
r, err := kvAPIOut(kv.keybase, m)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
return r, nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// Get returns an entry
|
||
5 years ago
|
func (kv KV) Get(namespace string, key string, revision ...uint) (KVAPI, error) {
|
||
5 years ago
|
m := KVAPI{
|
||
|
Params: &kvParams{},
|
||
|
}
|
||
|
m.Params.Options = kvOptions{
|
||
|
Team: kv.Team,
|
||
|
Namespace: namespace,
|
||
|
EntryKey: key,
|
||
|
}
|
||
|
|
||
5 years ago
|
if len(revision) > 0 {
|
||
|
m.Params.Options.Revision = revision[0]
|
||
|
}
|
||
|
|
||
5 years ago
|
m.Method = "get"
|
||
|
|
||
|
r, err := kvAPIOut(kv.keybase, m)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
return r, nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// Put adds an entry
|
||
5 years ago
|
func (kv KV) Put(namespace string, key string, value string, revision ...uint) (KVAPI, error) {
|
||
5 years ago
|
m := KVAPI{
|
||
|
Params: &kvParams{},
|
||
|
}
|
||
|
m.Params.Options = kvOptions{
|
||
|
Team: kv.Team,
|
||
|
Namespace: namespace,
|
||
|
EntryKey: key,
|
||
|
EntryValue: value,
|
||
|
}
|
||
|
|
||
5 years ago
|
if len(revision) > 0 {
|
||
|
m.Params.Options.Revision = revision[0]
|
||
|
}
|
||
|
|
||
5 years ago
|
m.Method = "put"
|
||
|
|
||
|
r, err := kvAPIOut(kv.keybase, m)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
return r, nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// Delete removes an entry
|
||
5 years ago
|
func (kv KV) Delete(namespace string, key string, revision ...uint) (KVAPI, error) {
|
||
5 years ago
|
m := KVAPI{
|
||
|
Params: &kvParams{},
|
||
|
}
|
||
|
m.Params.Options = kvOptions{
|
||
|
Team: kv.Team,
|
||
|
Namespace: namespace,
|
||
|
EntryKey: key,
|
||
|
}
|
||
|
|
||
5 years ago
|
if len(revision) > 0 {
|
||
|
m.Params.Options.Revision = revision[0]
|
||
|
}
|
||
|
|
||
5 years ago
|
m.Method = "del"
|
||
|
|
||
|
r, err := kvAPIOut(kv.keybase, m)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
return r, nil
|
||
|
}
|