12 Factor CLI Apps with Cobra - Markus Zimmermann - cosee

6e3ea86995d93d35c0fadf2694bca773?s=47 GoDays
January 23, 2020

12 Factor CLI Apps with Cobra - Markus Zimmermann - cosee

CLIs had a bit of a renaissance in recent years with powerful tooling like Docker and Kubernetes. They are faster and more productive to use than their visual counterparts. What makes these CLIs that good? Heroku defined 12 factors for great applications and did the same for CLIs. Good help documentation and shell completion are, for example, a very important factor. Modern CLIs take these factors into account to improve the user experience. Besides great UX, a powerful library is needed and Cobra takes this spot for Go. All the popular CLIs written in Go use Cobra.
In this talk, we want to look at the 12 factors that make CLIs successful and show how to implement a great CLI in Cobra.



January 23, 2020


  1. 2.

    About me Cloud Developer Master Thesis about npm Security (Analysis

    written in Go) Co-organizer Gophers Darmstadt
  2. 5.

    Nope - still modern and very powerful Scripting with Bash

    CI/CD automation Machine Readable Output Usable for API interactions Rapid prototyping
  3. 8.

    Bad UX in CLIs Do you remember how to extract

    a tar? tar -C /usr/local -xzf sample.tar.gz
  4. 9.

    “A user interface is like a joke. If you have

    to explain it, it’s not that good.” Martin LeBlanc, CEO of Iconfinder
  5. 13.

    Cobra Features Automatic generation of bash completion, man pages and

    help Intelligent suggestions Different flag types Code generator
  6. 14.

    Viper integration App Configuration Reads config files Reads from Environment

    Variables Reading from remote config systems like etcd Live watching and re-reading
  7. 15.
  8. 18.

    Great help is essential $ git $ git --help $

    git help $ git -h # get help for subcommand $ git subcommand --help $ git subcommand -h
  9. 19.

    Determine based on number of arguments (1 argument is fine)

    Order of flags can be random - arguments not Prefer flags to args copy --from /tmp --to /home
  10. 21.

    Mind the streams stdout get written to file or can

    be piped stderr get printed on terminal stdin accepted as input for root command
  11. 22.

    Handle things going wrong Informative Errors: Error Code Error title

    Fix for the Error URL with more information
  12. 23.

    Prompt if you can Never require a prompt - impedes

    automation! Dangerous actions can require a prompt Prompts can be visualized with Checkboxes or Radio buttons promptui https://github.com/manifoldco/promptui
  13. 24.

    And some more... Be fancy! Use tables Be speedy! Encourage

    contributions Be clear about subcommands Follow XDG-spec