Write an Agent Command-Line Interface

Our agent CLI will provide just enough features to use as a Docker image’s entry point and run our service, parse flags, and then configure and run the agent.

I use the Cobra[57] library to handle commands and flags because it works well for creating both simple CLIs and complex applications. It’s used in the Go community by projects such as Kubernetes, Docker, Helm, Etcd, Hugo, and more. And Cobra integrates with a library called Viper,[58] which is a complete configuration solution for Go applications.

The first step is to create a cmd/proglog/main.go file, beginning with this code:

 package​ main
 
 import​ (
 "log"
 "os"
 "os/signal"
 "path"
 "syscall" ...

Get Distributed Services with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.