-
Notifications
You must be signed in to change notification settings - Fork 123
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
Conversation
80e6f99
to
7c0c146
Compare
cmd/gateway/setup.go
Outdated
if !ok { | ||
return "unknown", "unknown", true | ||
} | ||
for _, kv := range info.Settings { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
073f63c
to
f95f18b
Compare
491c4f2
to
789eeb8
Compare
Dependency Review✅ No vulnerabilities or license issues found.Scanned Manifest Files |
789eeb8
to
7b59408
Compare
Is this still needed? @lucacome |
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 👀 |
@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. |
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. |
This PR was closed because it has been stalled for 14 days with no activity. |
Uses build info from
runtime/debug
, so it doesn't need to be injected at build time.This is possible since go 1.18