Upgrade to Pro — share decks privately, control downloads, hide ads and more …

12 Factor CLI Apps with Cobra - Markus Zimmerma...

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.

GoDays

January 23, 2020
Tweet

More Decks by GoDays

Other Decks in Technology

Transcript

  1. About me Cloud Developer Master Thesis about npm Security (Analysis

    written in Go) Co-organizer Gophers Darmstadt
  2. Nope - still modern and very powerful Scripting with Bash

    CI/CD automation Machine Readable Output Usable for API interactions Rapid prototyping
  3. Bad UX in CLIs Do you remember how to extract

    a tar? tar -C /usr/local -xzf sample.tar.gz
  4. “A user interface is like a joke. If you have

    to explain it, it’s not that good.” Martin LeBlanc, CEO of Iconfinder
  5. Cobra Features Automatic generation of bash completion, man pages and

    help Intelligent suggestions Different flag types Code generator
  6. Viper integration App Configuration Reads config files Reads from Environment

    Variables Reading from remote config systems like etcd Live watching and re-reading
  7. Great help is essential $ git $ git --help $

    git help $ git -h # get help for subcommand $ git subcommand --help $ git subcommand -h
  8. 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
  9. Mind the streams stdout get written to file or can

    be piped stderr get printed on terminal stdin accepted as input for root command
  10. Handle things going wrong Informative Errors: Error Code Error title

    Fix for the Error URL with more information
  11. 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
  12. And some more... Be fancy! Use tables Be speedy! Encourage

    contributions Be clear about subcommands Follow XDG-spec