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

Add --version flag #59

Closed
jimafisk opened this issue Aug 23, 2020 · 7 comments
Closed

Add --version flag #59

jimafisk opened this issue Aug 23, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@jimafisk
Copy link
Member

It would be great if users could see which version of plenti they are using: plenti --version

Currently releases are created automatically based on the git tag used, it would be ideal if the cmd could autodiscover that tag and use it. I would like to avoid having to manually increment versions every deploy because I'm likely to forget.

Potentially use https://github.com/christopherhein/go-version

@jimafisk jimafisk added the enhancement New feature or request label Aug 23, 2020
@jimafisk
Copy link
Member Author

Discussion on automatically updating git tag versions: https://www.reddit.com/r/golang/comments/4cpi2y/question_where_to_keep_the_version_number_of_a_go/

Since we're already using go generate that seems like an approachable way to accomplish this. This article describes the steps:

  1. Tag your git repository with a version number (ideally based on the semantic versioning specification e.g. v0.0.0): git tag -a v0.0.0 -m "First tag."
  2. Push the version number to the remote repository: git push --tags
  3. Generate an up-to-date version_PLATFORM.go file (normally done by your CI server): go generate (The version_PLATFORM.go file gets automatically updated with data from the tag)
  4. Build as normal: go build

@jimafisk
Copy link
Member Author

Looks like the example repo from the article above now recommends using ldflags instead: https://www.atatus.com/blog/golang-auto-build-versioning/

@jimafisk
Copy link
Member Author

jimafisk commented Aug 23, 2020

When enabling "Run" on the rootCmd I was having trouble optionally printing the default info if no --version flag was passed. I could have set up a separate command for "version" (run like plenti version) but I thought this worked better as a flag (run like plenti --version or plenti -v). This post shows you can simply print the default using cmd.Help(). That allows you to continue running plenti to find basic information about the project and how to use it.

@jimafisk
Copy link
Member Author

Helpful article from DO: https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications

Locally I can set values available in rootCmd like so: go install -ldflags="-X 'plenti/cmd.Version=test'"

jimafisk added a commit that referenced this issue Aug 23, 2020
@jimafisk
Copy link
Member Author

Needed to fix the build command to use following format:

go install -ldflags="-X 'plenti/cmd.Version=$(git describe --tags)'"

@jimafisk
Copy link
Member Author

It seems .goreleaser.yml is reading the -ldflags as a string, so running plenti -v just prints "git describe --tags" and not the evaluated output of that expression (e.g. v0.2.31). I also tried using backtics but had the same result:

go install -ldflags="-X 'plenti/cmd.Version=`git describe --tags`'"

@jimafisk
Copy link
Member Author

Looks like we can use a Name Template for this: https://goreleaser.com/customization/templates/

Something like -X main.version={{.Tag}} should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant