Skip to content

Read Go build info from the binary #179

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

Closed
wants to merge 1 commit into from
Closed

Conversation

lucacome
Copy link
Contributor

@lucacome lucacome commented Aug 8, 2022

Uses build info from runtime/debug, so it doesn't need to be injected at build time.

This is possible since go 1.18

@lucacome lucacome added the chore Pull requests for routine tasks label Aug 8, 2022
@lucacome lucacome requested a review from a team August 8, 2022 20:50
@lucacome lucacome self-assigned this Aug 8, 2022
@lucacome lucacome force-pushed the chore/build-info branch 2 times, most recently from 80e6f99 to 7c0c146 Compare August 8, 2022 21:26
if !ok {
return "unknown", "unknown", true
}
for _, kv := range info.Settings {
Copy link
Contributor

Choose a reason for hiding this comment

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

(sorry, missed this in the first review)

is there are a chance any of those settings are not present in info.Settings ? in that case, are we're ok with reporting empty strings? perhaps "unknown" would work better?

do we want a unit test to test this logic?

wonder what @kate-osborn thinks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, good catch! I've updated it

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should add unit tests wherever possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand it that information is not available when running the tests. We could manually build the binary and then check that the info is present, but maybe it doesn't belong in a unit test?

Copy link
Contributor

Choose a reason for hiding this comment

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

one approach is like below:

type buildInfoFunc func() (info *debug.BuildInfo, ok bool)

var buildInfo buildInfoFunc = debug.ReadBuildInfo

func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
	commitHash = "unknown"
	commitTime = "unknown"
	dirtyBuild = "unknown"

	info, ok := buildInfo()
	if !ok {
		return
	}
	for _, kv := range info.Settings {
		switch kv.Key {
		case "vcs.revision":
			commitHash = kv.Value
		case "vcs.time":
			commitTime = kv.Value
		case "vcs.modified":
			dirtyBuild = kv.Value
		}
	}

	return
}

this way it will be possible to write and use a stub of buildInfo to use in the unit test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about this, and if we can't test the actual values, there's no logic (that we implemented) to test in this function as there's just a loop reading values from strings. Right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see a few cases here:

  • normal case: buildInfo() returns OK with values populated in info.Settings
  • edge case: buildInfo() returns not OK
  • edge case: buildInfo() returns OK without values got populated info.Settings

in all those cases, we can ensure that the getBuildInfo() returns the expected values. There is some amount of logic, since we have if and a loop.

@lucacome lucacome force-pushed the chore/build-info branch 3 times, most recently from 073f63c to f95f18b Compare August 17, 2022 22:40
@lucacome lucacome force-pushed the chore/build-info branch 2 times, most recently from 491c4f2 to 789eeb8 Compare July 14, 2023 03:02
@github-actions
Copy link
Contributor

github-actions bot commented Jul 14, 2023

Dependency Review

✅ No vulnerabilities or license issues found.

Scanned Manifest Files

@pleshakov pleshakov self-assigned this Jul 21, 2023
@sjberman
Copy link
Collaborator

sjberman commented Sep 21, 2023

Is this still needed? @lucacome

@lucacome
Copy link
Contributor Author

yes @sjberman it would be nice to remove some extra variables. The only thing missing are the tests.

FWIW we've been using the same in multiple other projects and it works 👀

@sjberman
Copy link
Collaborator

@lucacome Ok cool, if it can be rebased and feedback addressed (on your time), I say we get this in since it's been sitting here for awhile.

Copy link
Contributor

github-actions bot commented Feb 7, 2024

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale Pull requests/issues with no activity label Feb 7, 2024
Copy link
Contributor

This PR was closed because it has been stalled for 14 days with no activity.

@github-actions github-actions bot closed this Feb 21, 2024
@lucacome lucacome mentioned this pull request Jul 31, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Pull requests for routine tasks stale Pull requests/issues with no activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants