minio/pkg/api/config/config.go

152 lines
3.2 KiB
Go
Raw Normal View History

2015-02-08 05:54:21 -05:00
/*
* Minimalist Object Storage, (C) 2015 Minio, Inc.
2015-02-08 05:54:21 -05:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2015-02-03 03:12:46 -05:00
package config
import (
"encoding/json"
2015-04-03 21:06:30 -04:00
"github.com/minio-io/iodine"
2015-02-05 19:10:49 -05:00
"io"
2015-02-03 03:12:46 -05:00
"os"
2015-03-01 18:12:09 -05:00
"os/user"
2015-02-03 03:12:46 -05:00
"path"
"sync"
)
2015-03-05 22:59:26 -05:00
// Config context
2015-02-03 03:12:46 -05:00
type Config struct {
ConfigPath string
ConfigFile string
ConfigLock *sync.RWMutex
2015-02-03 03:12:46 -05:00
Users map[string]User
}
2015-03-05 22:59:26 -05:00
// User context
2015-02-03 03:12:46 -05:00
type User struct {
Name string
AccessKey string
SecretKey string
}
2015-03-05 22:59:26 -05:00
// SetupConfig initialize config directory and template config
2015-02-03 03:12:46 -05:00
func (c *Config) SetupConfig() error {
2015-03-01 18:12:09 -05:00
u, err := user.Current()
if err != nil {
2015-04-03 21:06:30 -04:00
return iodine.New(err, nil)
2015-03-01 18:12:09 -05:00
}
confPath := path.Join(u.HomeDir, ".minio")
2015-02-03 03:12:46 -05:00
if err := os.MkdirAll(confPath, os.ModeDir); err != nil {
2015-04-03 21:06:30 -04:00
return iodine.New(err, nil)
2015-02-03 03:12:46 -05:00
}
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)
2015-02-03 03:12:46 -05:00
if err != nil {
2015-04-03 21:06:30 -04:00
return iodine.New(err, nil)
2015-02-03 03:12:46 -05:00
}
}
c.ConfigLock = new(sync.RWMutex)
2015-02-03 03:12:46 -05:00
return nil
}
2015-03-05 22:59:26 -05:00
// GetConfigPath config file location
2015-02-03 03:12:46 -05:00
func (c *Config) GetConfigPath() string {
return c.ConfigPath
2015-02-03 03:12:46 -05:00
}
2015-03-05 22:59:26 -05:00
// IsUserExists verify if user exists
func (c *Config) IsUserExists(username string) bool {
for _, user := range c.Users {
if user.Name == username {
return true
}
}
return false
}
2015-03-05 22:59:26 -05:00
// GetUser - get user from username
func (c *Config) GetUser(username string) User {
for _, user := range c.Users {
if user.Name == username {
return user
}
}
return User{}
}
2015-03-05 22:59:26 -05:00
// AddUser - add a user into existing User list
2015-02-03 03:12:46 -05:00
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
}
2015-03-05 22:59:26 -05:00
// WriteConfig - write encoded json in config file
2015-02-03 03:12:46 -05:00
func (c *Config) WriteConfig() error {
c.ConfigLock.Lock()
defer c.ConfigLock.Unlock()
2015-02-23 20:44:55 -05:00
2015-02-03 03:12:46 -05:00
var file *os.File
var err error
file, err = os.OpenFile(c.ConfigFile, os.O_WRONLY, 0666)
2015-02-03 03:12:46 -05:00
defer file.Close()
if err != nil {
2015-04-03 21:06:30 -04:00
return iodine.New(err, nil)
2015-02-03 03:12:46 -05:00
}
encoder := json.NewEncoder(file)
encoder.Encode(c.Users)
return nil
}
2015-03-05 22:59:26 -05:00
// ReadConfig - read json config file and decode
2015-02-03 03:12:46 -05:00
func (c *Config) ReadConfig() error {
c.ConfigLock.RLock()
defer c.ConfigLock.RUnlock()
2015-02-03 03:12:46 -05:00
2015-02-23 20:44:55 -05:00
var file *os.File
var err error
file, err = os.OpenFile(c.ConfigFile, os.O_RDONLY, 0666)
2015-02-03 03:12:46 -05:00
defer file.Close()
if err != nil {
2015-04-03 21:06:30 -04:00
return iodine.New(err, nil)
2015-02-03 03:12:46 -05:00
}
users := make(map[string]User)
decoder := json.NewDecoder(file)
err = decoder.Decode(&users)
2015-02-05 19:10:49 -05:00
switch err {
case io.EOF:
return nil
case nil:
c.Users = users
return nil
default:
2015-04-03 21:06:30 -04:00
return iodine.New(err, nil)
2015-02-03 03:12:46 -05:00
}
}