Building a Network Command Line Interface in Go
In this article, we are going to be building a very simple Command Line Interface in Go using the urfave/cli
package available on Github here: https://github.com/urfave/cli.
I’ve been doing one or two domain migrations across various hosting providers recently and thought it would be a cool idea to build a tool or program that could be used to query things like the Nameservers of a website, the CNAMEs, the IP addresses and so on.
The overall aim of this particular tutorial is to give you an idea as to how you can build your own CLIs that could do a wide variety of other things such as network monitoring, image manipulation and so on.
Popular Projects
Golang is growing massively in popularity and we have seen large enterprise companies such as Hashicorp adopt the language for quite a number of different tools and systems. And for good reason, the design of Go lends itself incredibly well to these styles of application and the ability to cross-compile a binary executable for all major platforms easily is a massive win.
Video Tutorial
If you prefer learning through the medium of video, then feel free to check out this tutorial here:
Getting Started
Let’s create a new directory on our computer called go-cli/
or something along those lines. We’ll be creating a directory structure that will look like this for our project:
|
|
Note - This structure follows the widely accepted Go project layout guide available on Github.
Getting Into The Code
Now that we’ve got a basic project structure down, we can start to work on our application. First of all, we will need a new file called cli.go
within our new cmd/my-cli/
directory. We’ll populate this with a very simple Hello World
type of application and use this as the base from which we’ll grow from.
|
|
We can then attempt to run this from our project’s root directory by typing:
|
|
Excellent, we’ve got the makings of our new CLI sorted, let’s now look at how we can add a few commands and make it somewhat useful.
Our First Command
As we’ll be using the urfave/cli
package we’ll need to download this package locally in order to use it, we can do that through a simple go get
command like so:
|
|
Now that we have the necessary package, let’s update our cli.go
file to use this package and create a new CLI application for us:
|
|
When we run this now, you’ll see it fleshes out our programs response and adds things like the version, how we use the cli and the various commands we have at our disposal.
|
|
Awesome, this is very quickly starting to look like a more polished project and not just a minor side project!
We can now start adding our own Commands
. Each of these commands will match up with one of our tests, so we’ll have one command: ns
which, when triggered and supplied with a url
will go off and lookup the Name Servers of that particular host.
Our final list of commands will look something like this:
ns
- will retrieve the name serverscname
- will lookup the CNAME for a given hostmx
- will lookup the mail exchange records for a given hostip
- will lookup the IP addresses for a given host.
Nice and simple, let’s get started by creating our first command:
|
|
We can then try to run this by typing:
|
|
This should then return the name servers for my site and print them out in the terminal. We can also do a run the help command which will show us exactly how to use our new command within our CLI.
Looking up IP Addresses
All of our command definitions will look really similar within our program, with the exception of how we go about printing out the results. The net.LookupIP()
function returns a slice of IP addresses and as such we’ll have to iterate over these in order to print them out in a nice fashion:
|
|
Looking up our CNAME
We can then add our cname
command which will use the net.LookupCNAME()
function with our passed in host and return a single CNAME string which we can then print out:
|
|
Looking up MX Records
Finally, we want to be able to query the Mail Exchange records for our given host, we can do that by using the net.LookupMX()
function and passing in our host. This will return a slice of mx records which, like our IPs, we’ll have to iterate over in order to print out:
|
|
Building our CLI
Now that we have a basic CLI up and running, it’s time to build it so that we can use it in anger.
|
|
This should compile a cli
executable which we can then run like so:
|
|
As you can see, all of our commands have been successfully listed in the COMMANDS section of the output.
Conclusion
So, in this tutorial we’ve managed to successfully build a really simple, yet effective CLI using the urface/cli
package from Github. The CLI can be cross-compiled for any of the major operating systems with minimal fuss and it features all the functionality that you would expect from a production-grade command line interface.