Menu

📰
0

The Airgap Test: Refactoring a Cobra CLI into a Library API

DEV Community: go·Bala Paranj·about 1 month ago
#lIPC2izd
#dev#class#code#highlight#cobra#article
Reading 0:00
15s threshold

How a single rule — only RunE touches *cobra.Command — turned a CLI codebase into a library that happens to ship with a CLI. I run a static analysis pass on my Go CLI that I call a contamination scan. It looks for places where the CLI framework has leaked out of the adapter layer and into code that has no business knowing a CLI exists. A recent run flagged 25 instances across 11 files. That's not a lot of code, but I was experimenting a lot with the existing codebase and my previous attempt at decoupling Cobra from existing business logic was still not complete. The reason I had to tackle the tech debt now was I now have lot of commands and running them in a terminal is not a productive way for me to create small programs that exercises a given behavior and find bugs to fix. Once I write a program using the Stave library, I just use different data to test different scenario, the code remains the same. It is faster to find and fix bugs. You might ask, that's why we have integration tests and unit tests.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More