caicloud / nirvana

Golang Restful API Framework for Productivity

Home Page:https://caicloud.github.io/nirvana/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[proposal] Cobra style command line interface and project layout

lichuan0620 opened this issue · comments

/kind design
/area cli

This issue proposes a new, Cobra-style CLI and a project layout that goes with it.

The Proposed Design

Right now, this is the recommended Nirvana project layout:

.
├── bin                           # binaries reside within project directories
│   └── nirvana-myproject         #
├── build                         # Dockerfiles reside within project directories
│   └── nirvana-myproject         #
│       └── Dockerfile            #
├── cmd                           # main packages reside within project directories
│   └── nirvana-myproject         #
│       └── main.go               #
├── pkg                           # packages
│   ...                           # other stuff

This encourages the user to build multiple binaries and house them within multiple container images. The CLI of each binary would support different flags and args.

With Cobra, however, encourage the use of command in support of flags and args. Basically, instead of this:

$ monitoring-init --mongo-endpoint=$(MONGO_ENDPOINT)
$ monitoring-server --port=8080 --mongo-endpoint=$(MONGO_ENDPOINT)
$ monitoring-operator --resync-period=5m

Cobra would do this:

$ monitoring init --mongo-endpoint=$(MONGO_ENDPOINT)
$ monitoring serve --port=8080 --mongo-endpoint=$(MONGO_ENDPOINT)
$ monitoring operate --resync-period=5m

And the project layout would look like this:

.
├── cmd                           # cmd package keeps all the commands
│   └── init.go                   # sub command init
│   └── serve.go                  # sub command serve
│   └── operate.go                # sub command operate
├── pkg                           # packages
├── Dockerfile                    # only one Dockerfile
├── main.go                       # only one main package located at the project root
├── project_bin                   # only one binary
│   ...                           # other stuff

Why Bother?

  1. Fewer steps during CICD. Only one binary and one Dockerfile.
  2. Easier delivery and version management. You could send someone an image, only to find out later that they need more, and have to send them more, and have to worry about if they have the compatible versions.
  3. Easier to learn. Cobra-style project layout and CLI are already massively popular, being adopted by Docker, Kubernetes, and many popular open-source projects. Why should we do things differently?
  4. Nirvana CLI is based on Cobra anyway, so why don't we focus on its core functionality and just use Cobra as it is?

/cc @ddysher @iawia002 @supereagle