[GH-ISSUE #56] Adding comments to hosts records #17

Open
opened 2026-03-02 05:08:09 +03:00 by kerem · 1 comment
Owner

Originally created by @Alvov1 on GitHub (Oct 21, 2025).
Original GitHub issue: https://github.com/goodhosts/hostsfile/issues/56

I'd like to get an option to write a hosts record with comment. In my organization we're adding a lot of hosts records through our cli tool, and we want to specify that this record relates to our stuff

func (h *Hosts) AddCommented(ip string, comment string, hosts ...string) error { ... }

hosts, err := hostsfile.NewHosts()
hosts.AddCommented("5.226.232.29", "added-by-ORG", "environment-10-proxy")

5.226.232.29 environment-10-proxy # added-by-ORG

Originally created by @Alvov1 on GitHub (Oct 21, 2025). Original GitHub issue: https://github.com/goodhosts/hostsfile/issues/56 I'd like to get an option to write a hosts record with comment. In my organization we're adding a lot of hosts records through our cli tool, and we want to specify that this record relates to our stuff ```go func (h *Hosts) AddCommented(ip string, comment string, hosts ...string) error { ... } hosts, err := hostsfile.NewHosts() hosts.AddCommented("5.226.232.29", "added-by-ORG", "environment-10-proxy") ``` > 5.226.232.29 environment-10-proxy # added-by-ORG
Author
Owner

@luthermonson commented on GitHub (Oct 23, 2025):

you can use AddRaw. it supports parsing a # and making a comment from the rest of the line since it uses NewHostsLine which supports parsing out the comment. Try something like the following...

hosts.AddRaw("5.226.232.229 environment-10-proxy # added by ord")

if you really want a func with that signature you could wrap AddRaw in your own code or if you really want AddCommented in this package feel free to put up a PR for it and I would merge it

<!-- gh-comment-id:3435018710 --> @luthermonson commented on GitHub (Oct 23, 2025): you can use [AddRaw](https://github.com/goodhosts/hostsfile/blob/main/hosts.go#L132C17-L132C23). it supports parsing a `#` and making a comment from the rest of the line since it uses [NewHostsLine](https://github.com/goodhosts/hostsfile/blob/main/hostsline.go#L26-L30) which supports parsing out the comment. Try something like the following... ``` hosts.AddRaw("5.226.232.229 environment-10-proxy # added by ord") ``` if you really want a func with that signature you could wrap AddRaw in your own code or if you really want AddCommented in this package feel free to put up a PR for it and I would merge it
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hostsfile#17
No description provided.