summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/keybase/go-keybase-chat-bot/kbchat/kvstore.go
blob: d7112936adbf9f5de6f3dcc7a6a84e22f9607866 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package kbchat

import (
	"encoding/json"
	"strings"

	"github.com/keybase/go-keybase-chat-bot/kbchat/types/keybase1"
)

type kvstoreMethod string

type kvstoreOptions 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 kvstoreParams struct {
	Options kvstoreOptions `json:"options"`
}

type kvstoreAPIReq struct {
	Method kvstoreMethod `json:"method"`
	Params kvstoreParams `json:"params"`
}

type GetEntryRes struct {
	Result keybase1.KVGetResult `json:"result"`
	Error  Error                `json:"error,omitempty"`
}

type PutEntryRes struct {
	Result keybase1.KVPutResult `json:"result"`
	Error  Error                `json:"error,omitempty"`
}

type DeleteEntryRes struct {
	Result keybase1.KVDeleteEntryResult `json:"result"`
	Error  Error                        `json:"error,omitempty"`
}

type ListNamespacesRes struct {
	Result keybase1.KVListNamespaceResult `json:"result"`
	Error  Error                          `json:"error,omitempty"`
}

type ListEntryKeysRes struct {
	Result keybase1.KVListEntryResult `json:"result"`
	Error  Error                      `json:"error,omitempty"`
}

type KVStoreAPI interface {
	PutEntry(teamName *string, namespace string, entryKey string, entryValue string) (keybase1.KVPutResult, error)
	PutEntryWithRevision(teamName *string, namespace string, entryKey string, entryValue string, revision int) (keybase1.KVPutResult, error)
	DeleteEntry(teamName *string, namespace string, entryKey string) (keybase1.KVDeleteEntryResult, error)
	DeleteEntryWithRevision(teamName *string, namespace string, entryKey string, revision int) (keybase1.KVDeleteEntryResult, error)
	GetEntry(teamName *string, namespace string, entryKey string) (keybase1.KVGetResult, error)
	ListNamespaces(teamName *string) (keybase1.KVListNamespaceResult, error)
	ListEntryKeys(teamName *string, namespace string) (keybase1.KVListEntryResult, error)
}

func (a *API) PutEntry(teamName *string, namespace string, entryKey string, entryValue string) (result keybase1.KVPutResult, err error) {
	return a.PutEntryWithRevision(teamName, namespace, entryKey, entryValue, 0)
}

func (a *API) PutEntryWithRevision(teamName *string, namespace string, entryKey string, entryValue string, revision int) (result keybase1.KVPutResult, err error) {

	opts := kvstoreOptions{
		Team:       teamName,
		Namespace:  &namespace,
		EntryKey:   &entryKey,
		EntryValue: &entryValue,
	}
	if revision != 0 {
		opts.Revision = &revision
	}
	args := kvstoreAPIReq{Method: "put", Params: kvstoreParams{Options: opts}}
	apiInput, err := json.Marshal(args)
	if err != nil {
		return result, err
	}

	cmd := a.runOpts.Command("kvstore", "api")
	cmd.Stdin = strings.NewReader(string(apiInput))
	bytes, err := cmd.Output()
	if err != nil {
		return result, APIError{err}
	}

	entry := PutEntryRes{}
	err = json.Unmarshal(bytes, &entry)
	if err != nil {
		return result, UnmarshalError{err}
	}
	if entry.Error.Message != "" {
		return result, entry.Error
	}
	return entry.Result, nil
}

func (a *API) DeleteEntry(teamName *string, namespace string, entryKey string) (result keybase1.KVDeleteEntryResult, err error) {
	return a.DeleteEntryWithRevision(teamName, namespace, entryKey, 0)
}

func (a *API) DeleteEntryWithRevision(teamName *string, namespace string, entryKey string, revision int) (result keybase1.KVDeleteEntryResult, err error) {

	opts := kvstoreOptions{
		Team:      teamName,
		Namespace: &namespace,
		EntryKey:  &entryKey,
	}
	if revision != 0 {
		opts.Revision = &revision
	}
	args := kvstoreAPIReq{Method: "del", Params: kvstoreParams{Options: opts}}
	apiInput, err := json.Marshal(args)
	if err != nil {
		return result, err
	}

	cmd := a.runOpts.Command("kvstore", "api")
	cmd.Stdin = strings.NewReader(string(apiInput))
	bytes, err := cmd.Output()
	if err != nil {
		return result, APIError{err}
	}

	entry := DeleteEntryRes{}
	err = json.Unmarshal(bytes, &entry)
	if err != nil {
		return result, UnmarshalError{err}
	}
	if entry.Error.Message != "" {
		return result, entry.Error
	}
	return entry.Result, nil
}

func (a *API) GetEntry(teamName *string, namespace string, entryKey string) (result keybase1.KVGetResult, err error) {

	opts := kvstoreOptions{
		Team:      teamName,
		Namespace: &namespace,
		EntryKey:  &entryKey,
	}
	args := kvstoreAPIReq{Method: "get", Params: kvstoreParams{Options: opts}}
	apiInput, err := json.Marshal(args)
	if err != nil {
		return result, err
	}
	cmd := a.runOpts.Command("kvstore", "api")
	cmd.Stdin = strings.NewReader(string(apiInput))
	bytes, err := cmd.Output()
	if err != nil {
		return result, APIError{err}
	}

	entry := GetEntryRes{}
	err = json.Unmarshal(bytes, &entry)
	if err != nil {
		return result, UnmarshalError{err}
	}
	if entry.Error.Message != "" {
		return result, entry.Error
	}
	return entry.Result, nil
}

func (a *API) ListNamespaces(teamName *string) (result keybase1.KVListNamespaceResult, err error) {

	opts := kvstoreOptions{
		Team: teamName,
	}
	args := kvstoreAPIReq{Method: "list", Params: kvstoreParams{Options: opts}}
	apiInput, err := json.Marshal(args)
	if err != nil {
		return result, err
	}

	cmd := a.runOpts.Command("kvstore", "api")
	cmd.Stdin = strings.NewReader(string(apiInput))
	bytes, err := cmd.Output()
	if err != nil {
		return result, APIError{err}
	}

	var namespaces ListNamespacesRes
	err = json.Unmarshal(bytes, &namespaces)
	if err != nil {
		return result, UnmarshalError{err}
	}
	if namespaces.Error.Message != "" {
		return result, namespaces.Error
	}
	return namespaces.Result, nil
}

func (a *API) ListEntryKeys(teamName *string, namespace string) (result keybase1.KVListEntryResult, err error) {

	opts := kvstoreOptions{
		Team:      teamName,
		Namespace: &namespace,
	}
	args := kvstoreAPIReq{Method: "list", Params: kvstoreParams{Options: opts}}
	apiInput, err := json.Marshal(args)
	if err != nil {
		return result, err
	}

	cmd := a.runOpts.Command("kvstore", "api")
	cmd.Stdin = strings.NewReader(string(apiInput))
	bytes, err := cmd.Output()
	if err != nil {
		return result, APIError{err}
	}

	entryKeys := ListEntryKeysRes{}
	err = json.Unmarshal(bytes, &entryKeys)
	if err != nil {
		return result, UnmarshalError{err}
	}
	if entryKeys.Error.Message != "" {
		return result, entryKeys.Error
	}
	return entryKeys.Result, nil
}