-
Notifications
You must be signed in to change notification settings - Fork 44
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
Adding convenience method for FQDN-aware hostname #108
Conversation
💔 Build Failed
Expand to view the summary
Build stats
Steps errorsExpand to view the steps failures
|
0c72a0c
to
5cd25a7
Compare
|
||
// FQDNAwareHostname returns the system hostname, honoring the given | ||
// flag to report the FQDN or not. | ||
func (host HostInfo) FQDNAwareHostname(wantFQDN bool) string { |
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 don't think we need this function:
- We can access the fields directly
- This function has 2 behaviours based on a boolean flag, I'd avoid this pattern, it's better to have 2 functions then. But as I mentioned above, those 2 functions would be just unnecessary getter functions for the accessible/public fields.
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.
Funny, because I received exactly the opposite feedback in a very similar situation on another (related to FQDN) PR 😄:
Without a helper function like this, we will repeat the (trivial) conditional logic in a few places: elastic/elastic-agent@d2c0512
This function has 2 behaviours based on a boolean flag, I'd avoid this pattern, it's better to have 2 functions then.
Can you elaborate a bit on why this pattern should be avoided? What is the downside or "cost" of such a pattern?
I think helper functions like this one are not uncommon and it's fine to have them as long as they're documented and tested well. It helps avoid repeating the same logic in multiple places and risking getting it wrong. I know the logic is fairly trivial in this case but I'm not really sure what the harm is in having a helper function encapsulate it. And, of course, the underlying public fields are still available for direct access in situations where that would make more sense.
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.
Can you elaborate a bit on why this pattern should be avoided? What is the downside or "cost" of such a pattern?
sure, for example, when you see
FQDNAwareHostname(true)
you cannot tell what true
means without going to the function definition. That's why boolean flags as function arguments are commonly avoided. It's better to predefine constants (like a enum) and use them instead. A constant name is way more descriptive than a boolean.
So, it could become something like:
type HostnameMode int
const (
FQDN HostnameMode = 0
Short HostnameMode = 1 // this might have a better name
)
func Hostname(mode HostnameMode) string
and then it can be used like:
hn := Hostname(FQDN)
If we don't want to have a type alias, it's:
func Hostname(hostInfo types.HostInfo, mode HostnameMode) string
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, okay. Thanks @rdner, I misunderstood your original comment. I thought the "pattern" to avoid was having helper functions like this one altogether. I see what you mean now; it's more about the ergonomics of the helper function from a readability perspective.
f97aa58
to
a50fec5
Compare
|
||
import "github.com/elastic/go-sysinfo/types" | ||
|
||
type HostInfo types.HostInfo |
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 don't think it needs to alias the type to achieve the result of having a helper. You could have a standalone function that accepts a types.HostInfo
as a parameter (e.g. hostname.Get(types.HostInfo, mode hostname.Mode) string
).
With recent changes in how the |
What does this PR do?
This PR aliases the
github.com/elastic/go-sysinfo/types/HostInfo
type togithub.com/elastic/elastic-agent-libs/sysinfo/HostInfo
and adds a convenience method for reporting the FQDN-aware hostname.Why is it important?
To consolidate logic of reporting the appropriate hostname in a single place.
Also see discussion on elastic/go-sysinfo#156 (comment).
Checklist
CHANGELOG.md
Author's Checklist
Related issues