2015-07-02 23:31:22 -04:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-07-02 23:31:22 -04: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
|
2015-08-03 19:17:21 -04:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-07-02 23:31:22 -04:00
|
|
|
"github.com/minio/minio/pkg/quick"
|
|
|
|
)
|
|
|
|
|
|
|
|
// getDonutConfigPath get donut config file path
|
2015-08-03 19:17:21 -04:00
|
|
|
func getDonutConfigPath() (string, *probe.Error) {
|
2015-07-14 14:55:28 -04:00
|
|
|
if customConfigPath != "" {
|
|
|
|
return customConfigPath, nil
|
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return "", probe.New(err)
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
donutConfigPath := filepath.Join(u.HomeDir, ".minio", "donut.json")
|
|
|
|
return donutConfigPath, nil
|
|
|
|
}
|
|
|
|
|
2015-07-14 14:55:28 -04:00
|
|
|
// internal variable only accessed via get/set methods
|
|
|
|
var customConfigPath string
|
|
|
|
|
|
|
|
// SetDonutConfigPath - set custom donut config path
|
|
|
|
func SetDonutConfigPath(configPath string) {
|
|
|
|
customConfigPath = configPath
|
|
|
|
}
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
// SaveConfig save donut config
|
2015-08-03 19:17:21 -04:00
|
|
|
func SaveConfig(a *Config) *probe.Error {
|
2015-07-14 14:55:28 -04:00
|
|
|
donutConfigPath, err := getDonutConfigPath()
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err.Trace()
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
qc, err := quick.New(a)
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err.Trace()
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
if err := qc.Save(donutConfigPath); err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err.Trace()
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig load donut config
|
2015-08-03 19:17:21 -04:00
|
|
|
func LoadConfig() (*Config, *probe.Error) {
|
2015-07-14 14:55:28 -04:00
|
|
|
donutConfigPath, err := getDonutConfigPath()
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err.Trace()
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
a := &Config{}
|
|
|
|
a.Version = "0.0.1"
|
|
|
|
qc, err := quick.New(a)
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err.Trace()
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
if err := qc.Load(donutConfigPath); err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err.Trace()
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
return qc.Data().(*Config), nil
|
|
|
|
}
|