/
config.go
84 lines (69 loc) · 1.93 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"errors"
"fmt"
"github.com/spf13/viper"
"strings"
)
type ConfigEnv struct {
Path string
Script string
Dist string
Branch string
LogPath string
PackageManager string
AdditionalScript string
AdditionalScriptArgs []string
}
// Config 配置.
var Config *viper.Viper
// InitConfig 读取配置文件,返回是否找得到配置文件
func InitConfig() (notFound bool) {
Config = viper.New()
Config.SetConfigFile("./config.toml")
//Config.SetConfigName("config")
//Config.SetConfigType("toml")
//Config.AddConfigPath(".")
err := Config.ReadInConfig() // 查找并读取配置文件
if err == nil {
return
}
notFound = false
var notFoundError viper.ConfigFileNotFoundError
ok := errors.Is(err, notFoundError)
if ok {
// 配置文件未找到错误;如果需要可以忽略
notFound = true
return
}
// 配置文件被找到,但产生了另外的错误
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
// GetEnv 获取输出目录 要运行的npm命令 项目的dist编译目录.
func GetEnv() *ConfigEnv {
configEnv := new(ConfigEnv)
configEnv.Path = Config.GetString("output")
configEnv.Script = Config.GetString("script")
configEnv.Dist = Config.GetString("dist")
configEnv.Branch = Config.GetString("branch")
configEnv.LogPath = Config.GetString("logPath")
configEnv.PackageManager = Config.GetString("package-manager")
configEnv.AdditionalScript = Config.GetString("additional-script")
configEnv.AdditionalScriptArgs = Config.GetStringSlice("additional-script-args")
return configEnv
}
// GetToken 获取token.
func GetToken() *string {
token := Config.GetString("token")
return &token
}
// GetPort 获取服务运行的端口
func GetPort() string {
port := Config.GetString("port")
if port != "" {
port = strings.Join([]string{":", port}, "")
} else {
port = ":8079"
}
return port
}