how to attach the pkg revision during go install

75 views
Skip to first unread message

Xie Yuchen

unread,
Aug 20, 2024, 12:06:29 AM8/20/24
to golang-nuts
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. 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  

Brian Candler

unread,
Aug 20, 2024, 5:01:31 AM8/20/24
to golang-nuts
As a reference, you could look at how prometheus does it:

% ~/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:
I couldn't see how to make it select a specific version though:


% go install github.com/prometheus/prometheus/cmd/prome...@v0.54.0
go: downloading github.com/prometheus/prometheus v0.54.0
go: github.com/prometheus/prometheus/cmd/prome...@v0.54.0 (in github.com/prometheus/prome...@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.

Reply all
Reply to author
Forward
0 new messages