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