/
helper.go
62 lines (47 loc) · 1.96 KB
/
helper.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
// Copyright 2022 Innkeeper Belm(孔令飞) <nosbelm@qq.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
package cmd
import (
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
clientcmdutil "github.com/superproj/onex/internal/pkg/util/clientcmd"
)
const (
// defaultConfigName 指定了 onex 服务的默认配置文件名.
defaultConfigName = "onexctl.yaml"
)
// var cfgFile string
// initConfig 设置需要读取的配置文件名、环境变量,并读取配置文件内容到 viper 中.
func initConfig(cfgFile string) {
if cfgFile != "" {
// 从命令行选项指定的配置文件中读取
viper.SetConfigFile(cfgFile)
} else {
// 查找用户主目录
home, err := os.UserHomeDir()
// 如果获取用户主目录失败,打印 `'Error: xxx` 错误,并退出程序(退出码为 1)
cobra.CheckErr(err)
// 将用 `$HOME/<recommendedHomeDir>` 目录加入到配置文件的搜索路径中
viper.AddConfigPath(filepath.Join(home, clientcmdutil.RecommendedHomeDir))
// 把当前目录加入到配置文件的搜索路径中
viper.AddConfigPath(".")
// 设置配置文件格式为 YAML (YAML 格式清晰易读,并且支持复杂的配置结构)
viper.SetConfigType("yaml")
// 配置文件名称(没有文件扩展名)
viper.SetConfigName(defaultConfigName)
}
// 读取匹配的环境变量
viper.AutomaticEnv()
// 读取环境变量的前缀为 ONEX,如果是 onex,将自动转变为大写。
viper.SetEnvPrefix("ONEX")
// 以下 2 行,将 viper.Get(key) key 字符串中 '.' 和 '-' 替换为 '_'
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// 读取配置文件。如果指定了配置文件名,则使用指定的配置文件,否则在注册的搜索路径中搜索
_ = viper.ReadInConfig()
}