Contributing to something always feels good, changes that are big or small, there is always something you can add to advance a project. I chose to work with Tony’s Go URL Checker once again and I ended up settling on adding a custom User-Agent flag to his tool. The changes I made which I will discuss can be found here.
Starting The Process
I already did a pull request earlier on this exact project about a week ago, but since then, Tony and other contributors have added a ton of new features to the tool. I had to figure out how to pull from the original repo using the upstream, after that I could get to work. Using Go’s http package I was able to create a request and add custom headers via Header.set.
req, err := http.NewRequest("HEAD", link, nil) req.Header.Set("User-Agent", userAgent) resp, err := client.Do(req)
So adding a custom header is nice and all but what is a User-Agent? Well, a User-Agent is a header sent in your request which carries a string that identifies which browser you are using, what version, and on which operating system. So I added the functionality for custom User-Agent strings, but how do you use it? Using the the existing FlagSet for the check command, I added the flag
-u to the tool which takes a string which will be your custom User-Agent, if one isn’t provided it reverts to Go’s default UA:
userAgent := checkCmd.String("u", "Go-http-client/1.1", "custom user-agent") checkCmd.Parse(flags) fmt.Println("Using User-Agent:", *userAgent)
Now you can pass a custom UA like so:
./linkDetector check -u "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0" -f [file-path]
Submitting a Pull Request
Getting my pull request accepted was not difficult, I submitted my changes and Tony had some suggestions on how to change the code to fit their preferred style. Originally I used a
StringVar flag which takes the address of a string and then changes it if a flag is passed, this was requested to be turned into a normal
String flag which doesn’t take a string but assigns it’s value to the pointer of a string instead. With those changes done I pushed my changes to my repo again and waited for a response, suddenly it was merged and my work was accepted. Thankfully this process went quite smoothly and there were no issues to be had but perhaps I could have gotten to work a little sooner :^)
Receiving Pull Requests
I received two submissions for WISA both of which were very nicely implemented and were in need of no touching up.
First I received a PR from Andy who implemented a new flag library for shorthand and long flags such as
--version and also added an http request timeout for requests which took too long as the previous requests just waited and waited until eventually the request timed out.
The second PR I received was from Isabella who got to work removing duplicate links to avoid running multiple of the same queries. Using a map with the urls as a key was very nice method for removing duplicate urls and so I merged the request immediately as there were no issues with the code.
With this work done, hopefully I can get into the groove of Hacktoberfest and contribute to some nice projects.