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

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.

6e3ea86995d93d35c0fadf2694bca773?s=128

GoDays

January 23, 2020
Tweet

Transcript

  1. cosee GmbH, Markus Zimmermann, @markus_zm 12 Factor CLI Apps with

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

    written in Go) Co-organizer Gophers Darmstadt
  3. Section one Why should we still use CLIs in 2020?

  4. Old technology? Source: Unsplash - Anastasia Dulgier

  5. Nope - still modern and very powerful Scripting with Bash

    CI/CD automation Machine Readable Output Usable for API interactions Rapid prototyping
  6. Section two UX for CLIs Multi-Cloud?

  7. Bad UX in GUIs Source: UX Planet

  8. Bad UX in CLIs Do you remember how to extract

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

    to explain it, it’s not that good.” Martin LeBlanc, CEO of Iconfinder
  10. Section three The snakes that power Go CLIs

  11. The two libraries for Go CLIs

  12. Powers many popular CLIs

  13. Cobra Features Automatic generation of bash completion, man pages and

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

    Variables Reading from remote config systems like etcd Live watching and re-reading
  15. Demo Time

  16. Section four 12 Factor CLIs

  17. Credit to Jeff Dickey @jdxcode

  18. Great help is essential $ git $ git --help $

    git help $ git -h # get help for subcommand $ git subcommand --help $ git subcommand -h
  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
  20. git version git --version => git version 2.17.1 What version

    am I on?
  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
  22. Handle things going wrong Informative Errors: Error Code Error title

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

    contributions Be clear about subcommands Follow XDG-spec
  25. for listening! Source: Tomasz Sroka – unsplash Thanks

  26. blog.cosee.biz cosee.jobs