building cli applications with golang

Post on 24-Jan-2018

201 Views

Category:

Internet

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Developing CLI Apps with

GolangAnshul Patel

Why Golang for CLI?

● Cross Platform Compilation. (Windows, Linux, Mac, BSD, Solaris)

● Dependencies can be statically linked & packaged into single binary.

● Execution performance is close to C/C++.

● Popular CLI Apps in Golang:

○ Packer

○ Terraform

○ Consul

○ Kubernetes

○ Prometheus

○ Docker

○ Hugo

○ etcd

CLI Structure

● Appname: Name of App

● Subcommand: Represents

action

● Flags: Modifiers for action

● Arguments: Input on which

actions will be applied.

Example:

Why CLI Framework?

● Provides libs/functions/boilerplate for following:

○ Subcommands and nested subcommands. (e.g. git clone/pull/push)

○ POSIX compliant flags. (e.g. git clone -q/--quiet)

○ Ability to generate help documentation from the comments. (e.g. git clone/pull --

help)

○ Ability to generate man pages. (man git)

○ Ability to generate bash autocomplete. (/etc/bash_completion.d/)

○ Intelligent suggestions. (git clne → Did you mean this ? → git clone)

○ Provide inline and configuration file support. ($HOME/.app.yaml)

Popular Golang CLI Frameworks

● spf13/Cobra

● urfave/cli

● docopt/docopt.go

● mitchellh/cli

Cobra CLI Framework Development Flow

Thank You!

References

● https://github.com/spf13/cobra

● https://github.com/avelino/awesome-go

● https://github.com/anshulpatel25/calccli-go

top related