Book Image

Building Modern CLI Applications in Go

By : Marian Montagnino
Book Image

Building Modern CLI Applications in Go

By: Marian Montagnino

Overview of this book

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.
Table of Contents (21 chapters)
1
Part 1: Getting Started with a Solid Foundation
6
Part 2: The Ins and Outs of a CLI
10
Part 3: Interactivity and Empathic Driven Design
14
Part 4: Building and Distributing for Different Platforms

What this book covers

Chapter 1, Understanding CLI Standards, The Command-Line Interface (CLI) was initially created as a way to interact with Operating Systems before graphical user interfaces (GUIs) were invented. Although the GUI and web have become more common, there has been a resurgence of CLI development in recent years, particularly as an additional offering alongside a company’s API. In this chapter, you will learn about the history and anatomy of the CLI, the principles of UNIX, and why Go is a compelling language for building CLI applications.

Chapter 2, Structuring Go Code for CLI Applications, This chapter serves as a guide for those who are unsure of how to begin creating a new CLI application. It covers popular ways to structure code, the concept of domain-driven design, and provides an example of a real-world use case for an audio metadata CLI application. By the end of the chapter, readers will have the necessary skills to develop an application based on their specific use cases and requirements.

Chapter 3, Building an Audio Metadata CLI, This chapter provides hands-on learning by walking readers through building an audio metadata CLI’s use cases from start to finish. The code is available online and can be explored independently or alongside the chapter. Additionally, readers are encouraged to use their imagination to consider alternative ways to implement commands.

Chapter 4, Popular Frameworks for Building CLIs, In this chapter, the most popular frameworks for developing modern CLI applications will be explored, with a focus on Cobra and its ability to quickly generate the scaffolding needed for a CLI application. Viper, which easily integrates with Cobra and provides extensive configuration options for applications, will also be discussed.

Chapter 5, Defining the Command-Line Process, This chapter delves deeper into the anatomy of a command-line application, breaking down the different types of input such as subcommands, arguments, and flags, as well as other inputs such as stdin, signals, and control characters. It also provides examples of processing data for each input type and how to return the result in a way that is easily interpreted by both humans and computers.

Chapter 6, Calling External Processes, Handling Errors and Timeouts, This chapter will teach you how to call external processes and handle errors, including timeouts, that may occur when interacting with other commands or API services. The os/exec package is discussed, which allows for the creation and running of commands with various options, such as retrieving data from standard output and standard error pipes. Additionally, the net/http package is explored for calling external API service endpoints, and the chapter concludes with strategies for capturing and handling errors that may arise.

Chapter 7, Developing for Different Platforms, One of the things that makes building a command line application powerful is the ability to easily create code that can run on different machines, regardless of their operating systems. The os, time, path, and runtime packages are great tools to help developers create platform-independent code. In this chapter, we explore the functions and methods in these packages with simple examples, and show how to specify code for operating systems using build tags. By the end, you’ll feel more confident in your ability to write code that works across multiple platforms.

Chapter 8, Building for Humans Versus Machines, Developing your command-line application with your end user in mind is an important aspect to consider for better usability. In this chapter, we’ll explore ways to build for humans and scripts, use ASCII art to increase information density, and the importance of consistency for better navigation across different commands and subcommands.

Chapter 9, The Empathic Side of Development, In this chapter, you will learn how to use empathy to develop a better command-line interface (CLI) by considering the output and errors written, providing empathetic documentation, and readily available help and support for users. By rewriting errors in a way that users can easily understand, providing detailed logging and help features like man pages, usage examples, and bug submission options, developers can create an empathetic CLI that meets the user’s perspective and provides them with reassurance.

Chapter 10, Interactivity with prompts and Terminal Dashboards, This chapter will show you how to improve usability by adding interactivity to your command-line application using either prompts or terminal dashboards. By providing examples and step-by-step instructions for creating surveys and dashboards, this chapter will help you build a more engaging and user-friendly interface. However, it is important to disable interactivity when not outputting to a terminal.

Chapter 11, Custom Builds and Testing CLI Commands, To enhance the stability and scalability of a growing Go project, it is essential to incorporate build tags with Boolean logic to enable targeted builds and testing. This chapter demonstrates the use of build tags and testing through a real-world example, the audiofile CLI, and covers topics such as integrating levels, enabling profiling, and testing HTTP clients.

Chapter 12, Cross Compilation Across Different Platforms, This chapter explains cross-compilation in Go, including the different operating systems and architectures that Go can compile and how to determine which is needed. It covers topics such as manual compilation versus build automation tools, using GOOS and GOARCH, compiling for Linux, MacOS, and Windows, and scripting to compile for multiple platforms.

Chapter 13, Using Containers for Distribution, In this chapter, we’ll dive into Docker containers and how they can benefit you when testing and sharing your CLI application. We’ll begin with the basics and gradually move onto more complex topics, such as using containers for integration testing. Additionally, we’ll weigh up the pros and cons of using Docker, helping you determine whether it’s the right choice for you. By the end of the chapter, you’ll be equipped to containerize your application, test it through Docker, and share it with others via Docker Hub.

Chapter 14, Publishing your Go binary as a Homebrew Formula with GoReleaser, In this chapter, you’ll learn how to automate the release of a Go binary as a Homebrew formula using GoReleaser and GitHub Actions. GoReleaser simplifies the creation, testing, and distribution of Go binaries, and GitHub Actions is a CI/CD platform that automates software development workflows. By creating a Homebrew tap for your application, you can simplify the installation process for MacOS users and reach a larger audience.