IO /5-keys-to-a…

The author of this article is the author of OpenFaaS. In the original article, the author combined with his own experience in developing OpenFaaS to explain what should be paid attention to CLI. In the translation process, many of them have been omitted for the convenience of understanding, but it does not hinder the overall understanding of the author to express the intention and method of using Go to create a good CLI.

The CLI (command line interface) is a text interface that provides a fast, automated way to interact with applications and create new workflows with other command line interfaces.

Select Go to create a CLI

Advantages of choosing Go to create CLI

Compiles to a single static binary – the ability to compile into a single binary package

Go can be easily distributed as a binary package depending on the platform. Depending on the platform, you only need to provide different environment variables at compile time:

GOOS=windows go build -o cli.exe
GOOS=linux go build -o cli
GOARCH=armv7 GOOS=linux go build -o cli-rpiCopy the code

Consistent style – A Consistent style

No matter what editor your project is based on, Go always provides a consistent code style, unlike Nodejs, which has a dizzying array of different task runners.

Go’s design is unambiguous in terms of style preservation, making it easy for developers to collaborate.

Fast on every platform – Fast on any platform

The compiled Go binary package loads very quickly compared to Nodejs.

Easy to create a REST Client – It is very Easy to create a RESTFUL client

Go provides a very rich HTTP client with built-in support for XML, JSON, and third-party libraries in the community that provide YAML support

Parse flags & arguments

The Go library provides the Flags package to create CLI applications:

package main
import (
	"flag"
	"fmt"
	"os"
)
func main() {
	var image string
	flag.StringVar(&image, "image", "", "Docker image")
    flag.Parse()
    
	if len(image) == 0 {
		fmt.Fprintf(os.Stderr, "You must specify a Docker image name")
	}
	fmt.Printf("Your Docker image was: %s", image)
}Copy the code

Go’s simplicity allows you to compile binary packages for execution using only one file.

If you feel that the features provided by the Flags package are no longer sufficient for your CLI, you can consider using Cobra.

Cobra is used by well-known open source software like Docker and Kubernetes, and Cobra also makes it very easy to create CLI documents. In addition, Cobra supports verb noun syntax, which helps improve the CLI user experience:

faas-cli -deploy -image=functions/alpine -name=cat -fprocess=/bin/catCopy the code

To:

faas-cli deploy --image=functions/alpine --name=cat --fprocess=/bin/catCopy the code

Automate everything

Automate build jobs using a free and publicly available CI platform such as Travis that could allow a Ficolin-3 to detect whether their contributions can be integrated.

Use Github Release to track project changes and milestones. You can create a post-build action in Travis to automatically package and publish applications for each platform.

If you’re using Docker, release the Docker image every time you release a new release.

Integration package manager

If you want your audience to be very easy to use your CLI, it’s best to have your CLI support installed using package manager:

  • Mac

    Most developers have Mac environments, so providing BREW support is a smart choice

  • Linux

    For Linux can provide installation and use of the command line, such as curl – sL https://cli.openfaas.com | sh

  • Windows

    Most Windows users prefer to use the installation method, Windows platform good shell tools are increasing, you can also consider providing a shell installation method

Regardless of which platform you support package management, make it a priority to ensure that your work is automated and that upgrades are smooth.

Accept community contributions and collect feedback

Provide users with a very convenient way to feedback. Basic feedback and statistics can be done on Github or BREW. Some key projects are where feedback can be collected via third party tools such as BREW, Visual Studio Code, etc. :

  • Which command is used
  • OS, CLI version, Location, etc