@@ -82,8 +82,14 @@ func (c *ConsulKV) Value() []byte {
8282
8383// }
8484
85- // helper method
86- func optsWithTimeout (parentCtx context.Context , timeout time.Duration ) (* api.WriteOptions , context.CancelFunc ) {
85+ // helper methods
86+ func queryOptsWithTimeout (parentCtx context.Context , timeout time.Duration ) (* api.QueryOptions , context.CancelFunc ) {
87+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
88+ opts := & api.QueryOptions {}
89+ return opts .WithContext (ctx ), cancel
90+ }
91+
92+ func writeOptsWithTimeout (parentCtx context.Context , timeout time.Duration ) (* api.WriteOptions , context.CancelFunc ) {
8793 ctx , cancel := context .WithTimeout (context .Background (), time .Second )
8894 opts := & api.WriteOptions {}
8995 return opts .WithContext (ctx ), cancel
@@ -92,7 +98,7 @@ func optsWithTimeout(parentCtx context.Context, timeout time.Duration) (*api.Wri
9298func (cc * ConsulClient ) Close () {
9399 // destroy session
94100 session := cc .client .Session ()
95- opts , cancel := optsWithTimeout (context .Background (), time .Second )
101+ opts , cancel := writeOptsWithTimeout (context .Background (), time .Second )
96102 defer cancel ()
97103 _ , err := session .Destroy (cc .sessionId , opts )
98104 if err != nil {
@@ -190,12 +196,40 @@ func (cc *ConsulClient) makeWatchHandler(ch UpdateChan) watch.HybridHandlerFunc
190196// kv put
191197func (cc * ConsulClient ) Put (ctx context.Context , key string , value []byte ) error {
192198 p := & api.KVPair {Key : key , Value : value }
193- opts , cancel := optsWithTimeout (ctx , time .Second )
199+ opts , cancel := writeOptsWithTimeout (ctx , time .Second )
194200 defer cancel ()
195201 _ , err := cc .client .KV ().Put (p , opts )
196202 return err
197203}
198204
205+ // kv put
206+ func (cc * ConsulClient ) Get (ctx context.Context , key string ) ([]byte , error ) {
207+ opts , cancel := queryOptsWithTimeout (ctx , time .Second )
208+ defer cancel ()
209+ res , _ , err := cc .client .KV ().Get (key , opts )
210+ if err != nil {
211+ return nil , err
212+ }
213+ return res .Value , nil
214+ }
215+
216+ func (cc * ConsulClient ) GetWithPrefix (ctx context.Context , prefix string ) ([]Field , error ) {
217+ opts , cancel := queryOptsWithTimeout (ctx , time .Second )
218+ defer cancel ()
219+ res , _ , err := cc .client .KV ().List (prefix , opts )
220+ if err != nil {
221+ return nil , err
222+ }
223+ var fields []Field
224+ for _ , kv := range res {
225+ fields = append (fields , Field {
226+ Key : []byte (kv .Key ),
227+ Value : kv .Value ,
228+ })
229+ }
230+ return fields , nil
231+ }
232+
199233// kv put with expiring session
200234type ErrAlreadyAquired struct {
201235 Key string
@@ -207,7 +241,7 @@ func (e *ErrAlreadyAquired) Error() string {
207241
208242func (cc * ConsulClient ) PutWithSession (ctx context.Context , key string , value []byte ) error {
209243 p := & api.KVPair {Key : key , Value : value , Session : cc .sessionId }
210- opts , cancel := optsWithTimeout (ctx , time .Second )
244+ opts , cancel := writeOptsWithTimeout (ctx , time .Second )
211245 defer cancel ()
212246 success , _ , err := cc .client .KV ().Acquire (p , opts )
213247 if ! success {
@@ -223,7 +257,7 @@ func (cc *ConsulClient) PutWithSession(ctx context.Context, key string, value []
223257
224258// kv delete with expiring session
225259func (cc * ConsulClient ) Delete (ctx context.Context , key string ) error {
226- opts , cancel := optsWithTimeout (ctx , time .Second )
260+ opts , cancel := writeOptsWithTimeout (ctx , time .Second )
227261 defer cancel ()
228262 _ , err := cc .client .KV ().Delete (key , opts )
229263 if err != nil {
0 commit comments