1 Star 0 Fork 0

妙音/Search Engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config.go 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
妙音 提交于 2021-05-18 14:27 +08:00 . 词服务独立
package se
import (
"os"
"path"
"gitee.com/xuender/oils/logs"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Config 设置.
type Config struct {
DataBasePath string
Deploy bool
Limit uint `toml:"-"`
Offset uint `toml:"-"`
Start string `toml:"-"`
Exts []string
Ignore []string
PassDir bool
}
// NewConfig 新建配置.
func NewConfig() *Config {
c := &Config{}
c.Chack()
return c
}
// Read 从命令行中读取配置.
func (p *Config) Read(cmd *cobra.Command) {
if db, err := cmd.Flags().GetString("db"); db != "" && err == nil {
p.DataBasePath = db
}
if limit, err := cmd.Flags().GetUint("limit"); err == nil {
p.Limit = limit
}
if offset, err := cmd.Flags().GetUint("offset"); err == nil {
p.Offset = offset
}
if start, err := cmd.Flags().GetString("start"); err == nil {
p.Start = start
}
}
// Chack 配置检查.
func (p *Config) Chack() {
if p.Limit == 0 {
p.Limit = 100
}
if p.DataBasePath == "" {
dir := logs.PanicString(homedir.Dir())
p.DataBasePath = path.Join(dir, ".se")
}
if len(p.Exts) == 0 {
p.Exts = []string{".go", ".txt", ".puml", ".java"}
}
if len(p.Ignore) == 0 {
p.Ignore = []string{"vendor", "target", "node_modules", ".git"}
}
}
// Save 保存配置.
func (p *Config) Save() {
if viper.ConfigFileUsed() == "" {
home := logs.PanicString(homedir.Dir())
viper.SetConfigFile(path.Join(home, ".se.toml"))
}
logs.Debug("保存日志", viper.ConfigFileUsed())
p.Chack()
file, err := os.Create(viper.ConfigFileUsed())
logs.Panic(err)
defer file.Close()
e := toml.NewEncoder(file)
logs.Panic(e.Encode(p))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xuender/se.git
git@gitee.com:xuender/se.git
xuender
se
Search Engine
v1.0.3

搜索帮助