Powerful Command-Line Applications in Go

Book description

Whether you want to automate tasks, analyze data, parse logs, talk to network services, or address other systems requirements, writing your own command-line tool may be the fastest - and perhaps the most fun - way to do it. The Go programming language is a great choice for developing tools that are fast, reliable, and cross-platform. Create command-line tools that work with files, connect to services, and even manage external processes, all while using tests and benchmarks to ensure your programs are fast and correct.

When you want to develop cross platform command-line tools that are fast and reliable, use Go, a modern programming language that combines the reliability of compiled languages with the ease of use and flexibility of dynamic typed languages. Work through practical examples to develop elegant and efficient tools by applying Go's rich standard library, its built in support for concurrency, and its expressive syntax. Use Go's integrated testing capabilities to automatically test your tools, ensuring they work reliably even across code refactoring.

Develop CLI tools that interact with your users by using common input/output patterns, including environment variables and flags. Handle files to read or persist data, and manipulate paths consistently in cross-platform scenarios. Control processes and handle signals, and use a benchmark driven approach and Go's concurrency primitives to create tools that perform well. Use powerful external libraries such as Cobra to create modern and flexible tools that handle subcommands, and develop tools that interact with databases, APIs, and network services. Finally, leverage what you learned by tackling additional challenges at the end of each chapter.

What You Need:

Go 1.8 or higher, an internet connection to download the example files and additional libraries, and a text editor to write your programs.

Publisher resources

View/Submit Errata

Table of contents

  1.  Foreword
  2.  Acknowledgments
  3.  Preface
    1. What’s in This Book
    2. How to Use This Book
    3. About the Example Code
    4. Go Modules
    5. Online Resources
  4. 1. Your First Command-Line Program in Go
    1. Building the Basic Word Counter
    2. Testing the Basic Word Counter
    3. Adding Command-Line Flags
    4. Compiling Your Tool for Different Platforms
    5. Exercises
    6. Wrapping Up
  5. 2. Interacting with Your Users
    1. Organizing Your Code
    2. Defining the To-Do API
    3. Creating the Initial To-Do Command-Line Tool
    4. Testing the Initial CLI Implementation
    5. Handling Multiple Command-Line Options
    6. Display Command-Line Tool Usage
    7. Improving the List Output Format
    8. Increasing Flexibility with Environment Variables
    9. Capturing Input from STDIN
    10. Exercises
    11. Wrapping Up
  6. 3. Working with Files in Go
    1. Creating a Basic Markdown Preview Tool
    2. Writing Tests for the Markdown Preview Tool
    3. Adding Temporary Files to the Markdown Preview Tool
    4. Using Interfaces to Automate Tests
    5. Adding an Auto-Preview Feature
    6. Cleaning Up Temporary Files
    7. Improving the Markdown Preview Tool with Templates
    8. Exercises
    9. Wrapping Up
  7. 4. Navigating the File System
    1. Developing a File System Crawler
    2. Testing with Table-Driven Testing
    3. Deleting Matched Files
    4. Testing with the Help of Test Helpers
    5. Logging Deleted Files
    6. Archiving Files
    7. Exercises
    8. Wrapping Up
  8. 5. Improving the Performance of Your CLI Tools
    1. Developing the Initial Version of colStats
    2. Writing Tests for colStats
    3. Benchmarking Your Tool
    4. Profiling Your Tool
    5. Reducing Memory Allocation
    6. Tracing Your Tool
    7. Improving the colStats Tool to Process Files Concurrently
    8. Reduce Scheduling Contention
    9. Exercises
    10. Wrapping Up
  9. 6. Controlling Processes
    1. Executing External Programs
    2. Handling Errors
    3. Writing Tests for Goci
    4. Defining a Pipeline
    5. Adding Another Step to the Pipeline
    6. Handling Output from External Programs
    7. Running Commands with Contexts
    8. Integration Tests with a Local Git Server
    9. Testing Commands with Mock Resources
    10. Handling Signals
    11. Exercises
    12. Wrapping Up
  10. 7. Using the Cobra CLI Framework
    1. Starting Your Cobra Application
    2. Navigating Your New Cobra Application
    3. Adding the First Subcommand to Your Application
    4. Starting the Scan Package
    5. Creating the Subcommands to Manage Hosts
    6. Testing the Manage Hosts Subcommands
    7. Adding the Port Scanning Functionality
    8. Using Viper for Configuration Management
    9. Generating Command Completion and Documentation
    10. Exercises
    11. Wrapping Up
  11. 8. Talking to REST APIs
    1. Developing a REST API Server
    2. Testing the REST API Server
    3. Completing the REST API Server
    4. Developing the Initial Client for the REST API
    5. Testing the Client Without Connecting to the API
    6. Viewing a Single Item
    7. Adding an Item
    8. Testing HTTP Requests Locally
    9. Completing and Deleting Items
    10. Executing Integration Tests
    11. Exercises
    12. Wrapping Up
  12. 9. Developing Interactive Terminal Tools
    1. Initializing the Pomodoro Application
    2. Storing Data with the Repository Pattern
    3. Testing the Pomodoro Functionality
    4. Building the Interface Widgets
    5. Organizing the Interface’s Layout
    6. Building the Interactive Interface
    7. Initializing the CLI with Cobra
    8. Exercises
    9. Wrapping Up
  13. 10. Persisting Data in a SQL Database
    1. Getting Started with SQLite
    2. Go, SQL, and SQLite
    3. Persisting Data in the Database
    4. Testing the Repository with SQLite
    5. Updating the Application to Use the SQLite Repository
    6. Displaying a Summary to the Users
    7. Exercises
    8. Wrapping Up
  14. 11. Distributing Your Tool
    1. Starting the Notify Package
    2. Including OS-Specific Data
    3. Including OS-Specific Files in the Build
    4. Testing the Notify Package
    5. Conditionally Building Your Application
    6. Cross-Compiling Your Application
    7. Compiling Your Go Application for Containers
    8. Distributing Your Application as Source Code
    9. Exercises
    10. Wrapping Up

Product information

  • Title: Powerful Command-Line Applications in Go
  • Author(s): Ricardo Gerardi
  • Release date: December 2021
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680509328