Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): display version without initializing app #463

Merged
merged 1 commit into from
Oct 30, 2024

Conversation

Moraxyc
Copy link
Contributor

@Moraxyc Moraxyc commented Oct 30, 2024

This PR separates "showing version" from the entire program.

According to the original code, running "./dashboard -v" will produce the following errors:

panic: open data/config.yaml: no such file or directory

goroutine 1 [running]:
github.com/naiba/nezha/service/singleton.InitConfigFromPath(...)
github.com/naiba/nezha/service/singleton/singleton.go:49
main.init.0()
github.com/naiba/nezha/cmd/dashboard/main.go:37 +0x20b

In Go, the init() will run before the main(), and some codes in init() initialize the service. Therefore, we can check the version flag in the init stage, and after the version is displayed, the program exits.

@uubulb
Copy link
Contributor

uubulb commented Oct 30, 2024

I think the the program should exit after printing version number. That's the common case

@Moraxyc Moraxyc force-pushed the fix-version-display branch 3 times, most recently from d2a6847 to 4b09e28 Compare October 30, 2024 11:45
@Moraxyc
Copy link
Contributor Author

Moraxyc commented Oct 30, 2024

please take a look again @uubulb

@uubulb
Copy link
Contributor

uubulb commented Oct 30, 2024

please take a look again @uubulb

wait for the maintainer to review

@naiba
Copy link
Member

naiba commented Oct 30, 2024

可以把 init 里面的东西放到 main 中延后初始化而不是把 main 中的东西放到 init 哈
init 都懒用了

@Moraxyc
Copy link
Contributor Author

Moraxyc commented Oct 30, 2024

直接不使用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()
...

@naiba
Copy link
Member

naiba commented Oct 30, 2024

是的

@naiba
Copy link
Member

naiba commented Oct 30, 2024

与运行时无关的可以放到 init,与运行时状态有关的是要放在 main 的

@naiba naiba merged commit 61fdcd2 into nezhahq:master Oct 30, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants