Skip to content

Instantly share code, notes, and snippets.

@by-sabbir
Created October 8, 2022 20:24
Show Gist options
  • Save by-sabbir/1a82a7230d327bd40e6978c6319d7b16 to your computer and use it in GitHub Desktop.
Save by-sabbir/1a82a7230d327bd40e6978c6319d7b16 to your computer and use it in GitHub Desktop.

Revisions

  1. by-sabbir created this gist Oct 8, 2022.
    38 changes: 38 additions & 0 deletions kv-store.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package consul

    import (
    "log"

    "github.com/hashicorp/consul/api"
    )

    type KVClient struct {
    *api.KV
    }

    func NewKVClient(c *ConsulClient) *KVClient {
    return &KVClient{
    c.KV(),
    }
    }

    func (k *KVClient) PutKV(key, value string) error {
    p := &api.KVPair{Key: key, Value: []byte(value)}
    _, err := k.Put(p, nil)
    if err != nil {
    log.Println("error instergin KV: ", err)
    return err
    }

    return nil
    }

    func (k *KVClient) GetKV(key string) (string, error) {
    p, _, err := k.Get(key, nil)
    if err != nil {
    log.Println("error getting value from key: ", err)
    return "", nil
    }

    return string(p.Value), nil
    }