Building Modern CLI Applications in Go

Book description

Evolve the humble CLI using Go and unleash the next generation of powerful, flexible, and empathy-driven interfaces Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Discover how Go enables the development of elegant and intuitive CLIs
  • Explore a range of CLI development aspects and pick up a vast array of best practices
  • Create engaging and user-friendly interfaces and learn how to distribute them

Book Description

Although graphical user interfaces (GUIs) are intuitive and user-friendly, nothing beats a command-line interface (CLI) when it comes to productivity. Many organizations settle for a GUI without searching for alternatives that offer better accessibility and functionality. If this describes your organization, then pick up this book and get them to rethink that decision.

Building Modern CLI Applications in Go will help you achieve an interface that rivals a GUI in elegance yet surpasses it in high-performance execution. Through its practical, step-by-step approach, you’ll learn everything you need to harness the power and simplicity of the Go language to build CLI applications that revolutionize the way you work.

After a primer on CLI standards and Go, you’ll be launched into tool design and proper framework use for true development proficiency. The book then moves on to all things CLI, helping you master everything from arguments and flags to errors and API calls. Later, you’ll dive into the nuances of empathic development so that you can ensure the best UX possible, before you finish up with build tags, cross-compilation, and container-based distribution.

By the end of this UX book, you’ll be fully equipped to take the performance and flexibility of your organization’s applications to the next level.

What you will learn

  • Master the Go code structure, testing, and other essentials
  • Add a colorful dashboard to your CLI using engaging ASCII banners
  • Use Cobra, Viper, and other frameworks to give your CLI an edge
  • Handle inputs, API commands, errors, and timeouts like a pro
  • Target builds for specific platforms the right way using build tags
  • Build with empathy, using easy bug submission and traceback
  • Containerize, distribute, and publish your CLIs quickly and easily

Who this book is for

This book is for beginner- and intermediate-level Golang developers who take an interest in developing CLIs and enjoy learning by doing. You’ll need an understanding of basic Golang programming concepts, but will require no prior knowledge of CLI design and development. This book helps you join a community of CLI developers and distribute within the popular Homebrew package management tool.

