|
|
@ -2,136 +2,190 @@ package keybase |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"samhofi.us/x/keybase/types/keybase1" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// kvAPIOut sends a JSON request to the kvstore API and returns its response.
|
|
|
|
// KVListNamespaces returns all namespaces for a team
|
|
|
|
func kvAPIOut(k *Keybase, kv KVAPI) (KVAPI, error) { |
|
|
|
func (k *Keybase) KVListNamespaces(team *string) (keybase1.KVListNamespaceResult, error) { |
|
|
|
jsonBytes, _ := json.Marshal(kv) |
|
|
|
type res struct { |
|
|
|
|
|
|
|
Result keybase1.KVListNamespaceResult `json:"result"` |
|
|
|
|
|
|
|
Error *Error `json:"error"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var r res |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
arg := newKVArg("list", KVOptions{ |
|
|
|
|
|
|
|
Team: team, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonBytes, _ := json.Marshal(arg) |
|
|
|
|
|
|
|
|
|
|
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes)) |
|
|
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return KVAPI{}, err |
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var r KVAPI |
|
|
|
err = json.Unmarshal(cmdOut, &r) |
|
|
|
if err := json.Unmarshal(cmdOut, &r); err != nil { |
|
|
|
if err != nil { |
|
|
|
return KVAPI{}, err |
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if r.Error != nil { |
|
|
|
if r.Error != nil { |
|
|
|
return KVAPI{}, errors.New(r.Error.Message) |
|
|
|
return r.Result, fmt.Errorf("%s", r.Error.Message) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return r, nil |
|
|
|
return r.Result, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Namespaces returns all namespaces for a team
|
|
|
|
// KVListKeys returns all non-deleted keys for a namespace
|
|
|
|
func (kv KV) Namespaces() (KVAPI, error) { |
|
|
|
func (k *Keybase) KVListKeys(team *string, namespace string) (keybase1.KVListEntryResult, error) { |
|
|
|
m := KVAPI{ |
|
|
|
type res struct { |
|
|
|
Params: &kvParams{}, |
|
|
|
Result keybase1.KVListEntryResult `json:"result"` |
|
|
|
} |
|
|
|
Error *Error `json:"error"` |
|
|
|
m.Params.Options = kvOptions{ |
|
|
|
|
|
|
|
Team: kv.Team, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var r res |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
arg := newKVArg("list", KVOptions{ |
|
|
|
|
|
|
|
Team: team, |
|
|
|
|
|
|
|
Namespace: &namespace, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
m.Method = "list" |
|
|
|
jsonBytes, _ := json.Marshal(arg) |
|
|
|
|
|
|
|
|
|
|
|
r, err := kvAPIOut(kv.keybase, m) |
|
|
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return r, err |
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
return r, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keys returns all non-deleted keys for a namespace
|
|
|
|
err = json.Unmarshal(cmdOut, &r) |
|
|
|
func (kv KV) Keys(namespace string) (KVAPI, error) { |
|
|
|
if err != nil { |
|
|
|
m := KVAPI{ |
|
|
|
return r.Result, err |
|
|
|
Params: &kvParams{}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
m.Params.Options = kvOptions{ |
|
|
|
|
|
|
|
Team: kv.Team, |
|
|
|
if r.Error != nil { |
|
|
|
Namespace: namespace, |
|
|
|
return r.Result, fmt.Errorf("%s", r.Error.Message) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
m.Method = "list" |
|
|
|
return r.Result, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r, err := kvAPIOut(kv.keybase, m) |
|
|
|
// KVGet returns an entry
|
|
|
|
if err != nil { |
|
|
|
func (k *Keybase) KVGet(team *string, namespace string, key string) (keybase1.KVGetResult, error) { |
|
|
|
return r, err |
|
|
|
type res struct { |
|
|
|
|
|
|
|
Result keybase1.KVGetResult `json:"result"` |
|
|
|
|
|
|
|
Error *Error `json:"error"` |
|
|
|
} |
|
|
|
} |
|
|
|
return r, nil |
|
|
|
var r res |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
arg := newKVArg("get", KVOptions{ |
|
|
|
|
|
|
|
Team: team, |
|
|
|
|
|
|
|
Namespace: &namespace, |
|
|
|
|
|
|
|
EntryKey: &key, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// Get returns an entry
|
|
|
|
jsonBytes, _ := json.Marshal(arg) |
|
|
|
func (kv KV) Get(namespace string, key string, revision ...uint) (KVAPI, error) { |
|
|
|
|
|
|
|
m := KVAPI{ |
|
|
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes)) |
|
|
|
Params: &kvParams{}, |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
m.Params.Options = kvOptions{ |
|
|
|
|
|
|
|
Team: kv.Team, |
|
|
|
err = json.Unmarshal(cmdOut, &r) |
|
|
|
Namespace: namespace, |
|
|
|
if err != nil { |
|
|
|
EntryKey: key, |
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(revision) > 0 { |
|
|
|
if r.Error != nil { |
|
|
|
m.Params.Options.Revision = revision[0] |
|
|
|
return r.Result, fmt.Errorf("%s", r.Error.Message) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
m.Method = "get" |
|
|
|
return r.Result, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r, err := kvAPIOut(kv.keybase, m) |
|
|
|
// KVPutWithRevision puts an entry, specifying the revision number
|
|
|
|
if err != nil { |
|
|
|
func (k *Keybase) KVPutWithRevision(team *string, namespace string, key string, value string, revision int) (keybase1.KVPutResult, error) { |
|
|
|
return r, err |
|
|
|
type res struct { |
|
|
|
|
|
|
|
Result keybase1.KVPutResult `json:"result"` |
|
|
|
|
|
|
|
Error *Error `json:"error"` |
|
|
|
} |
|
|
|
} |
|
|
|
return r, nil |
|
|
|
var r res |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Put adds an entry
|
|
|
|
opts := KVOptions{ |
|
|
|
func (kv KV) Put(namespace string, key string, value string, revision ...uint) (KVAPI, error) { |
|
|
|
Team: team, |
|
|
|
m := KVAPI{ |
|
|
|
Namespace: &namespace, |
|
|
|
Params: &kvParams{}, |
|
|
|
EntryKey: &key, |
|
|
|
|
|
|
|
EntryValue: &value, |
|
|
|
} |
|
|
|
} |
|
|
|
m.Params.Options = kvOptions{ |
|
|
|
if revision != 0 { |
|
|
|
Team: kv.Team, |
|
|
|
opts.Revision = &revision |
|
|
|
Namespace: namespace, |
|
|
|
|
|
|
|
EntryKey: key, |
|
|
|
|
|
|
|
EntryValue: value, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(revision) > 0 { |
|
|
|
arg := newKVArg("put", opts) |
|
|
|
m.Params.Options.Revision = revision[0] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m.Method = "put" |
|
|
|
jsonBytes, _ := json.Marshal(arg) |
|
|
|
|
|
|
|
|
|
|
|
r, err := kvAPIOut(kv.keybase, m) |
|
|
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return r, err |
|
|
|
return r.Result, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(cmdOut, &r) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
return r, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Delete removes an entry
|
|
|
|
if r.Error != nil { |
|
|
|
func (kv KV) Delete(namespace string, key string, revision ...uint) (KVAPI, error) { |
|
|
|
return r.Result, fmt.Errorf("%s", r.Error.Message) |
|
|
|
m := KVAPI{ |
|
|
|
|
|
|
|
Params: &kvParams{}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
m.Params.Options = kvOptions{ |
|
|
|
|
|
|
|
Team: kv.Team, |
|
|
|
return r.Result, nil |
|
|
|
Namespace: namespace, |
|
|
|
} |
|
|
|
EntryKey: key, |
|
|
|
|
|
|
|
|
|
|
|
// KVPut puts an entry
|
|
|
|
|
|
|
|
func (k *Keybase) KVPut(team *string, namespace string, key string, value string) (keybase1.KVPutResult, error) { |
|
|
|
|
|
|
|
return k.KVPutWithRevision(team, namespace, key, value, 0) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// KVDeleteWithRevision deletes an entry, specifying the revision number
|
|
|
|
|
|
|
|
func (k *Keybase) KVDeleteWithRevision(team *string, namespace string, key string, revision int) (keybase1.KVDeleteEntryResult, error) { |
|
|
|
|
|
|
|
type res struct { |
|
|
|
|
|
|
|
Result keybase1.KVDeleteEntryResult `json:"result"` |
|
|
|
|
|
|
|
Error *Error `json:"error"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var r res |
|
|
|
|
|
|
|
|
|
|
|
if len(revision) > 0 { |
|
|
|
opts := KVOptions{ |
|
|
|
m.Params.Options.Revision = revision[0] |
|
|
|
Team: team, |
|
|
|
|
|
|
|
Namespace: &namespace, |
|
|
|
|
|
|
|
EntryKey: &key, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if revision != 0 { |
|
|
|
|
|
|
|
opts.Revision = &revision |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
arg := newKVArg("del", opts) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonBytes, _ := json.Marshal(arg) |
|
|
|
|
|
|
|
|
|
|
|
m.Method = "del" |
|
|
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes)) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return r.Result, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r, err := kvAPIOut(kv.keybase, m) |
|
|
|
err = json.Unmarshal(cmdOut, &r) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return r, err |
|
|
|
return r.Result, err |
|
|
|
} |
|
|
|
} |
|
|
|
return r, nil |
|
|
|
|
|
|
|
|
|
|
|
if r.Error != nil { |
|
|
|
|
|
|
|
return r.Result, fmt.Errorf("%s", r.Error.Message) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return r.Result, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// KVDelete deletes an entry
|
|
|
|
|
|
|
|
func (k *Keybase) KVDelete(team *string, namespace string, key string) (keybase1.KVDeleteEntryResult, error) { |
|
|
|
|
|
|
|
return k.KVDeleteWithRevision(team, namespace, key, 0) |
|
|
|
} |
|
|
|
} |
|
|
|