From b1db70c807e4fc99cf0b31351a4697649a2c1775 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 3 Feb 2015 00:12:46 -0800 Subject: [PATCH] Add json config reader/writer --- pkg/utils/config/config.go | 97 +++++++++++++++++++++++++++++++++ pkg/utils/config/config_test.go | 46 ++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 pkg/utils/config/config.go create mode 100644 pkg/utils/config/config_test.go diff --git a/pkg/utils/config/config.go b/pkg/utils/config/config.go new file mode 100644 index 000000000..97bfabf13 --- /dev/null +++ b/pkg/utils/config/config.go @@ -0,0 +1,97 @@ +package config + +import ( + "encoding/json" + "os" + "path" + "sync" + + "github.com/minio-io/minio/pkg/utils/helpers" +) + +type Config struct { + configPath string + configFile string + configLock *sync.RWMutex + Users map[string]User +} + +type User struct { + Name string + AccessKey string + SecretKey string +} + +func (c *Config) SetupConfig() error { + confPath := path.Join(helpers.HomeDir(), ".minio") + if err := os.MkdirAll(confPath, os.ModeDir); err != nil { + return err + } + + c.configPath = confPath + c.configFile = path.Join(c.configPath, "config.json") + if _, err := os.Stat(c.configFile); os.IsNotExist(err) { + _, err = os.Create(c.configFile) + if err != nil { + return err + } + } + + c.configLock = new(sync.RWMutex) + return nil +} + +func (c *Config) GetConfigPath() string { + return c.configPath +} + +func (c *Config) AddUser(user User) { + var currentUsers map[string]User + if len(c.Users) == 0 { + currentUsers = make(map[string]User) + } else { + currentUsers = c.Users + } + currentUsers[user.AccessKey] = user + c.Users = currentUsers +} + +func (c *Config) WriteConfig() error { + var file *os.File + var err error + + c.configLock.Lock() + defer c.configLock.Unlock() + file, err = os.OpenFile(c.configFile, os.O_WRONLY, 0666) + defer file.Close() + if err != nil { + return err + } + + encoder := json.NewEncoder(file) + encoder.Encode(c.Users) + return nil +} + +func (c *Config) ReadConfig() error { + var file *os.File + var err error + + c.configLock.RLock() + defer c.configLock.RUnlock() + + file, err = os.OpenFile(c.configFile, os.O_RDONLY, 0666) + defer file.Close() + if err != nil { + return err + } + + users := make(map[string]User) + decoder := json.NewDecoder(file) + err = decoder.Decode(&users) + if err != nil { + return err + } + c.Users = users + return nil +} diff --git a/pkg/utils/config/config_test.go b/pkg/utils/config/config_test.go new file mode 100644 index 000000000..48194d543 --- /dev/null +++ b/pkg/utils/config/config_test.go @@ -0,0 +1,46 @@ +package config + +import ( + "testing" + + "github.com/minio-io/minio/pkg/utils/crypto/keys" + . "gopkg.in/check.v1" +) + +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func Test(t *testing.T) { TestingT(t) } + +func (s *MySuite) TestConfig(c *C) { + conf := Config{} + conf.SetupConfig() + + accesskey, _ := keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID) + secretkey, _ := keys.GetRandomBase64(keys.MINIO_SECRET_ID) + + user := User{ + Name: "gnubot", + AccessKey: string(accesskey), + SecretKey: string(secretkey), + } + + conf.AddUser(user) + err := conf.WriteConfig() + c.Assert(err, IsNil) + + err = conf.ReadConfig() + c.Assert(err, IsNil) + + accesskey, _ = keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID) + secretkey, _ = keys.GetRandomBase64(keys.MINIO_SECRET_ID) + user = User{ + Name: "minio", + AccessKey: string(accesskey), + SecretKey: string(secretkey), + } + conf.AddUser(user) + err = conf.WriteConfig() + c.Assert(err, IsNil) +}