Remove global custom config path variables, use get/set methods instead

This commit is contained in:
Harshavardhana 2015-07-14 11:55:28 -07:00
parent 893782ffab
commit da8b9fd112
8 changed files with 38 additions and 55 deletions

View File

@ -39,6 +39,9 @@ type Config struct {
// getAuthConfigPath get donut config file path
func getAuthConfigPath() (string, error) {
if customConfigPath != "" {
return customConfigPath, nil
}
u, err := user.Current()
if err != nil {
return "", iodine.New(err, nil)
@ -47,20 +50,19 @@ func getAuthConfigPath() (string, error) {
return authConfigPath, nil
}
// NOTE - this is not thread safe use it carefully, currently its purpose is only for testing purposes.
var CustomConfigPath string
// customConfigPath not accessed from outside only allowed through get/set methods
var customConfigPath string
// SetAuthConfigPath - set custom auth config path
func SetAuthConfigPath(configPath string) {
customConfigPath = configPath
}
// SaveConfig save donut config
func SaveConfig(a *Config) error {
var authConfigPath string
var err error
if CustomConfigPath != "" {
authConfigPath = CustomConfigPath
} else {
authConfigPath, err = getAuthConfigPath()
if err != nil {
return iodine.New(err, nil)
}
authConfigPath, err := getAuthConfigPath()
if err != nil {
return iodine.New(err, nil)
}
qc, err := quick.New(a)
if err != nil {
@ -74,15 +76,9 @@ func SaveConfig(a *Config) error {
// LoadConfig load donut config
func LoadConfig() (*Config, error) {
var authConfigPath string
var err error
if CustomConfigPath != "" {
authConfigPath = CustomConfigPath
} else {
authConfigPath, err = getAuthConfigPath()
if err != nil {
return nil, iodine.New(err, nil)
}
authConfigPath, err := getAuthConfigPath()
if err != nil {
return nil, iodine.New(err, nil)
}
a := &Config{}
a.Version = "0.0.1"

View File

@ -26,6 +26,9 @@ import (
// getDonutConfigPath get donut config file path
func getDonutConfigPath() (string, error) {
if customConfigPath != "" {
return customConfigPath, nil
}
u, err := user.Current()
if err != nil {
return "", iodine.New(err, nil)
@ -34,20 +37,19 @@ func getDonutConfigPath() (string, error) {
return donutConfigPath, nil
}
// NOTE - this is not thread safe use it carefully, currently its purpose is only for testing purposes.
var CustomConfigPath string
// internal variable only accessed via get/set methods
var customConfigPath string
// SetDonutConfigPath - set custom donut config path
func SetDonutConfigPath(configPath string) {
customConfigPath = configPath
}
// SaveConfig save donut config
func SaveConfig(a *Config) error {
var donutConfigPath string
var err error
if CustomConfigPath != "" {
donutConfigPath = CustomConfigPath
} else {
donutConfigPath, err = getDonutConfigPath()
if err != nil {
return iodine.New(err, nil)
}
donutConfigPath, err := getDonutConfigPath()
if err != nil {
return iodine.New(err, nil)
}
qc, err := quick.New(a)
if err != nil {
@ -61,15 +63,9 @@ func SaveConfig(a *Config) error {
// LoadConfig load donut config
func LoadConfig() (*Config, error) {
var donutConfigPath string
var err error
if CustomConfigPath != "" {
donutConfigPath = CustomConfigPath
} else {
donutConfigPath, err = getDonutConfigPath()
if err != nil {
return nil, iodine.New(err, nil)
}
donutConfigPath, err := getDonutConfigPath()
if err != nil {
return nil, iodine.New(err, nil)
}
a := &Config{}
a.Version = "0.0.1"

View File

@ -66,7 +66,7 @@ func (s *MyDonutSuite) SetUpSuite(c *C) {
conf.DonutName = "test"
conf.NodeDiskMap = createTestNodeDiskMap(root)
conf.MaxSize = 100000
CustomConfigPath = filepath.Join(root, "donut.json")
SetDonutConfigPath(filepath.Join(root, "donut.json"))
err = SaveConfig(conf)
c.Assert(err, IsNil)

View File

@ -44,7 +44,7 @@ func (s *MyCacheSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.root = root
CustomConfigPath = filepath.Join(root, "donut.json")
SetDonutConfigPath(filepath.Join(root, "donut.json"))
dc, err = New()
c.Assert(err, IsNil)

View File

@ -46,15 +46,6 @@ const (
yyyymmdd = "20060102"
)
var ignoredHeaders = map[string]bool{
"Authorization": true,
"Content-Type": true,
"Accept-Encoding": true,
"Content-Length": true,
"User-Agent": true,
"Connection": true,
}
// sumHMAC calculate hmac between two input byte array
func sumHMAC(key []byte, data []byte) []byte {
hash := hmac.New(sha256.New, key)

View File

@ -51,7 +51,7 @@ func (s *MyAPIDonutCacheSuite) SetUpSuite(c *C) {
conf := &donut.Config{}
conf.Version = "0.0.1"
conf.MaxSize = 100000
donut.CustomConfigPath = filepath.Join(root, "donut.json")
donut.SetDonutConfigPath(filepath.Join(root, "donut.json"))
err = donut.SaveConfig(conf)
c.Assert(err, IsNil)

View File

@ -70,7 +70,7 @@ func (s *MyAPIDonutSuite) SetUpSuite(c *C) {
conf.DonutName = "test"
conf.NodeDiskMap = createTestNodeDiskMap(root)
conf.MaxSize = 100000
donut.CustomConfigPath = filepath.Join(root, "donut.json")
donut.SetDonutConfigPath(filepath.Join(root, "donut.json"))
err = donut.SaveConfig(conf)
c.Assert(err, IsNil)

View File

@ -59,7 +59,7 @@ func (s *MyAPISignatureV4Suite) SetUpSuite(c *C) {
conf.DonutName = "test"
conf.NodeDiskMap = createTestNodeDiskMap(root)
conf.MaxSize = 100000
donut.CustomConfigPath = filepath.Join(root, "donut.json")
donut.SetDonutConfigPath(filepath.Join(root, "donut.json"))
err = donut.SaveConfig(conf)
c.Assert(err, IsNil)
@ -78,7 +78,7 @@ func (s *MyAPISignatureV4Suite) SetUpSuite(c *C) {
s.accessKeyID = string(accessKeyID)
s.secretAccessKey = string(secretAccessKey)
auth.CustomConfigPath = filepath.Join(root, "users.json")
auth.SetAuthConfigPath(filepath.Join(root, "users.json"))
err = auth.SaveConfig(authConf)
c.Assert(err, IsNil)