Write an Agent Command-Line Interface
Distributed Services with Go — by Travis Jeffery (74 / 84)
👈 Use Kind for Local Development and Continuous Integration | TOC | Build Your Docker Image 👉
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:
DeployLocally/cmd/proglog/main.go
package main
import (
"log"
"os"
"os/signal"
"path"
"syscall"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/travisjeffery/proglog/internal/agent"
"github.com/travisjeffery/proglog/internal/config"
)
func main() {
cli := &cli{}
» cmd := &cobra.Command{
» Use: "proglog",
» PreRunE: cli.setupConfig,
» RunE: cli.run,
» }
if err := setupFlags(cmd); err != nil {
…