github.com/wuhuizuo/gomplate@v3.5.0+incompatible/libkv/libkv.go (about) 1 package libkv 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "strings" 7 8 "github.com/docker/libkv/store" 9 ) 10 11 // LibKV - 12 type LibKV struct { 13 store store.Store 14 } 15 16 // Login - 17 func (kv *LibKV) Login() error { 18 return nil 19 } 20 21 // Logout - 22 func (kv *LibKV) Logout() { 23 } 24 25 // Read - 26 func (kv *LibKV) Read(path string) ([]byte, error) { 27 data, err := kv.store.Get(path) 28 if err != nil { 29 return nil, err 30 } 31 32 return data.Value, nil 33 } 34 35 // List - 36 func (kv *LibKV) List(path string) ([]byte, error) { 37 data, err := kv.store.List(path) 38 if err != nil { 39 return nil, err 40 } 41 42 result := []map[string]string{} 43 for _, pair := range data { 44 // Remove the path from the key 45 key := strings.TrimPrefix( 46 pair.Key, 47 strings.TrimLeft(path, "/"), 48 ) 49 result = append( 50 result, 51 map[string]string{ 52 "key": key, 53 "value": string(pair.Value), 54 }, 55 ) 56 } 57 58 var buf bytes.Buffer 59 enc := json.NewEncoder(&buf) 60 if err := enc.Encode(result); err != nil { 61 return nil, err 62 } 63 return buf.Bytes(), nil 64 }