12 Building a command-line interface

 

This chapter covers

  • Core components of command-line interfaces
  • Introducing the Cobra framework
  • Creating command skeletons
  • Producing a CLI that replaces the combined use of the main.go and curl programs

Throughout the book, we have been using a crude main program to operate our orchestration system (figure 12.1). This program is a single monolith that does it all: starts the workers, starts the workers’ API servers, starts the manager, and starts the manager’s API server. If we want to stop the manager for any reason, we also stop the workers, and vice versa. When we want to interact with the orchestrator, however, we do this separately via the curl command.

Figure 12.1 The old way of operating and interacting with our orchestrator
12-01

Now that we have our orchestration system implemented, let’s turn our attention to how we operate it. Popular orchestration systems like Kubernetes and Nomad are operated by command-line interfaces (CLIs). Kubernetes uses multiple binaries to implement its CLI:

  • kubeadm bootstraps a Kubernetes cluster.
  • kubelet performs the same function as our worker (i.e., it runs tasks).
  • kubectl provides an interface to interact with the cluster.

Nomad, on the other hand, implements its CLI with a single binary called nomad but otherwise provides a similar interface.

Our task in this chapter will be to implement a CLI for the Cube orchestrator. This CLI will support the following operations:

12.1 The core components of CLIs

12.2 Introducing the Cobra framework

12.3 Setting up our Cobra application

12.4 Understanding the new main.go

12.5 Understanding root.go

12.6 Implementing the worker command