-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
63 lines (53 loc) · 1.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"gopkg.in/yaml.v2"
)
type Config struct {
UserID string `yaml:"UserID"`
Region string `yaml:"Region"`
Bucket string `yaml:"Bucket"`
SecretID string `yaml:"SecretID"`
SecretKey string `yaml:"SecretKey"`
SessionToken string `yaml:"Token"`
}
var conf Config
var default_config_path = ".costool.yaml"
func init_config() {
// 读取默认路径
user, err := user.Current()
if nil == err {
default_config_path = filepath.Join(user.HomeDir, default_config_path)
fmt.Println("\033[32m> load config in ", default_config_path, "\033[0m")
} else {
panic("default_config_path")
}
config(default_config_path) // 加载config
// 检测是否设定bucket
if conf.Bucket == "" {
SelectBucket()
}
fmt.Println("\033[32m> 选中", conf.Bucket, "\033[0m")
}
// 加载config
func config(path string) {
bytes, errP := ioutil.ReadFile(path)
err := yaml.Unmarshal(bytes, &conf)
if err != nil || errP != nil {
ErrorP("config parser fail")
ErrorP("请在用户目录创建 .costool.yaml文件")
ErrorP("填写格式如下")
fmt.Println("UserID: xxxxxx\n" +
"SecretID: xxxxxx\n" +
"SecretKey: xxxxxx\n")
os.Exit(1)
}
}
func SaveConfig() {
data2, _ := yaml.Marshal(conf)
ioutil.WriteFile(default_config_path, data2, 0660)
}