As a reference, you could look at how prometheus does it: % go install github.com/prometheus/prometheus/cmd/prometheus % ~/go/bin/prometheus --version prometheus, version (branch: , revision: b914a9b580abd7bb7e879602c17fea0e06fd0266) ...
I know it uses a build utility (promu) to embed the info in the binary, but I haven't worked out how that hooks into the "go install" process, of if that's even supported. You could use these as starting points for investigation: https://github.com/prometheus/common/blob/v0.55.0/version/info.go#L25-L38 https://github.com/prometheus/prometheus/blob/v2.54.0/.promu.yml#L21-L26 https://github.com/prometheus/promu/blob/v0.17.0/pkg/repository/info.go#L63-L113 I couldn't see how to make it select a specific version though: % go install github.com/prometheus/prometheus/cmd/prometheus@v2.54.0 go: github.com/prometheus/prometheus/cmd/prometheus@v2.54.0: github.com/prometheus/prometheus@v2.54.0: invalid version: module contains a go.mod file, so module path must match major version ("github.com/prometheus/prometheus/v2") % go install github.com/prometheus/prometheus/v2/cmd/prometheus@v2.54.0 go: github.com/prometheus/prometheus/v2/cmd/prometheus@v2.54.0: github.com/prometheus/prometheus@v2.54.0: invalid version: module contains a go.mod file, so module path must match major version ("github.com/prometheus/prometheus/v2") % go install github.com/prometheus/prometheus/cmd/prometheus@v0.54.0 go: downloading github.com/prometheus/prometheus v0.54.0 go: github.com/prometheus/prometheus/cmd/prometheus@v0.54.0 (in github.com/prometheus/prometheus@v0.54.0): The go.mod file for the module providing named packages contains one or more replace directives. It must not contain directives that would cause it to be interpreted differently than if it were the main module. % go install github.com/prometheus/prometheus/cmd/prometheus@v2 go: github.com/prometheus/prometheus/cmd/prometheus@v2: no matching versions for query "v2" On Tuesday 20 August 2024 at 05:06:29 UTC+1 Xie Yuchen wrote: > Dear all > > I'm a 3rd cli tool developer, and most of my users install the cli tool > via *go install github.com/example/mycli > <http://github.com/example/mycli>*. I got trouble for attaching the > revision by default so users could run *mycli version *to get a > revision(tag or git commit). > > The known workarounds cannot solve my problem: > 1. put the tag inside the codebase: doesn't work for commit because i > cannot know the concrete commit before committing > 2. pass it by ldflag: doesn't work because i cannot ask users to attach > the version, that's quite dummy > > The ideal way is that during *go install *the go compiler could help > attach this information, but i haven't found this related docs. Could > anyone provide some ideas about it? > > If the ideal way is not possible, is it possible to get the revision after > users install my cli tool via go install? > > xieyuschen > Regards > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0dac3649-bccf-4bda-9629-17cb4d52e746n%40googlegroups.com.