-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
66 lines (59 loc) · 1.52 KB
/
config.go
File metadata and controls
66 lines (59 loc) · 1.52 KB
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
64
65
66
package main
import (
"log"
"github.com/Unknwon/goconfig"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
)
// LoadGitFeedCfg 加载 gitfeed 配置文件
func LoadGitFeedCfg(c *cli.Context) GitFeedCfg {
username := ""
maxPage := 1
debug := false
// 从指令中加载参数
if len(c.String("user")) > 0 {
username = c.String("user")
}
if c.Int("max_page") > 0 {
maxPage = c.Int("max_page")
}
if c.Bool("debug") {
debug = true
}
if len(username) == 0 {
basePath, _ := homedir.Dir()
path := basePath + "/.gitfeed/gitfeed.ini"
if len(c.String("config")) > 0 {
// 读取到你设置的配置文件
path = c.String("config")
}
// 加载配置文件
cfg, err := goconfig.LoadConfigFile(path)
if err != nil {
log.Fatalf("无法加载配置文件,请创建并配置参数:%s", err)
}
username, err = cfg.GetValue("GitHub Newsfeed", "user")
if err != nil {
log.Fatalf("无法获取键值(%s):%s", "username", err)
}
maxPage, err = cfg.Int("GitHub Newsfeed", "max_page")
if err != nil {
log.Fatalf("无法获取键值(%s):%s", "max_page", err)
}
debug, err = cfg.Bool("GitHub Newsfeed", "debug")
if err != nil {
log.Fatalf("无法获取键值(%s):%s", "debug", err)
}
}
cfgInfo := GitFeedCfg{}
cfgInfo.Username = username
cfgInfo.MaxPage = maxPage
cfgInfo.Debug = debug
return cfgInfo
}
// GitFeedCfg gitfeed config
type GitFeedCfg struct {
Username string `json:"username"`
MaxPage int `json:"max_page"`
Debug bool `json:"debug"`
}