114 lines
2.3 KiB
Go
114 lines
2.3 KiB
Go
|
package badger
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/kelseyhightower/envconfig"
|
||
|
|
||
|
"github.com/ipfs/ipfs-cluster/config"
|
||
|
)
|
||
|
|
||
|
const configKey = "badger"
|
||
|
const envConfigKey = "cluster_badger"
|
||
|
|
||
|
// Default values for badger Config
|
||
|
const (
|
||
|
DefaultSubFolder = "badger"
|
||
|
)
|
||
|
|
||
|
// Config is used to initialize a BadgerDB datastore. It implements the
|
||
|
// ComponentConfig interface.
|
||
|
type Config struct {
|
||
|
config.Saver
|
||
|
|
||
|
// The folder for this datastore. Non-absolute paths are relative to
|
||
|
// the base configuration folder.
|
||
|
Folder string
|
||
|
}
|
||
|
|
||
|
type jsonConfig struct {
|
||
|
Folder string `json:"folder,omitempty"`
|
||
|
}
|
||
|
|
||
|
// ConfigKey returns a human-friendly identifier for this type of Datastore.
|
||
|
func (cfg *Config) ConfigKey() string {
|
||
|
return configKey
|
||
|
}
|
||
|
|
||
|
// Default initializes this Config with sensible values.
|
||
|
func (cfg *Config) Default() error {
|
||
|
cfg.Folder = DefaultSubFolder
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ApplyEnvVars fills in any Config fields found as environment variables.
|
||
|
func (cfg *Config) ApplyEnvVars() error {
|
||
|
jcfg := cfg.toJSONConfig()
|
||
|
|
||
|
err := envconfig.Process(envConfigKey, jcfg)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return cfg.applyJSONConfig(jcfg)
|
||
|
}
|
||
|
|
||
|
// Validate checks that the fields of this Config have working values,
|
||
|
// at least in appearance.
|
||
|
func (cfg *Config) Validate() error {
|
||
|
if cfg.Folder == "" {
|
||
|
return errors.New("folder is unset")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// LoadJSON reads the fields of this Config from a JSON byteslice as
|
||
|
// generated by ToJSON.
|
||
|
func (cfg *Config) LoadJSON(raw []byte) error {
|
||
|
jcfg := &jsonConfig{}
|
||
|
err := json.Unmarshal(raw, jcfg)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
cfg.Default()
|
||
|
|
||
|
return cfg.applyJSONConfig(jcfg)
|
||
|
}
|
||
|
|
||
|
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
||
|
config.SetIfNotDefault(jcfg.Folder, &cfg.Folder)
|
||
|
return cfg.Validate()
|
||
|
}
|
||
|
|
||
|
// ToJSON generates a JSON-formatted human-friendly representation of this
|
||
|
// Config.
|
||
|
func (cfg *Config) ToJSON() (raw []byte, err error) {
|
||
|
jcfg := cfg.toJSONConfig()
|
||
|
|
||
|
raw, err = config.DefaultJSONMarshal(jcfg)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (cfg *Config) toJSONConfig() *jsonConfig {
|
||
|
jCfg := &jsonConfig{}
|
||
|
|
||
|
if cfg.Folder != DefaultSubFolder {
|
||
|
jCfg.Folder = cfg.Folder
|
||
|
}
|
||
|
|
||
|
return jCfg
|
||
|
}
|
||
|
|
||
|
// GetFolder returns the BadgerDB folder.
|
||
|
func (cfg *Config) GetFolder() string {
|
||
|
if filepath.IsAbs(cfg.Folder) {
|
||
|
return cfg.Folder
|
||
|
}
|
||
|
|
||
|
return filepath.Join(cfg.BaseDir, cfg.Folder)
|
||
|
}
|