devOpifex / cranlogs

Cranlogs
MIT License
7 stars 2 forks source link

feat: add version information #6

Closed dpastoor closed 2 years ago

dpastoor commented 2 years ago

By setting the version information, this autopopulates the -v and --version flags to be useable

for example, now:

❯ go run main.go --version
dev

This should fix the build failures https://github.com/devOpifex/cranlogs/runs/5147713836?check_suite_focus=true:

Setting up cranlogs (0.0.2~next) ...
Error: unknown flag: --version
Usage:
unknown flag: --version
dpastoor commented 2 years ago

how this works is we're leveraging the fact that goreleaser by default provides to build command with:

https://goreleaser.com/customization/build/

# Default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`.

To read this - its saying that it will look for the variables version commit and date in the main file (main.version) and inject them with the environment variable(s) Version, Commit, and Date, of which goreleaser injects automatically. So version will now be a variable in main, then we pass that in to the Execute function, and that sets the version information on the root command.

This version is something cobra does on the root automagically - https://github.com/spf13/cobra/blob/master/user_guide.md#version-flag

Cobra adds a top-level '--version' flag if the Version field is set on the root command. Running an application with the '--version' flag will print the version to stdout using the version template. The template can be customized using the cmd.SetVersionTemplate(s string) function

dpastoor commented 2 years ago

Looks like it worked!

JohnCoene commented 2 years ago

I'm not used to collaborating on Go stuff so I'm not sure how this works. Is there any way to have you credited for what you've done?

Maybe at least cobra persistent flags for authorship?

If you have any idea, feel free to make a PR

dpastoor commented 2 years ago

hmm, yeah I actually don't know, I've likewise never tied individual contributors to like an authors field or the like. I'm good enough with the fact my icon appears in the contributors section on github

Least I could do given the amount of stuff you've pushed out into the community :-)

JohnCoene commented 2 years ago

Well thank you, I owe you one!