Table of contents

  1. Building Modern CLI Applications in Go
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
    9. Download a free PDF copy of this book
  7. Part 1: Getting Started with a Solid Foundation
  8. Chapter 1: Understanding CLI Standards
    1. A brief introduction and history of the command line
      1. About the history
      2. Introducing the CLI
    2. The philosophy of CLI development
      1. Checklist for a successful CLI
    3. The guidelines
      1. Name
      2. Help and documentation
      3. Input
      4. Output
      5. Configuration
      6. Security
      7. Open source community
      8. Software lifespan and robustness
    4. Go for CLIs
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  9. Chapter 2: Structuring Go Code for CLI Applications
    1. Technical requirements
    2. Commonly used program layouts for robust applications
      1. Program layouts
      2. Common folders
    3. Determining use cases and requirements
      1. Use cases
      2. Requirements
      3. Disadvantages and benefits of use cases and requirements
      4. Use cases, diagrams, and requirements for a CLI
      5. Requirements for a metadata CLI
    4. Structuring an audio metadata CLI application
      1. Bounded context
      2. Language
      3. Entities and value objects
      4. Aggregation
      5. Service
      6. Events
      7. Repository
      8. Creating the structure
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  10. Chapter 3: Building an Audio Metadata CLI
    1. Technical requirements
    2. Defining the components
      1. cmd/
      2. cmd/api/
      3. cmd/cli/
      4. cmd/cli/command
      5. extractors/
      6. extractors/tags
      7. extractors/transcript
      8. internal/interfaces
      9. models/
      10. services/metadata
      11. storage/
      12. vendor/
    3. Implementing use cases
      1. Uploading audio
      2. Requesting metadata
    4. Testing a CLI
      1. Manual testing
      2. Testing and mocking
    5. Summary
    6. Questions
    7. Answers
  11. Chapter 4: Popular Frameworks for Building CLIs
    1. Technical requirements
    2. Cobra – a library for building modern CLI applications
      1. Creating subcommands
      2. Global, local, and required flags
      3. Intelligent suggestions
      4. Automatically generated help and man pages
      5. Powering your CLI
    3. Viper – easy configuration for CLIs
      1. Configuration types
      2. Watching for live config changes
    4. Basic calculator CLI using Cobra and Viper
      1. The Cobra CLI commands
      2. The Viper configuration
      3. Running the basic calculator
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  12. Part 2: The Ins and Outs of a CLI
  13. Chapter 5: Defining the Command-Line Process
    1. Technical requirements
    2. Receiving the input and user interaction
      1. Defining subcommands, arguments, and flags
      2. Piping
      3. Signals and control characters
      4. User interaction
    3. Processing data
    4. Returning the resulting output and defining best practices
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  14. Chapter 6: Calling External Processes and Handling Errors and Timeouts
    1. Technical requirements
    2. Calling external processes
      1. The os/exec package
      2. Creating commands using the Cmd struct
      3. Running the command
    3. Interacting with REST APIs
      1. Get request
      2. Pagination
      3. Rate limiting
    4. Handling the expected – timeouts and errors
      1. Timeouts with external command processes
      2. Errors or panics with external command processes
      3. Timeouts and other errors with HTTP requests
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  15. Chapter 7: Developing for Different Platforms
    1. Technical requirements
    2. Packages for platform-independent functionality
      1. The os package
      2. The time package
      3. The path package
      4. The runtime package
    3. Implementing independent or platform-specific code
      1. Platform-independent code
      2. Platform-specific code
    4. Build tags for targeted platforms
      1. The build package
      2. Build tags
      3. OS-level differences
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  16. Part 3: Interactivity and Empathic Driven Design
  17. Chapter 8: Building for Humans versus Machines
    1. Technical requirements
    2. Building for humans versus machines
      1. Is it a TTY?
      2. Designing for a machine
      3. Designing for a human
    3. Increasing information density with ASCII art
      1. Displaying information with tables
      2. Clarifying with emojis
      3. Using color with intention
      4. Spinners and progress bars
      5. Disabling colors
    4. Being consistent across CLIs
      1. Naming
      2. Positional versus flag arguments
      3. Flag naming
      4. Usage
    5. Summary
    6. Questions
    7. Further reading
    8. Answers
  18. Chapter 9: The Empathic Side of Development
    1. Technical requirements
    2. Rewriting errors to be human-readable
      1. Guidelines for writing error messages
      2. Decorating errors
      3. Customizing errors
      4. Writing better error messages
    3. Providing debug and traceback information
      1. Logging data
      2. Initiating a logger
      3. Implementing a logger
      4. Trying out verbose mode to view stack traces
    4. Effortless bug submission
    5. Help, documentation, and support
      1. Generating help text
      2. Generating man pages
      3. Embedding empathy into your documentation
    6. Summary
    7. Questions
    8. Further reading
    9. Answers
  19. Chapter 10: Interactivity with Prompts and Terminal Dashboards
    1. Technical requirements
    2. Guiding users with prompts
    3. Designing a useful terminal dashboard
      1. Learning about Termdash
    4. Implementing a terminal dashboard
      1. Creating the terminal layer
      2. Creating the infrastructure layer
      3. Creating the container layer
      4. Creating the widgets layer
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  20. Part 4: Building and Distributing for Different Platforms
  21. Chapter 11: Custom Builds and Testing CLI Commands
    1. Technical requirements
    2. What are build tags and how can you use them?
    3. How to utilize build tags
      1. Creating a pro, free, and dev version
      2. Adding build tags to enable pprof
    4. Building with tags
      1. Building a free version
      2. Building a pro version
      3. Building to enable pprof on the pro version
    5. Testing CLI commands
      1. Mocking the HTTP client
      2. Handling test configuration
      3. Creating a test for a command
      4. Running the tests
    6. Summary
    7. Questions
    8. Answers
    9. Further reading
  22. Chapter 12: Cross-Compilation across Different Platforms
    1. Technical requirements
    2. Manual compilation versus build automation tools
    3. Using GOOS and GOARCH
    4. Compiling for Linux, macOS, and Windows
      1. Building using tags
      2. Building using the GOOS environment variable
      3. Building using the GOARCH environment variable
      4. Installing using tags and GOOS env va
      5. Installing using tags and GOARCH env var
    5. Scripting to compile for multiple platforms
      1. Creating a bash script to compile in Darwin or Linux
      2. Creating a PowerShell script in Windows
    6. Summary
    7. Questions
    8. Answers
    9. Further reading
  23. Chapter 13: Using Containers for Distribution
    1. Technical requirement
    2. Why use containers?
      1. Benefiting from containers
      2. Deciding not to use containers
      3. Building a simple Docker image
      4. Running a simple Docker container
    3. Testing with containers
      1. Creating the integration test file
      2. Writing the integration tests
      3. Writing the Dockerfiles
    4. Distributing with containers
      1. Building a new image to run as an executable
      2. Interacting with your container as an executable
      3. Mapping host machine to container file paths
      4. Reducing image size by using multi-stage builds
      5. Distributing your Docker image
      6. Depending on Docker
    5. Summary
    6. Questions
    7. Further reading
    8. Answers
  24. Chapter 14: Publishing Your Go Binary as a Homebrew Formula with GoReleaser
    1. Technical requirements
    2. GoReleaser workflow
      1. Defining the workflow
    3. Trigger release
      1. Tag and push the code
    4. Installing with Homebrew and Testing
    5. Summary
    6. Questions
    7. Further reading
    8. Answers
  25. Index
    1. Why subscribe?
  26. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Building Modern CLI Applications in Go
  • Author(s): Marian Montagnino, William Kennedy
  • Release date: March 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781804611654