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

fix: version command #135

Merged
merged 8 commits into from Dec 29, 2023
Merged

fix: version command #135

merged 8 commits into from Dec 29, 2023

Conversation

limesten
Copy link
Contributor

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Problem/Feature

the command go-blueprint version works fine when downloading the pre-build binary from a release.
However, if doing go install github.com/melkeydev/go-blueprint@latest it seems to result in an empty GoBlueprintVersion string.

Description of Changes:

lost a few hairs on my head investigating this but:

  • Added a function called getGoBlueprintVersion
    If the version var has been set by goreleaser it will use that
    else we will use ReadBuildInfo from the runtime/debug package (which gets the version from git tags)
    also added a fallback which just prints version unknown

Checklist

  • I have self-reviewed the changes being requested
  • I have updated the documentation (if applicable)

Copy link
Owner

@Melkeydev Melkeydev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying out this branch and I am getting the following:

  ~/Documents/Personal/Go/test                                                                                                                                                             
❯ go-blueprint version
Go Blueprint CLI version (devel)

Is this expected? I am unsure what i did


"github.com/spf13/cobra"
)

// GoBlueprintVersion is the version of the cli to be overwritten by goreleaser in the CI run with the version of the release in github
var GoBlueprintVersion string

func getGoBlueprintVersion() string {
if len(GoBlueprintVersion) != 0 {
return GoBlueprintVersion
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably define this inline and not have a separate variable declared on line 14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But GoBlueprintVersion needs to be on package level to be able to be set by the ldflag in .goreleaser.yml?
Or am I misunderstanding you?

cmd/version.go Outdated
if info, ok := debug.ReadBuildInfo(); ok {
return info.Main.Version
}
return "unknown"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe we can return something else rather than "unknown" since that is a big ambiguous

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, will try to come up with something better

@limesten
Copy link
Contributor Author

I am trying out this branch and I am getting the following:

  ~/Documents/Personal/Go/test                                                                                                                                                             
❯ go-blueprint version
Go Blueprint CLI version (devel)

Is this expected? I am unsure what i did

For the info.Main.Version to embed the version from the git tag it needs to be installed like:
go install github.com/melkeydev/go-blueprint@latest (or with the specific version instead of latest)

If we install like that and then run go version -m go-blueprint on the binary in the bin folder we get:

image

However, if we run `go install` locally with the source code we will get `(devel)`

image

@muandane
Copy link
Contributor

i'm a bit confused to why it's not working also at build i tested the build locally
image

@limesten limesten marked this pull request as draft November 28, 2023 16:57
@limesten limesten marked this pull request as ready for review November 30, 2023 05:21
@limesten
Copy link
Contributor Author

limesten commented Nov 30, 2023

Thoughts on these changes?

  • Added more comments

  • Added an additional fallback using git revision hash and date

  • Also changed to a less ambiguous output if no version info is available

Also for additional info about getting (devel) from local builds with debug.ReadBuildInfo() there are these related issues on the golang repo:
golang/go#29228
golang/go#50603

@Melkeydev
Copy link
Owner

@limesten I am still investigating this but it seems it still does not work
image

@limesten
Copy link
Contributor Author

limesten commented Dec 14, 2023

@limesten I am still investigating this but it seems it still does not work image

I assume you are checking out the pr and then running go install. I've not found a good way to include the version info in this case.

It will work if:

  1. you make a release with goreleaser (for example goreleaser build --single-target --skip-validate --clean as a local test release)
    (or manually enter a ldflag when running go build as @muandane did)

  2. you install like this: go install github.com/melkeydev/go-blueprint@latest or @v.0.4.1 etc
    In this case the part after the @ is what gets added to the buildinfo so it will be accessible from the version command

The weird thing with 2) is that u cant't rly test it locally beforehand (as far as I can see at least) cuz this PR first needs to be included in a release so we can install it with go install github.com/melkeydev/go-blueprint@latest 😅 Hope that makes sense

@Ujstor
Copy link
Collaborator

Ujstor commented Dec 19, 2023

@Melkeydev This is working. I created a test repo, pushed the tag, the build passed and the version was correct. LGTM
Screenshot from 2023-12-19 07-22-20

Copy link
Owner

@Melkeydev Melkeydev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Melkeydev Melkeydev merged commit 9471913 into Melkeydev:main Dec 29, 2023
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants