Convert keystore funcs to use avdl compiled types
This commit is contained in:
@ -89,14 +89,6 @@ func (k *Keybase) NewTeam(name string) Team {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewKV returns a new KV instance
|
|
||||||
func (k *Keybase) NewKV(team string) KV {
|
|
||||||
return KV{
|
|
||||||
keybase: k,
|
|
||||||
Team: team,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWallet returns a new Wallet instance
|
// NewWallet returns a new Wallet instance
|
||||||
func (k *Keybase) NewWallet() Wallet {
|
func (k *Keybase) NewWallet() Wallet {
|
||||||
return Wallet{
|
return Wallet{
|
||||||
|
|||||||
234
kvstore.go
234
kvstore.go
@ -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
|
||||||
|
|
||||||
m.Method = "list"
|
arg := newKVArg("list", KVOptions{
|
||||||
|
Team: team,
|
||||||
|
Namespace: &namespace,
|
||||||
|
})
|
||||||
|
|
||||||
r, err := kvAPIOut(kv.keybase, m)
|
jsonBytes, _ := json.Marshal(arg)
|
||||||
|
|
||||||
|
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
|
|
||||||
func (kv KV) Keys(namespace string) (KVAPI, error) {
|
|
||||||
m := KVAPI{
|
|
||||||
Params: &kvParams{},
|
|
||||||
}
|
|
||||||
m.Params.Options = kvOptions{
|
|
||||||
Team: kv.Team,
|
|
||||||
Namespace: namespace,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Method = "list"
|
err = json.Unmarshal(cmdOut, &r)
|
||||||
|
|
||||||
r, err := kvAPIOut(kv.keybase, m)
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns an entry
|
// KVGet returns an entry
|
||||||
func (kv KV) Get(namespace string, key string, revision ...uint) (KVAPI, error) {
|
func (k *Keybase) KVGet(team *string, namespace string, key string) (keybase1.KVGetResult, error) {
|
||||||
m := KVAPI{
|
type res struct {
|
||||||
Params: &kvParams{},
|
Result keybase1.KVGetResult `json:"result"`
|
||||||
}
|
Error *Error `json:"error"`
|
||||||
m.Params.Options = kvOptions{
|
|
||||||
Team: kv.Team,
|
|
||||||
Namespace: namespace,
|
|
||||||
EntryKey: key,
|
|
||||||
}
|
}
|
||||||
|
var r res
|
||||||
|
|
||||||
if len(revision) > 0 {
|
arg := newKVArg("get", KVOptions{
|
||||||
m.Params.Options.Revision = revision[0]
|
Team: team,
|
||||||
}
|
Namespace: &namespace,
|
||||||
|
EntryKey: &key,
|
||||||
|
})
|
||||||
|
|
||||||
m.Method = "get"
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put adds an entry
|
|
||||||
func (kv KV) Put(namespace string, key string, value string, revision ...uint) (KVAPI, error) {
|
|
||||||
m := KVAPI{
|
|
||||||
Params: &kvParams{},
|
|
||||||
}
|
|
||||||
m.Params.Options = kvOptions{
|
|
||||||
Team: kv.Team,
|
|
||||||
Namespace: namespace,
|
|
||||||
EntryKey: key,
|
|
||||||
EntryValue: value,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(revision) > 0 {
|
err = json.Unmarshal(cmdOut, &r)
|
||||||
m.Params.Options.Revision = revision[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Method = "put"
|
|
||||||
|
|
||||||
r, err := kvAPIOut(kv.keybase, m)
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes an entry
|
// KVPutWithRevision puts an entry, specifying the revision number
|
||||||
func (kv KV) Delete(namespace string, key string, revision ...uint) (KVAPI, error) {
|
func (k *Keybase) KVPutWithRevision(team *string, namespace string, key string, value string, revision int) (keybase1.KVPutResult, error) {
|
||||||
m := KVAPI{
|
type res struct {
|
||||||
Params: &kvParams{},
|
Result keybase1.KVPutResult `json:"result"`
|
||||||
|
Error *Error `json:"error"`
|
||||||
}
|
}
|
||||||
m.Params.Options = kvOptions{
|
var r res
|
||||||
Team: kv.Team,
|
|
||||||
Namespace: namespace,
|
opts := KVOptions{
|
||||||
EntryKey: key,
|
Team: team,
|
||||||
|
Namespace: &namespace,
|
||||||
|
EntryKey: &key,
|
||||||
|
EntryValue: &value,
|
||||||
|
}
|
||||||
|
if revision != 0 {
|
||||||
|
opts.Revision = &revision
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(revision) > 0 {
|
arg := newKVArg("put", opts)
|
||||||
m.Params.Options.Revision = revision[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Method = "del"
|
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
|
|
||||||
|
err = json.Unmarshal(cmdOut, &r)
|
||||||
|
if err != nil {
|
||||||
|
return r.Result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Error != nil {
|
||||||
|
return r.Result, fmt.Errorf("%s", r.Error.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
opts := KVOptions{
|
||||||
|
Team: team,
|
||||||
|
Namespace: &namespace,
|
||||||
|
EntryKey: &key,
|
||||||
|
}
|
||||||
|
if revision != 0 {
|
||||||
|
opts.Revision = &revision
|
||||||
|
}
|
||||||
|
|
||||||
|
arg := newKVArg("del", opts)
|
||||||
|
|
||||||
|
jsonBytes, _ := json.Marshal(arg)
|
||||||
|
|
||||||
|
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes))
|
||||||
|
if err != nil {
|
||||||
|
return r.Result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(cmdOut, &r)
|
||||||
|
if err != nil {
|
||||||
|
return r.Result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
76
types.go
76
types.go
@ -114,6 +114,32 @@ type SendResponse struct {
|
|||||||
Error *Error `json:"error,omitempty"`
|
Error *Error `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type KVOptions struct {
|
||||||
|
Team *string `json:"team"`
|
||||||
|
Namespace *string `json:"namespace,omitempty"`
|
||||||
|
EntryKey *string `json:"entryKey,omitempty"`
|
||||||
|
EntryValue *string `json:"entryValue,omitempty"`
|
||||||
|
Revision *int `json:"revision,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type kvParams struct {
|
||||||
|
Options KVOptions `json:"options"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type kvArg struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Params kvParams `json:"params"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func newKVArg(method string, options KVOptions) kvArg {
|
||||||
|
return kvArg{
|
||||||
|
Method: method,
|
||||||
|
Params: kvParams{
|
||||||
|
Options: options,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ChatAPI holds information about a message received by the `keybase chat api-listen` command
|
// ChatAPI holds information about a message received by the `keybase chat api-listen` command
|
||||||
type ChatAPI struct {
|
type ChatAPI struct {
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
@ -714,41 +740,6 @@ type teamInfo struct {
|
|||||||
Implicit implicit `json:"implicit,omitempty"`
|
Implicit implicit `json:"implicit,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// KVAPI holds information sent and received to/from the kvstore api
|
|
||||||
type KVAPI struct {
|
|
||||||
Method string `json:"method,omitempty"`
|
|
||||||
Params *kvParams `json:"params,omitempty"`
|
|
||||||
Result *kvResult `json:"result,omitempty"`
|
|
||||||
Error *Error `json:"error"`
|
|
||||||
keybase Keybase
|
|
||||||
}
|
|
||||||
|
|
||||||
type kvOptions struct {
|
|
||||||
Team string `json:"team,omitempty"`
|
|
||||||
Namespace string `json:"namespace,omitempty"`
|
|
||||||
EntryKey string `json:"entryKey,omitempty"`
|
|
||||||
Revision uint `json:"revision,omitempty"`
|
|
||||||
EntryValue string `json:"entryValue,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type kvParams struct {
|
|
||||||
Options kvOptions `json:"options,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type entryKey struct {
|
|
||||||
EntryKey string `json:"entryKey"`
|
|
||||||
Revision uint `json:"revision"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type kvResult struct {
|
|
||||||
TeamName string `json:"teamName"`
|
|
||||||
Namespaces []string `json:"namespaces"`
|
|
||||||
EntryKeys []entryKey `json:"entryKeys"`
|
|
||||||
EntryKey string `json:"entryKey"`
|
|
||||||
EntryValue string `json:"entryValue"`
|
|
||||||
Revision uint `json:"revision"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// UserAPI holds information received from the user/lookup api
|
// UserAPI holds information received from the user/lookup api
|
||||||
type UserAPI struct {
|
type UserAPI struct {
|
||||||
Status uStatus `json:"status"`
|
Status uStatus `json:"status"`
|
||||||
@ -951,20 +942,6 @@ type wallet interface {
|
|||||||
TxDetail(txid string) (WalletAPI, error)
|
TxDetail(txid string) (WalletAPI, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// KV holds basic information about a KVStore
|
|
||||||
type KV struct {
|
|
||||||
keybase *Keybase
|
|
||||||
Team string
|
|
||||||
}
|
|
||||||
|
|
||||||
type kvInterface interface {
|
|
||||||
Namespaces() (KVAPI, error)
|
|
||||||
Keys(namespace string) (KVAPI, error)
|
|
||||||
Get(namespace string, key string) (KVAPI, error)
|
|
||||||
Put(namespace string, key string, value string) (KVAPI, error)
|
|
||||||
Delete(namespace string, key string) (KVAPI, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type keybase interface {
|
type keybase interface {
|
||||||
AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error)
|
AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error)
|
||||||
AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error)
|
AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error)
|
||||||
@ -973,7 +950,6 @@ type keybase interface {
|
|||||||
CreateTeam(name string) (TeamAPI, error)
|
CreateTeam(name string) (TeamAPI, error)
|
||||||
NewChat(channel chat1.ChatChannel) Chat
|
NewChat(channel chat1.ChatChannel) Chat
|
||||||
NewTeam(name string) Team
|
NewTeam(name string) Team
|
||||||
NewKV(team string) KV
|
|
||||||
NewWallet() Wallet
|
NewWallet() Wallet
|
||||||
Run(handler func(ChatAPI), options ...RunOptions)
|
Run(handler func(ChatAPI), options ...RunOptions)
|
||||||
status() status
|
status() status
|
||||||
|
|||||||
Reference in New Issue
Block a user