Closed Moraxyc closed 3 weeks ago
I think the the program should exit after printing version number. That's the common case
please take a look again @uubulb
please take a look again @uubulb
wait for the maintainer to review
可以把 init 里面的东西放到 main 中延后初始化而不是把 main 中的东西放到 init 哈 init 都懒用了
直接不使用init嘛? @naiba
大概这样?
func main() {
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.BoolVarP(&dashboardCliParam.Version, "version", "v", false, "查看当前版本号")
flag.StringVarP(&dashboardCliParam.ConfigFile, "config", "c", "data/config.yaml", "配置文件路径")
flag.StringVar(&dashboardCliParam.DatebaseLocation, "db", "data/sqlite.db", "Sqlite3数据库文件路径")
flag.Parse()
if dashboardCliParam.Version {
fmt.Println(singleton.Version)
os.Exit(0)
}
// 初始化 dao 包
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath(dashboardCliParam.DatebaseLocation)
singleton.InitLocalizer()
initSystem()
// TODO 使用 cmux 在同一端口服务 HTTP 和 gRPC
singleton.CleanMonitorHistory()
...
是的
与运行时无关的可以放到 init,与运行时状态有关的是要放在 main 的
This PR separates "showing version" from the entire program.
According to the original code, running "./dashboard -v" will produce the following errors:
In Go, the
init()
will run before themain()
, and some codes ininit()
initialize the service. Therefore, we can check the version flag in the init stage, and after the version is displayed, the program exits.