github.com/hairyhenderson/templater@v3.5.0+incompatible/libkv/boltdb.go (about)

     1  package libkv
     2  
     3  import (
     4  	"net/url"
     5  	"time"
     6  
     7  	"github.com/docker/libkv"
     8  	"github.com/docker/libkv/store"
     9  	"github.com/docker/libkv/store/boltdb"
    10  	"github.com/hairyhenderson/gomplate/conv"
    11  	"github.com/hairyhenderson/gomplate/env"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // NewBoltDB - initialize a new BoltDB datasource handler
    16  func NewBoltDB(u *url.URL) (*LibKV, error) {
    17  	boltdb.Register()
    18  
    19  	config, err := setupBoltDB(u.Fragment)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	kv, err := libkv.NewStore(store.BOLTDB, []string{u.Path}, config)
    24  	if err != nil {
    25  		return nil, errors.Wrapf(err, "BoltDB store creation failed")
    26  	}
    27  	return &LibKV{kv}, nil
    28  }
    29  
    30  func setupBoltDB(bucket string) (*store.Config, error) {
    31  	if bucket == "" {
    32  		return nil, errors.New("missing bucket - must specify BoltDB bucket in URL fragment")
    33  	}
    34  
    35  	t := conv.MustParseInt(env.Getenv("BOLTDB_TIMEOUT"), 10, 16)
    36  	return &store.Config{
    37  		Bucket:            bucket,
    38  		ConnectionTimeout: time.Duration(t) * time.Second,
    39  		PersistConnection: conv.Bool(env.Getenv("BOLTDB_PERSIST")),
    40  	}, nil
    41  }