building cli applications with golang

7

Click here to load reader

Upload: anshul-patel

Post on 24-Jan-2018

201 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: Building CLI Applications with Golang

Developing CLI Apps with

GolangAnshul Patel

Page 2: Building CLI Applications with Golang

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

Page 3: Building CLI Applications with Golang

CLI Structure

● Appname: Name of App

● Subcommand: Represents

action

● Flags: Modifiers for action

● Arguments: Input on which

actions will be applied.

Example:

Page 4: Building CLI Applications with Golang

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)

Page 5: Building CLI Applications with Golang

Popular Golang CLI Frameworks

● spf13/Cobra

● urfave/cli

● docopt/docopt.go

● mitchellh/cli

Page 6: Building CLI Applications with Golang

Cobra CLI Framework Development Flow

Page 7: Building CLI Applications with Golang

Thank You!

References

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

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

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