I have a Go codegen tool and while building it as a binary I want to inject it's own current Pseudo-version for it's main module at build time using Go the tools.
I know that for dependencies, I can find their pseudo-version using go list -m all or go list -m -f '{{ .Version }}' $MODULE. The version for the main module is listed as empty.
I know that I can probably calculate the current pseudo version with:
git describe --tags `git rev-list --tags --max-count=1`
and:
TZ=UTC git --no-pager show \
--quiet \
--abbrev=12 \
--date='format-local:%Y%m%d%H%M%S' \
--format="%cd-%h"
To get a reasonable approximation like: v0.3.0-20210907161133-495c072cb418
After I have the Pseudo-version, I intend to inject it at build time like this:
go build -ldflags="-X generate.version v0.3.0-20210907161133-495c072cb418" -o mytool ./main.go
However, the recommendation is to use a Go tool directly to get the current Pseudo-version rather than some hacky bash thing I worked out on a napkin. Any suggestions as to how to do this?